0% found this document useful (0 votes)
6 views14 pages

Python Programs

Uploaded by

MB Raja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views14 pages

Python Programs

Uploaded by

MB Raja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Python Programs

1. Print the statements: use single quote or double quote to print the statements.
print('Hello, World!')
print("Learn Python Code!")
Output:
Hello, World!
Learn Python Code!
2. By default statement end the line.
We should add the end
print("Hello, World!",end=" ")
print("Learn Python Code!")
Output:
Hello, World! Learn Python Code!
3. Deal with variables and values
Values are
int – 10
float - 10.5
complex = 5+10j
string - str1 = "Hi" str2 = 'Hi'
list – a = [ ], a = [10,20,50.25,"Hello"]
Tuple t = ( ) , t = ("Hello","user")
Boolean – True / False
Set - set1 = ( ) , set2 = ("Welcome")
Dictionary - d1 = {1:"One",2:"two"} , d2 = {"ID": 1,"Name":"Clara"}

a = 10
b = 20
print(a+b)
print("Addition = ",a+b)
c=a+b
print("sum = ",c)
print(f"sum of {a} and {b} is {c}")
print(f"sum of {a} and {b} is",c)

Output:
a = 10
b = 20
print(a+b)
print("Addition = ",a+b)
c=a+b
print("sum = ",c)
print(f"sum of {a} and {b} is {c}")
print(f"sum of {a} and {b} is",c)
print("a = %d , b =%d , c=%d " %(a,b,c))
print("Sum of %d and %d is %d" %(a,b,c))

Output:
30
Addition = 30
sum = 30
sum of 10 and 20 is 30
sum of 10 and 20 is 30
a = 10 , b =20 , c=30
Sum of 10 and 20 is 30

4. Multiple declaration in single line


a,b = 10,20
print(a,b)
print(a,b,sep="...")
Output:
10 20
10...20
a,b,c = 1,10.5,"Hello"
print(a,b,c)
Ouput:
1 10.5 Hello

5. Assigning same value to multiple variable


a = b = 10
print(a,b)

Output:
10 10

6. type ( ) : This function is used to find the data type of the variable
a = 10
b = 20.5
c = "Hello"
d = True
e = 6 +7j
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))

Output:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'complex'>
7.Type Conversion: The process of converting one data type to another data
type.
2 types
1. Implicit type conversion: Conversion done automatically by interpreter
during execution of program.
Ex:
a = 10
b = 20.5
c=a+b
print (c)
print (type(c))
Output:
30.5
<class 'float'>
2. Explicit type conversion: Conversion done by the user, also know as type
casting.
Ex 1:
a = "10"
b = 20.5
c = float(a) + b
print (c)

print (type(a))
Output:
30.5
<class 'str'>
Ex 2:
a = 10
b = 20.5
c = a + int(b) # loss of data
print(c)
print(type(c))
Ex:
30
<class 'int'>

8. input( ) – It is used to read values at run time.


Ex 1:
a = input("Enter a: ")
b = input("Enter b: ")
c = input("Enter c: ")
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
By default, input( ) function takes string value.
Output:
Enter a: 10
Enter b: 20
Enter c: Hello
10
20
Hello
<class 'str'>
<class 'str'>
<class 'str'>

Ex: 2
a = input("Enter a number: ")
a = int(a)
print(a)
print(type(a))
Output:
Enter a number: 10
10
<class 'int'>
Ex 3:
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c=a+b
print("Sum = ",c)

Output:
Enter a:10
Enter b:20
Sum = 30

Ex 4: Area of circle
radius = float(input("Enter the radius of the circle: "))
area = 3.14* radius * radius
print("The area of the circle is: ", area)

area = (22/7)*radius*radius
print("The area of the circle is: ", area)
print("pi = ",22/7)
Output:
Enter the radius of the circle: 4.5
The area of the circle is: 63.585
The area of the circle is: 63.64285714285714
pi = 3.142857142857143
9. Operators:
1.Arithmetic Operator:
+ , - , * , / , % , **, //
2. Relational Operator:
== , != , > , < , >= , <= ,
3. Logical Operator:
and , or , not
4. Assignment Operator:
= , += , -= , *= , /= , %= , **=, //=
5. Bitwise Operator:
& , | , ~ , ^ , >> , <<
6. Ternary Operator:
Synatx: var_name = "True" if (condition) else "False"

