A boolean data type represents one of the two values: True or False. The use of these data types will be clear once we start using the comparison operator. The first letter T for True and F for False should be capital, unlike in JavaScript. Example: Boolean Values
print(True)
print(False)
Operators
Python language supports several types of operators. In this section, we will focus on a few of them.
Assignment Operators
Assignment operators are used to assign values to variables. Let us take = as an example. An equal sign in mathematics shows that two values are equal, however in Python, it means we are storing a value in a certain variable and we call it an assignment or assigning value to a variable. The table below shows the different types of Python assignment operators, taken from w3school.
Arithmetic Operators:
Addition(+): a + b
Subtraction(-): a – b
Multiplication(*): a * b
Division(/): a / b
Modulus(%): a % b
Floor division(//): a // b
Exponentiation(**): a ** b
Example: Integers
# Arithmetic Operations in Python# Integersprint('Addition: ', 1+2) # 3print('Subtraction: ', 2-1) # 1print('Multiplication: ', 2*3) # 6print ('Division: ', 4/2) # 2.0 Division in Python gives floating numberprint('Division: ', 6/2) # 3.0 print('Division: ', 7/2) # 3.5print('Division without the remainder: ', 7//2) # 3, gives without the floating number or without the remainingprint ('Division without the remainder: ',7//3) # 2print('Modulus: ', 3%2) # 1, Gives the remainderprint('Exponentiation: ', 2**3) # 9 it means 2 * 2 * 2
Example: Floats
# Floating numbersprint('Floating Point Number, PI', 3.14)
print('Floating Point Number, gravity', 9.81)
Let’s declare a variable and assign a number data type. I am going to use single character variable but remember do not develop a habit of declaring such types of variables. Variable names should be all the time mnemonic.
Example:
# Declaring the variable at the top firsta=3# a is a variable name and 3 is an integer data typeb=2# b is a variable name and 3 is an integer data type# Arithmetic operations and assigning the result to a variabletotal=a+bdiff=a-bproduct=a*bdivision=a/bremainder=a%bfloor_division=a//bexponential=a**b# I should have used sum instead of total but sum is a built-in function - try to avoid overriding built-in functionsprint(total) # if you do not label your print with some string, you never know where the result is coming fromprint('a + b = ', total)
print('a - b = ', diff)
print('a * b = ', product)
print('a / b = ', division)
print('a % b = ', remainder)
print('a // b = ', floor_division)
print('a ** b = ', exponentiation)
Let us start connecting the dots and start making use of what we already know to calculate (area, volume, density, weight, perimeter, distance, force).
Example:
# Calculating area of a circleradius=10# radius of a circlearea_of_circle=3.14*radius**2# two * sign means exponent or powerprint('Area of a circle:', area_of_circle)
# Calculating area of a rectanglelength=10width=20area_of_rectangle=length*widthprint('Area of rectangle:', area_of_rectangle)
# Calculating a weight of an objectmass=75gravity=9.81weight=mass*gravityprint(weight, 'N') # Adding unit to the weight# Calculate the density of a liquidmass=75# in Kgvolume=0.075# in cubic meterdensity=mass/volume# 1000 Kg/m^3
Comparison Operators
In programming we compare values, we use comparison operators to compare two values. We check if a value is greater or less or equal to another value. The following table shows Python comparison operators which were taken from w3shool.
Example: Comparison Operators
print(3>2) # True, because 3 is greater than 2print(3>=2) # True, because 3 is greater than 2print(3<2) # False, because 3 is greater than 2print(2<3) # True, because 2 is less than 3print(2<=3) # True, because 2 is less than 3print(3==2) # False, because 3 is not equal to 2print(3!=2) # True, because 3 is not equal to 2print(len('mango') ==len('avocado')) # Falseprint(len('mango') !=len('avocado')) # Trueprint(len('mango') <len('avocado')) # Trueprint(len('milk') !=len('meat')) # Falseprint(len('milk') ==len('meat')) # Trueprint(len('tomato') ==len('potato')) # Trueprint(len('python') >len('dragon')) # False# Comparing something gives either a True or Falseprint('True == True: ', True==True)
print('True == False: ', True==False)
print('False == False:', False==False)
In addition to the above comparison operator Python uses:
is: Returns true if both variables are the same object(x is y)
is not: Returns true if both variables are not the same object(x is not y)
in: Returns True if the queried list contains a certain item(x in y)
not in: Returns True if the queried list doesn’t have a certain item(x in y)
print('1 is 1', 1is1) # True - because the data values are the sameprint('1 is not 2', 1isnot2) # True - because 1 is not 2print('A in Asabeneh', 'A'in'Asabeneh') # True - A found in the stringprint('B in Asabeneh', 'B'in'Asabeneh') # False - there is no uppercase Bprint('coding'in'coding for all') # True - because coding for all has the word codingprint('a in an:', 'a'in'an') # Trueprint('4 is 2 ** 2:', 4is2**2) # True
Logical Operators
Unlike other programming languages python uses keywords and, or and not for logical operators. Logical operators are used to combine conditional statements:
print(3>2and4>3) # True - because both statements are trueprint(3>2and4<3) # False - because the second statement is falseprint(3<2and4<3) # False - because both statements are falseprint('True and True: ', TrueandTrue)
print(3>2or4>3) # True - because both statements are trueprint(3>2or4<3) # True - because one of the statements is trueprint(3<2or4<3) # False - because both statements are falseprint('True or False:', TrueorFalse)
print(not3>2) # False - because 3 > 2 is true, then not True gives Falseprint(notTrue) # False - Negation, the not operator turns true to falseprint(notFalse) # Trueprint(notnotTrue) # Trueprint(notnotFalse) # False
Now do some exercises for your brain and your muscles.
Exercises – Day 3
Declare your age as an integer variable
Declare your height as a float variable
Declare a variable that stores a complex number
Write a script that prompts the user to enter the base and height of the triangle and calculate the area of this triangle (area = 0.5 x b x h).
Write a script that prompts the user to enter side a, side b, and side c of the triangle. Calculate the perimeter of the triangle (perimeter = a + b + c).
Write a script that prompts the user to enter a number of years. Calculate the number of seconds a person can live. Assume a person can live a hundred years
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.