L O A D I N G ## PYTHON TRAINING SESSIONS SEP 2023

# inbuilt functions, followed with ()
# are for comments – you are asking Python to ignore the content after #
print(“5+3”)
print(5+3)
print(“5+3=”,5+3,‘and’,“6*4=”,6*4)
# parameters are the values that we give to a function
# He asked, “how are you?”

# escape character \
# \\ \t \new \’ …

value1 = 5
print(value1)

value1 = 10
print(value1)

## Day 1: About print() function

# variables
a= 5
cost = 25
product1, product2 = 100,120
total_cost = product1 + product2
# variable names should begin with alphabet, and it can contain numbers & _
# invalid names: 1var, num.one
a=35

”’
Multi-line comment
Basic data types:
1. integer (int) – non-decimal numbers both -ve and +ve: -99, -66,0,33,999
2. float (float) – decimal numbers both -ve and +ve: -45.8 , -66.0 , 0.0, 5.78984744
3. complex (complex) – square root of -ve numbers: square root of -1 is i (maths) j in Python
4. Boolean (bool) – True  or False 
5. String (str) – text content
”’
# type() – type of the data
number_ppl = 55
print(“Data type of number_ppl:”, type(number_ppl))
cost_price = 66.50
print(“type(cost_price): “, type(cost_price))
value1 = 9j
print(“type(value1): “,type(value1))
print(“Square of value1 = “, value1 * value1)

bool_val1 = True # False
print(“type(bool_val1): “,type(bool_val1))

text_val1 = “HELLO”
print(“Type of text_val1 = “,type(text_val1))

# Formatting the output
quantity = 31
cost = 19
total_cost = quantity * cost
# total cost of 51 pens which cost 19 will be total_cost rupees
print(“total cost of”,quantity,“pens which cost”,cost,“will be”,total_cost,“rupees”)
# f-string – formatting the string
print(f”total cost of {quantity} pens which cost {cost} will be {total_cost} rupees”)

quantity = 31; total_cost = 900
cost = total_cost / quantity
print(f”total cost of {quantity} pens which cost {cost:.2f} will be {total_cost} rupees”)

player,country,position = “Virat”,“India”,“Opener”;
print(f”Player {player:<15} plays for {country:^10} at {position:>15} position”);
player,country,position = “Mbwangagya”,“Zimbabwe”,“Wicket-keeper”;
print(f”Player {player:<15} plays for {country:^10} at {position:>15} position”);

## Day 2: Data types and format string

#### Read and Practice the flowchart and Algorithm:

