The document discusses Python comments and variables. It explains that comments in Python begin with # and can be placed on their own line or at the end of a line. Multiline comments can use multiple # or triple quotes. Variables do not need declaration and are created when a value is assigned. Variable names can be short or descriptive and have certain naming rules. The data types in Python include strings, integers, floats, complexes and various sequence, mapping and set types. The type of an object can be checked with type() and data types are set automatically based on the value assigned.
Lecture02 - Fundamental Programming with Python Language
1. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Fundamental Programming with Python
(Lecture # 02)
by
Muhammad Haroon
Python Comments
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code.
Creating a Comment
Comments starts with a #, and Python will ignore them:
Example
#This is a comment
print("This is the first python program!")
2. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Comments can be placed at the end of a line, and Python will ignore the rest of the line:
Example
print("This is the first python program!") #This is a comment
3. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Comments does not have to be text to explain the code, it can also be used to prevent Python from
executing code:
Example
#print("Muhammad Haroon!")
print("Muhammad Haroon!")
Multi Line Comments
Python does not really have a syntax for multi line comments.
To add a multiline comment you could insert a # for each line:
Example
#This is a comment
#written in
#more than just one line
print("Muhammad Haroon!")
4. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Or, not quite as intended, you can use a multiline string.
Since Python will ignore string literals that are not assigned to a variable, you can add a multiline string
(triple quotes) in your code, and place your comment inside it:
Example
"""
This is a comment
written in
more than just one line
"""
print("Muhammad Haroon!")
5. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
As long as the string is not assigned to a variable, Python will read the code, but then ignore it, and you
have made a multiline comment.
Python Variables
Creating Variables
Variables are containers for storing data values.
Unlike other programming languages, Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
Example
x = 786
y = "Muhammad Haroon"
print(x)
print(y)
6. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Variables do not need to be declared with any particular type and can even change type after they have
been set.
Example
x = 786 # x is of type int
x = "Muhammad Haroon" # x is now of type str
print(x)
7. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
String variables can be declared either by using single or double quotes:
Example
x = "Muhammad Haroon"
# is the same as
x = 'Muhammad Haroon'
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, bikename, total_value).
Rules for Python variables:
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are three different variables)
Python Data Types
Built-in Data Types
In programming, data type is an important concept.
Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, in these categories:
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
Getting the Data Type
You can get the data type of any object by using the type() function:
8. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
Example
Print the data type of the variable x:
x = 786
print(type(x))
Setting the Data Type
In Python, the data type is set when you assign a value to a variable:
Example Data Type Result
x = "Muhammad Haroon" str 01.py
x = 786 int 02.py
x = 20.5 float 03.py
x = 1j complex 04.py
x = ["apple", "banana", "cherry"] list 05.py
x = ("apple", "banana", "cherry") tuple 06.py
x = range(6) range 07.py
x = {"name" : "Muhammad Haroon", "age" : 27} dict 08.py
9. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
x = {"apple", "banana", "cherry"} set 09.py
x = frozenset({"apple", "banana", "cherry"}) frozenset 10.py
x = True bool 11.py
x = b"Muhammad Haroon" bytes 12.py
x = bytearray(5) bytearray 13.py
x = memoryview(bytes(5)) memoryview 14.py
Setting the Specific Data Type
If you want to specify the data type, you can use the following constructor functions:
Example Data Type Result
x = str("Muhammad Haroon") str 15.py
x = int(786) int 16.py
x = float(10.5) float 17.py
x = complex(1j) complex 18.py
x = list(("apple", "banana", "cherry")) list 19.py
x = tuple(("apple", "banana", "cherry")) tuple 20.py
x = range(6) range 21.py
x = dict(name="Muhammad Haroon", age=27) dict 22.py
x = set(("apple", "banana", "cherry")) set 23.py
x = frozenset(("apple", "banana", "cherry")) frozenset 24.py
x = bool(5) bool 25.py
x = bytes(5) bytes 26.py
x = bytearray(5) bytearray 27.py
x = memoryview(bytes(5)) memoryview 28.py
10. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
11. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
12. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
13. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
14. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
15. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
16. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
17. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
18. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
19. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
20. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
21. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
22. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
23. Fundamental Programming with Python mr.harunahmad2014@gmail.com CS/IT/SE/CSE
Muhammad Haroon – PhD CS (Enrolled) from Hitec University Taxila 0300-7327761
End