7. Membership Operator:
in and not in
8. Identity Operator:
is and is not
Ex 1:
a = 10
b=2
c = 15
d=8
print(a**b)
print(c//d)

b1 = (a>b) and (c>d)


print(b1)
b2 = (b>a) or (b<d)
print(b2)
b3 = not(d>b)
print(b3)
a **= b # a = a**b
print(a)
c //= d # c = c//d
print(c)

res = "Even" if(a % 2 == 0) else "Odd"


print(res)

Output:
100
1
True
True
False
100
1
Odd

Ex 2: membership operator
str1 = "Hello"
a = ['apple','banana','cherry']
print("o" in str1)
print('cherry' in a)
print('H' not in str1)
print('Kiwi' not in a)

Output:
True
True
False
True
Ex 3: Identity Operator
a = 10
b = 10
l1 = [10,20,30]
l2 = [10,20,30]
l3 = l1

print(a is b)
print(b is not a)
print(l1 is l3) # checks memory object reference
print(l1 is not l2)

str1 = "Hello"
str2 = "Hello"
print(str1 is str2)
print(id(l1),id(l2),id(l3))
print(id(a),id(b),id(str1),id(str2))

Output:
True
False
True
True
True
2687817000064 2687816851712 2687817000064
140730757932232 140730757932232 2687855274736 2687855274736

10.Flow Control: Flow of control refers to the order in which the statements in a program are
executed. By default, Python executes statements sequentially from top to bottom. However,
control flow statements allow for altering this default order, enabling decision-making,
repetition, and handling of various program states.
The Primary Types of control flow are
a) Selection / Conditional statements
b) Repetition / Looping / iterative statements and
c) Jumping statements

1. a) Selection statements:
Selection statements also known as decision-making or branching statements, are used to
control the flow of a program based on certain conditions. These statements allow the
program to execute different blocks of code depending on whether a condition is true or false.
Python provides several types of selection statements,
1. Simple if
2. if…else
3.Nested if
4. if…elif
1. Simple if
Ex:
a = 10
if(a>5):
print("a is greater than 5")
print("Program Completed!")

Output:
a is greater than 5
Program Completed!

2. if…else
Ex:
x=2
if (x > 5):
print("x is greater than 5")
else:
print("x is less than 5")
Output:
x is less than 5

3. Nested if:
Ex:
age=int(input("Enter your age:"));
if age>=18:
if age>=60:
print("Eligible to vote and senior citizen")
else:
print("Eligible to vote but not senior citizen")
else:
print("Not eligible to vote")
Output:
Enter your age:21
Eligible to vote but not senior citizen
4.if…elif: short for "else if"
Ex:
a=10
b=10
if(a>b):
print("a is greater!")
elif(b>a):
print("b is greater!")
else:
print("a and b are equal!")
Output:
a and b are equal!

b) Repetitive / Looping:
1. While loop:
n = int(input("Enter n value:"))
i=1
while(i<=n):
print(i)
i=i+1
Output:
Enter n value:5
1
2
3
4
5
2. For loop:
range ( ) function:
range ( ) is a built-in function, used to create a list containing a sequence of
integers from the give start value up to stop value (excluding stop value).The parameters are
start, stop and step.

 The start and step parameters are optional.


 If the start value is not specified, by default starts from 0.
 If step is also not specified, by default the value increases by 1 in each iteration.
 All parameters of range( ) function must be integers.
 The step parameter can be positive or a negative integer excluding(except) 0.

Ex 1:
n = int(input("Enter n value:"))
for i in range(n):
print(i)
Output:
Enter n value:5
1
2
3
Ex 2:
n = int(input("Enter n value:"))
for i in range(1,n+1):
print(i)
Output:
Enter n value:5
1
2
3
4
5

Ex 3:

n = int(input("Enter n value:"))
for i in range(1,n+1,3):
print(i)

Output:
Enter n value:10
1
4
7
10
c) Jumping statement
1. break
2. continue
3. return
1. break:
for i in range(5):
if i == 3:
break
print(i)
Output:
0
1
2

2. continue:
Ex:
for i in range(5):
if i == 2:
continue
print(i)

Output:
0
1
3
4

3. return
Ex:
def calculate_sum(a, b):
return a + b

result = calculate_sum(5, 7)
print(result)

Output:
12

Nested Loop: Loop inside another loop


Ex:
n=int(input("Enter n to generate patterns: "))
for i in range(1,n+1):
for j in range(1,i+1):
print(j,end=" ")
print(" ")
Output:
Enter n to generate patterns: 5
1
12
123
1234
12345

You might also like