# Operators in Python
# Arithematic Operators: + – * / // (int division), ** (power) % (modulus)
a = 13
b = 10
print(a+b)
print(a-b)
print(a*b)
print(a / b) # 1.3 – output is always float
print(a // b) # only integer – 1
print(a ** b) # 13 to the power of 10
# % – mod – gives you the remainder
print(a % b)

# relational /conditional operators: < > <= >= == !=
# asking question: is it ??? – output is always a bool value (T/F)
print(“Relational: “)
a,b,c = 10,10,13
print(a < b) # is a less than b ? F
print(b > c) # F
print(a<=b) # T
print(b >= c) # F
print(a==b) # is equal to ? – T
print(a!=b) # F

# Logical operators: and or not
# input and output – both are bool
”’
Prediction 1: Rohit and Ishan will open the batting – F
Prediction 2: Rohit or Ishan will open the batting – T
Actual: Rohit and Gill opened the batting

When using AND – Everything condition has to be true to be True
When using OR – Even one condition is true, result will be True
Not taken only 1 input: Not True = False, Not F=T
”’
a,b,c = 10,10,13
print(a < b and b > c or a<=b and b >= c or a==b and a!=b)
# F

## Input() – is used to read values from the user
# int() float() str() complex() bool()
num1 = input(“Enter a number to add: “)
num1 = int(num1) #explicit conversion
print(num1, “and the data type is”,type(num1))
num2 = int(input(“Enter second number: “))
s = num1 + num2 #implicit conversion
print(“Sum of two numbers: “,s)

## A program to convert Celcius to Fahrenheit
c = float(input(“Enter the degree celcius value: “))
f = (9*c/5) + 32
print(“Equivalent temperature in Fahrenheit = “,f)

## Program to find area and perimeter of a rectangle
l = int(input(“Enter length of the rectangle: “))
b = int(input(“Enter breadth of the rectangle: “))
a = l * b
p = 2*(l+b)
print(f”Rectangle with length {l} and breadth {b} will have area as {a} and perimeter as {p})
”’
ASSIGNMENT:
1. WAP to convert cm to inches
2. WAP to calculate area and circumference of a circle
3. WAP to calculate average of three given values
4. Input the maximum and minimum temperature recorded in a day and calculate
the average temperature for the day.
5. Convert the distance entered in metres to kilometres.
6. Write a program in C to interchange two integer numbers a and b.
7. WAP to input a,b,c and calculate z as a*b-c
8. WAP to input a two digit number and then interchange the values at 1s and 10s place:
e.g. 65 -> 56 , 83 -> 38

”’

## DAY 3: Operators in Python and Basic Programming

# Conditions
# passed or failed based on the avg marks
print(“Pass”)
print(“Fail”)
avg = 55
if avg >=50: # if executes only if the condition is True
print(“Pass”)
print(“Congratulations”)
else: # above condition is False then else
print(“Fail”)
print(“Second option”)
”’
avg >= 80: Grade A: IF
avg <50 (else): Grade E : ELSE

avg > 90: print(“Awesome”)
”’
avg =95
if avg>=80:
print(“Chocolate”)
if avg >= 90:
print(“Awesome”)
elif avg>=70:
print(“Chocolate”)
elif avg >=60:
elif avg >=50:
else:

avg = 85
# if 70 <= avg
if avg>=70:
if 70 < avg:
if avg >=90:
print(“Awesome”)
else:
print(“Chocolate”)
elif avg>=60:
elif avg >=50:
else:

a,b,c = 40,40,40
if a>=b and a>=c:
print(“A is greatest”)
if b>=c and b>=a:
print(“B is greatest”)
if c>=a and c>=b:
print(“C is greatest”)

## DAY 4: Conditions in Python

# WAP to input a number and check if its Odd or even

num = int(input(“Enter a number: “))

if num<0:
print(“Its negative”)
elif num>0:
print(“Its positive”)
else:
print(“Its neither positive nor negative”)

if num %2==0:
print(“Its even number”)
else:
print(“Its odd”)
print(” ============= “)
if num<0:
print(“Its negative”)
else:
if num==0:
print(“Its neither positive nor negative”)
print(“Its even number”)
else:
print(“Its positive”)
if num%2==0:
print(“Its even number”)
else:
print(“Its odd”)

”’
Loops: Repeating a certain block of code multiple times
1. You know how many times to repeat : FOR loop
2. When you have a condition to repeat: WHILE loop

range(=start, <stop, =step)
range(5,17,4): 5, 9, 13
range(=start, <stop) – default step is 1
range(4,9) : 4, 5,6,7,8
range(<stop) : start = 0, step = 1
range(5): 0, 1,2, 3, 4
”’
for i in range(5,17,4):
print(“HELLO”)
print(“Value of i:”,i)

for i in range(4,9):
print(“HELLO AGAIN!”)
print(“Value of i:”,i)

for i in range(5):
print(“How are you?”)
print(“Value of i:”,i)

## While: should have condition and loop repeats only when the condition is true
cont = “yes”
i=0
while cont==“yes”:
print(“HELLO : “,i)
i+=5 # i = i + 5
if i>50:
cont = “No”

## DAY 5: Nested Condition and Loops Introduction

for i in range(1,11):
print(i,end=” : “)
print()
a,b,c,d,e = 50,60,50,60,70
total = a + b+c+d+e
total2 =0
for i in range(5):
val = int(input(“Enter the number: “))
total2 +=val # total2 = total2 + val

print(“Total sum of all the values from the loop = “,total2)

# * * * * *
for i in range(5):
print(“*”,end=” “)
print()
”’
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
”’
for j in range(5):
for i in range(5):
print(“*”, end=” “)
print()
”’
*
* *
* * *
* * * *
* * * * *
”’
for j in range(5):
for i in range(j+1):
print(“*”, end=” “)
print()

## DAY 6: Practice on Loops - 1  ## For loop
”’
*
* *
* * *
* * * *
* * * * *
”’
for i in range(5):
for j in range(i+1):
print(“*”,end=” “)
print()

”’
* * * * *
* * * *
* * *
* *
*
”’
for i in range(5):
for j in range(5-i):
print(“*”,end=” “)
print()

”’
* * * * *
* * * *
* * *
* *
*
”’
for i in range(5):
for j in range(i):
print(” “,end=“”)
for j in range(5-i):
print(“*”,end=” “)
print()

”’
Assignment:
*
* *
* * *
* * * *
* * * * *
”’
”’
Option 1: implementing While using a condition
”’
cont =“Y”
while cont==“Y”:
num1 = int(input(“Enter a number: “))
num2 = int(input(“Enter a number: “))
print(“2. Subtraction”)
print(“3. Multiplication”)
print(“4. Division”)
print(“99. Exit”)
if ch==“1”:
elif ch==“2”:
print(“Subtraction = “, num1 – num2)
elif ch==“3”:
print(“Multiplication = “, num1 * num2)
elif ch==“4”:
print(“Division = “, num1 / num2)
elif ch==“99”:
#pass
cont=“N”
else:
print(“Invalid Input”)
print(“option1: going for one more iteration…”)

”’
Option 2: implementing While using True
”’
print(“Running option 2:”)
while True:
num1 = int(input(“Enter a number: “))
num2 = int(input(“Enter a number: “))
print(“2. Subtraction”)
print(“3. Multiplication”)
print(“4. Division”)
print(“99. Exit”)
if ch==“1”:
elif ch==“2”:
print(“Subtraction = “, num1 – num2)
elif ch==“3”:
print(“Multiplication = “, num1 * num2)
elif ch==“4”:
print(“Division = “, num1 / num2)
elif ch==“99”:
break # it breaks the loop
else:
print(“Invalid Input”)
print(“option2: going for one more iteration…”)

”’
Assignments:
1. Generate Prime numbers between given values
2. WAP to calculate average and grade for the given number of students
3. Calculate area for given choice of shape till user wants
4. Generate fibonacci series numbers till user says so
”’

## Below programs to be done later  num= 5
if num<0:
print(“Positive”)
elif num>0:
pass
else:
pass

import random
number = random.randint(1,100)
count = 0
while True:
guess=int(input(“Guess the number (1-100):”))
count+=1
if guess <1 or guess>100:
print(“Invalid guess, try again”)
continue
elif number==guess:
print(f”You have correctly guessed the number in {count} attempts”)
break
elif number < guess:
print(“You have guessed a higher number. Try again…”)
else:
print(“You have guessed a lower number. Try again…”)

print(“Hello”)
print(“How are you?”)
print(“Hello”, end=“. “)
print(“How are you?”)

###############
# String
var1 = “HELLO”
var2 = ‘how are you there?’
# triple quotes can have multiline of text
var3 = ”’I am fine”’
var4 = “””I am here”””
print(var1,var2,var3,var4)
print(type(var1),type(var2),type(var3),type(var4))
var3 = ”’I am fine
I am doing well
I am alright”’
var4 = “””I am here
I am there
I am everywhere”””
print(var3)
print(var4)

# len() – gives you number of characters in a string
print(len(“hello”))
v1 = “hello”
print(len(v1))

## DAY 8: Loops Practice Programs and String start

#string
var1 = “HELLO”
var2 = “THERE”
# operations
print(var1 + var2)
print((var1+” “) * 5)

for i in var1:
print(i)

print(” indexing – [] , position starts from 0″)
var1 = “HELLO”
print(var1)
print(var1)
#backward index – starts from minus 1
print(var1[-1], var1[-5])

print(var1[len(var1)-1], var1[-1])

# strings are immutable – you cant edit/ you can replace
var1 = “hELLO there”
print(var1)
#var1 = ‘H’ TypeError: ‘str’ object does not support item assignment

# : to read continuous set of values
# need 2 to 4 element
print(var1[1:4])
print(“First 3 elements: “, var1[0:3],var1[:3], var1[-5:-2], var1[-len(var1):-len(var1)+3])
print(“Last 3 elements: “,var1[-3:])
print(“Content: “,var1, var1[:])