Python Module-1
Python Module-1
S Savitha
Asst. Professor
WHAT IS PROGRAMMING
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different variables)
If you want to specify the data type of a variable, this can be done with casting.
x = y = z = "Orange"
print(x)
print(y)
print(z)
EXAMPLE
x = 5
y = "John"
print(type(x))
print(type(y))
<class 'int'>
<class 'str'>
x = 5
y = "John"
print(x)
print(y)
<class 'int'>
<class 'str'>
Blocks of code
Lines of Python code can be grouped together in blocks. You can tell when a block
begins and ends from the indentation of the lines of code.
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
Syntax error:
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
Python Comments
Float division: The quotient returned by this operator is always a float number, no
matter if two numbers are integers. For example:
Example: The code performs division operations and prints the results. It
demonstrates that both integer and floating-point divisions return accurate results.
print(5/5) #1.0
print(10/2)#5.0
print(-10/2)#-5.0
print(20.0/2)#10.0
Division operator
Floor division/ Integer division: The quotient returned by this operator is dependent
on the argument being passed. If any of the numbers is float, it returns output in
float.
It is also known as Floor division because, if any number is negative, then the
output will be floored.
print(10//3)#3
print(10.0//3)#3.0
print (-5//2)#-3
print (5.0//2)#2.0
print (-5.0//2)#-3.0
Assignment operator
Comparator operator
Logical operator
Membership operator
Bit operator
Order of Operations
The Python operators follow the precedence rule (which can be remembered as
PEMDAS)
next priority: Out of these two operations, whichever comes first in the expression
is evaluated.
function input()
Python uses the built-in function input() to read the data from the keyboard. When
this function is invoked, the user-input is expected.
For example:
>>> str1=input()
One can use new-line character \n in the function input() to make the cursor to
appear in the next line of prompt message
float()
A function float() is used to convert a valid value enclosed within quotes into float
number as shown below -
>>> f= input("Enter a float value:")
Enter a float value: 3.5
>>> type(f)
<class 'str'> #f is actually a string “3.5”
>>> f= float(f) #converting “3.5” into float value 3.5
>>> type(f)
<class 'float'>
chr()
Enter an integer:65
>>> ch=chr(a)
Character Equivalent of 65 is A
Flow control statements
● If statements
● Else statements
● Elif statements
● While loop statements
If statements
if condition:
Statement block
>>> x=10
>>> if x<40:
print("Fail")
Fail #output
If-else (Alternative Execution)
x=int(input("Enter x:"))
if x%2==0:
print("x is even")
else:
print("x is odd")
Chained Conditionals
The conditions are checked one by one sequentially. If any condition is satisfied, the
The conditions are checked one by one sequentially. If any condition is satisfied, the
respective statement block will be executed and further conditions are not checked.
Note that, the last else block is not necessary always.
Nested Conditionals
The conditional statements can be nested. That is, one set of conditional statements can
be nested inside the other. It can be done in multiple ways depending on programmer’s
requirements.
Ex: marks=float(input("Enter marks:"))
if marks>=60:
if marks<70:
print("First Class")
else:
print("Distinction")
gender=input("Enter gender:")
age=int(input("Enter age:"))
if gender == "M" :
if age >= 21:
print("Boy, Eligible for Marriage")
else:
print("Boy, Not Eligible for Marriage")