Class - 1
Class - 1
Class Overview
1. Python Installation
2. Writing Comments to your program code
3. Print()
4. Variables
5. Input()
6. Operators
Python Comments
Commenting our code helps explain our thought process, and
help us to understand later on the intention of our code.
Creating a Comment
Parameter Description
object(s) Any object, and as many as you like. Will be converted to string before printed
sep='separator' Optional. Specify how to separate the objects, if there is more than one.
Default is SPACE
end='end' Optional. Specify what to print at the end. Default is NEW LINE
11
Ex1. print("Hello")
print(‘Hello’)
print(‘’’Hello’’’)
Print(“””Hello”””)
Ex2. name= ‘Johny’
print(name)
13
Ex3. name= ‘Johny’
print(“Hello “,name)
print(“Hello”,name, sep=‘***’)
Ex4. print(1, 2, 3, 4,sep=‘X’,end=‘\n\n\n’)
print(1, 2, 3, 4, sep='*')
print(1, 2, 3, 4, sep='#', end=‘THE END')
15
Ex5
x = 100
.
y = 200
print(“x is”, x , ”y is” , y)
print(“x is”, x , ”y is” , y, sep=‘\n’)
print(“x is”, x, ”y is”, y, sep=‘$’, end=‘THE END’)
str.format()
Formatters work by putting in one or more replacement fields and placeholders
defined by a pair of curly braces { } into a string and calling the str.format()
S y n t a x : ‘ S t r i n g h e r e { } t h e n a l s o { } ’. f o r m a t ( ‘ s o m e t h i n g 1 ′ ,’s o m e t h i n g 2 ’ )
a,b=10,20
c=a+b
print('A is {0} B is {1} C is {2}'.format(a,b,c))
Var is a variable.
Var has value 10.
Var can have a different value later.
Var1 = 10
Var2 = 5 + Var1
Var3 = 4 * Var2
Var1 = (Var1 + Var3)
print(Var1, Var2, Var3)
Strings X = 1.12
Integer
Y = True
DataTypes Numbers
Float Z = 100
Boolean S = ‘Hello everyone’
Exercise
What will the following code snippet print?
Var1 = 10
Var2 = 5 + Var1
Var3 = 4 * Var2
Var1 = (Var1 + Var3)/2
print(Var1, Var2, Var3)
Exercise
What will the following code snippet print?
a = 10
b=a*5
c = "Your result is: "
print(c, b)
Exercise
What will the following code snippet print?
a = 10
b=a
a=3
print(b)
Any guesses?
Exercise
What will the following code snippet print?
glass_of_water=
print("I drank", glass_of_water, "glasses of water today.")
27
input() function
✓ Python input() function is used to take user input.
✓ input() returns a string object.
✓ Even if the input value is an integer/float, input() takes it as a string.
28
Input
Sometimes, we’d like the user to participate! We can
also get input from the user.
value = input("Please enter a number ")
print(value)
print("The name of the user is {0} and his/her age is {1}".format(name, age))
• Arithmetic Operators
• Comparison Operators
• Assignment Operators
• Bitwise Operators
• Logical Operators
• Conditional (or ternary) Operators
31
Python Arithmetic Operators:
Assume a=10 b=20
Operator Description Example
+ Addition - Adds values on either side of the operator a + b will give 30
* Multiplication - Multiplies values on either side of the operator a * b will give 200
/ Division - Divides left hand operand by right hand operand b / a will give 2.0
Floor Division - The division of operands where the result is 9//2 is equal to 4
// the quotient in which the digits after the decimal point are and
removed. 9.0//2.0 is equal to 4.0
32
Python Comparison Operators:
Operator Description Example
Checks if the value of two operands are equal or not,
== if yes then condition becomes True. (a == b) is False.
33
Python Assignment Operators:
Operator Description Example
c = a + b will assign
= Assigns values from right side operands to left side operand value of a + b into c
It adds right operand to the left operand and assign the result c += a is equivalent to
+= to left operand c=c+a
It subtracts right operand from the left operand and assign the c -= a is equivalent to
-= result to left operand c=c-a
It multiplies right operand with the left operand and assign the c *= a is equivalent to
*= result to left operand c=c*a
It divides left operand with the right operand and assign the c /= a is equivalent to
/= result to left operand c=c/a
It takes modulus using two operands and assign the result to c %= a is equivalent to
%= left operand c=c%a
Performs exponential (power) calculation on operators and c **= a is equivalent to
**= assign value to the left operand c = c ** a
Performs floor division on operators and assign value to the c //= a is equivalent to
//= left operand c = c // a
34
Python Logical Operators:
a=True b=True
Operator Description Example
36
Python Operators Precedence
Operator Description
** Exponentiation (raise to the power)
Complement, unary plus and minus
~+- (method names for the last two are +@ and -@)
* / % // Multiply, divide, modulo and floor division
+- Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'
^| Bitwise exclusive `OR' and regular `OR'
<= < > >= Comparison operators
<> == != Equality operators
Sample Input 1:
1200
Sample Output1:
12
Sample Input 2:
242
Sample Output2:
7 45
Lucky Gifts
Krishna is Chocolate lover.
In an event, he was asked to distribute chocolates to few lucky attendees.
5
There are 'N' chocolates available. He need to decide how many Chocolates (P) to place in each pack. Each
pack must contain the same number of chocolates. Place exactly P chocolates into each pack. He should
make as many packs as possible.
Write a program that will calculate the pack size P so that he can eat as many chocolates as possible.
If multiple pack size will result in the same number of leftover candies, then print the largest pack size.
Number of hours Employee-X has worked in the weekdays is 10 more than the number of
hours he had worked during weekends. If the total salary paid to him in this month is
known, write a program to estimate the number of hours he had worked during weekdays
and the number of hours he had worked during weekends.
Given the total number of people who have attended the event in the first 3 days, find the
number of people who have attended the event on day 1, day 2 and day 3.