Lesson 2 : Data Types and Operators
Exercise : Here is Python Code related to Operators and Variables
# ---------------- OPERATORS ----------------
n1 = 200
n2 = 20
print("Arithmetic Operators ")
print(n1, n2, n1 + n2, n1 - n2)
print(n1 * n2)
print(n1 / n2)
print("Floating Point or Fractional numbers " )
print(n2 / n1)
print("Modulus i.e. remainder of dividing two numbers : ")
print(5 % 2) # Simple example
print(n2 % n1) # Mod
# Find whether a number is even or not
n3 = int(input(" Enter a number "))
if(n3 % 2 == 0):
print("Input number is an Even number")
else:
print("Input number is an Odd number")
# ----------- MORE OPERATORS -----------------
# ----------- '//' Floor division - division that results into whole number adjusted to the left in the number line
# or Closest whole number multiple
boxes = 100
apples = 26 # Assume even distribution in all boxes
print (" Can I pack all apples assuming even distribution per box ? ")
if (boxes % apples == 0):
print("Yes")
else:
print("No")
print (" How many apples can I pack per box ? ")
print(boxes // apples) # Closet whole number Multiple
+, -, *, /, %, ** (raised to), //
q = 459
p = 0.098
print(q, p, p * q)
#---------- COMPLEX NUMBERS ------------
a = 20 + 10j
a.real # Returns the real component
a.img # Returns the Imaginary component
b = 20 - 10j
c = a*b (which is a^2 + b^2)
# ----------- VARIABLES-----------------
# Variables seen before
x,y,z = 10, 20, 30
print(x, y, z)
x, y = y, x
print(x, y)
# Variable Names
15APPLES = 15
APPLES15 = 10
apples15 = 20
print(" Variables %d %d "% (APPLES15, apples15))
apples15_new = apples15 * 3
print(" Variables %d %d "% (APPLES15, apples15_new))
dollar$100 = 100
print(" Variables %d "% dollar$100)
print( "Two variables can refer to same data ? ")
flower = 'rose'; red_flower = flower;
print(" Flower : %s"% flower)
print(" Red Rose : %s" red_flower)
flower = 'rose and violet';
red_flower = flower;
print(" Red Rose Changed : %s %s"% (red_flower, flower))
mark = 100
print("Mark ? %d"% mark)
mark = mark + 10
print("Mark + 10 ? %d"% mark)
# ----------- TYPE CASTING-----------------
print("Integer converted to String %s" % mark[0])
markStr = str(mark)
print("Integer converted to String %s" % markStr[0])
round(3.145)
int("100") + 11
float("{:.3f}".format(100.00)