Python 3 Tutorial for Everyone Chapter 2: Comments | Variable | Operators
In Python there are 2 ways to write comments. For single line comment we can use # sign.
This is a single line comment
For multiline comments we can use ”’ at the beginning and ”” at the end. And Inside we can write multiline comments. Instead of single quotes we can also use double quotes “”” …. “””
''' Multiline Comments ''' """ Again multiline comments """
We have a video tutorial for this chapter for Bangla speaking people:
Variable is like a container where we can store value. In a program when we need the value we just use the name to retrieve the value we assigned before. To declare variable we just simple write the name and using = sign we can assign value to it. In the following code, we created six variables.
# Variable name = "Grocery List" detail = 'Buy from supershop' number_of_items = 5 budget = 2500 # taka amount_of_rice = 1.56 # kg should_we_buy_today = True print( name, type(name) ) print( detail, type(detail) ) print( number_of_items, type(number_of_items) ) print( amount_of_rice, type(amount_of_rice) ) print( should_we_buy_today, type(should_we_buy_today) )
Grocery List <class 'str'> Buy from supershop <class 'str'> 5 <class 'int'> 1.56 <class 'float'> True <class 'bool'>
Using the print() function, we passed comma separated values to print the variables’ value and what type they are. type(variable) is a function which produce the output of a variable type. We see the first output is ‘Grocery List’ which is ‘str’ type that means it is a string variable. Similarly we see other types like 5 is int, 1.56 is float and True is bool type.
Python is a duck type language. Which means we can store any value in a variable at anytime. In programming language there is another term which is called static type. In a static type programming language, we have to declare a variable type before assign any value.
For example, if we use Apple’s Swift language we have to define variable like this way:
var num:Int = 10 var fNum:Float = 103.3 var str:String = "Life is good" fNum = num // Is not possible without type casting
So in static programming language, we have to either refer what type a variable is, or the value will infer automatically based on value. But we can not assign a floating point value in a integer variable after defined it. But in Python it is possible.
Variable name has some rules. Python is case sensitive language so a variable named ‘num’ and ‘Num’ are not equal. Variable name can not be started with digit like 1_plus. But plus_1 is valid.
1_plus = 100 # not a valid name plus_1 = 100 # is okay
In python variable names are written as snake case. So we need to separate words using _ . In Swift and some other languages camel case is used. In camel case, every second words will be start with capital letter.
first_name = "Jonathon" # snake case python style firstName = "Jonathon" # camel case Swift style
There are some reserved words in python, which can not be used as variable name.
class = 'M' # reserved word not a valid name for = 3 # reserved word not a valid name
So we need to remember these common rules to define variable. We should also try to define good variable names so that it make some sense.
amount_of_rice = 1.56 # Good name makes a meaning' ar = 1.56 # bad name doesn't make sense
We can use most of the common operators in mathematics in python language. To add a value in number_of_items we can add the variable name + value after the equal sign.
number_of_items = 5 number_of_items = number_of_items + 2 print( number_of_items )
There is a shortcut version of the above example. We can use += value to a variable like the below example instead of variable + value
number_of_items += 2 print( number_of_items )
Similarly we can do similar way for other operations
amount_of_rice += 2.3 # float
# both are same amount_of_rice -= 2 amount_of_rice = amount_of_rice - 2
number_of_items *= 3 # Multiplication number_of_items /= 3 # Division
Division of 2 Integers
x = 10 y = 2 result = x / y print (result)
In the above program, we see that the output is 5.0 a floating point number. So that means if we divide 2 integer number we will get a floating point value. If we want the result as Integer we have to type cast it.
x = 10 y = 2 result = int (x / y) print (result)
But in this case we have to remember, the values after the . will be truncated from floating point to integer.
x = 10 y = 3 result = x / y print (result) result = int (x / y) print (result)
So for this case we will get the following result:
Sometimes it is very important to know whether a division expression produce any remainder or not. Specially if we want to test whether a number is even or odd.
For this kind of issue we can use the modulus % operator. It is also called remainder operator
x = 10 y = 3 print (x, y, x % y)
10 3 1
So if we divide 10 by 3 we will get 1 as remainder.
To make a value negative we can just use the – sign before value or variable when assign
x = 10 x = -x print (x)
If we need the dividend and remainder at the same time, we can use divmod(x,y) operator.
x = 10 y = 3 result = divmod(x, y) print (result)
Divmod(x,y) produce a tuple value where the first value is dividend and last value is the remainder.
To generate exponential number we can use ** operator. In some other languages ^ character is also used.
x = 2 result = x ** 4 print (result)
There is another way to get exponential number. We can use pow(x,y) operator
x = 2 result = pow (x, 4)
Thank you for reading the post