0% found this document useful (0 votes)
2 views16 pages

Python Programming

Uploaded by

roshanprithik71
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)
2 views16 pages

Python Programming

Uploaded by

roshanprithik71
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

PYTHON PROGRAMMING

Single variables Variables:

Program:

A=4
print(A)

OUTPUT:
4

Program:

A=”name”
print(A)

OUTPUT:
name

Multiple variables for multiple values:

Program:

x,y,z=”orange,apple,banana”
print(x)
print(y)
print(z)

OUTPUT:
Orange
Apple
banana

Multiple variables for single values:

Program:

x=y=z=1
print(x)
print(y)
print(z)

OUTPUT:
1
1
1
DATATYPE
 Integer
 Float
 String
 Boolean
 List
 Tuple
 Set
 Dictionary

1. Integer (int)

Program:

a = 10
b=5
print("Sum:", a + b)

Output:
Sum: 15

2. Float (float)

Program:

x = 5.5
y = 2.0
print("Division:", x / y)

Output:
Division: 2.75

3. String (str)

Program:

name = "Alice"
print("Hello " + name)

Output:
Hello Alice

4. Boolean (bool)

Program:

is_python_easy = True
print(is_python_easy)

Output:
True
5. List (list)

Program:

fruits = ["apple", "banana", "cherry"]


print(fruits[1])

Output:
banana

6. Tuple (tuple)

Program:

numbers = (1, 2, 3)
print(numbers[0])

Output:
1

7. Set (set)

Program:

unique_numbers = {1, 2, 2, 3}
print(unique_numbers)

Output:
{1, 2, 3}

8. Dictionary (dict)

Program:

student = {"name": "John", "age": 20}


print(student["name"])

Output:
John

9. None Type (NoneType)

Program:

value = None
print(value)

Output:
None
TYPES OF OPERATORS
1. Arithmatic operators
2. Assignment operators
3. Relational operators
4. logical operators
5. Bitwise operators
6. Membership operators
7. Identity operators

1. Arithmetic Operators

Program:

a = 10
b=3
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Modulus:", a % b)
print("Exponent:", a ** b)
print("Floor Division:", a // b)

Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Modulus: 1
Exponent: 1000
Floor Division: 3

2. Comparison (Relational) Operators

Program:

x=5
y = 10
print(x == y)
print(x != y)
print(x > y)
print(x < y)
print(x >= y)
print(x <= y)

Output:
False
True
False
True
False
True
3. Logical Operators

Program:

a = True
b = False
print("AND:", a and b)
print("OR:", a or b)
print("NOT:", not a)

Output:
AND: False
OR: True
NOT: False

4. Assignment Operators

Program:

n=5
n += 3
print("+= :", n)
n -= 2
print("-= :", n)
n *= 2
print("*= :", n)
n /= 3
print("/= :", n)

Output:
+= : 8
-= : 6
*= : 12
/= : 4.0

5. Bitwise Operators

Program:
a = 5 # 0101
b = 3 # 0011
print("AND:", a & b)
print("OR:", a | b)
print("XOR:", a ^ b)
print("NOT:", ~a)
print("Left Shift:", a << 1)
print("Right Shift:", a >> 1)

Output:
AND: 1
OR: 7
XOR: 6
NOT: -6
Left Shift: 10
Right Shift: 2
6. Membership Operators

Program:

nums = [1, 2, 3, 4]
print(2 in nums)
print(5 not in nums)

Output:
True
True

7. Identity Operators

Program:

a = [1, 2]
b=a
c = [1, 2]
print(a is b)
print(a is c)
print(a is not c)

Output:
True
False
True

CODITIONAL STATEMENT
🔹 If Statement
🔹 If-Else Statement
🔹 If-Elif-Else Statement
🔹 Nested If Statement
🔹 Short-Hand If (One-Line If)
🔹 Short-Hand If-Else (Ternary Operator)
🔹 Multiple Conditions Using Logical Operators

1. if Statement

Program:

age = 18
if age >= 18:
print("You are eligible to vote")

Output:
You are eligible to vote

2. if-else Statement
Program:

num = 7
if num % 2 == 0:
print("Even number")
else:
print("Odd number")

Output:
Odd number

3. if-elif-else Statement

Program:

marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 70:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Fail")

Output:
Grade B

4. Nested if Statement

Program:

num = 10
if num > 0:
if num % 2 == 0:
print("Positive Even number")

Output:
Positive Even number

5. Short-hand if (One-line if)

Program:

a=5
b=3
if a > b: print("a is greater")

Output:
a is greater

6. Short-hand if-else (Ternary Operator)


Program:

a = 10
b = 20
print("a is greater") if a > b else print("b is greater")

Output:
b is greater

7. Multiple Conditions using Logical Operators

Program:

age = 25
has_id = True
if age >= 18 and has_id:
print("Allowed entry")

Output:
Allowed entry

LOOPING STATEMENT
🔹 For Loop
🔹 While Loop
🔹 While Loop With Break
🔹 While Loop With Continue
🔹 Nested Loop
🔹 Loop With Else

1. for Loop

Program:

for i in range(1, 6):


print(i)

Output:
1
2
3
4
5

2. while Loop

Program:

i=1
while i <= 5:
print(i)
i += 1

Output:
1
2
3
4
5

3. for Loop with break

Program:

for i in range(1, 6):


if i == 3:
break
print(i)

Output:
1
2

[Link] Loop with continue:

Program:

for i in range(1, 6):


if i == 3:
continue
print(i)

Output:
1
2
4
5

5. while Loop with break

Program:

i=1
while True:
if i == 4:
break
print(i)
i += 1

Output:
1
2
3

6. while Loop with continue


Program:

i=0
while i < 5:
i += 1
if i == 3:
continue
print(i)

Output:
1
2
4
5

7. Nested Loop

Program:

for i in range(1, 4):


for j in range(1, 3):
print(i, j)

Output:
11
12
21
22
31
32

8. Loop with else

Program:

for i in range(3):
print(i)
else:
print("Loop finished")

Output:
0
1
2
Loop finished

JUMPING STATEMENTS
🔹 Break Statement
🔹 Continue Statement
🔹 Pass Statement

1. break Statement

Stops the loop immediately.

Program:

for i in range(1, 6):


if i == 4:
break
print(i)

Output:
1
2
3

2. continue Statement

Program:

for i in range(1, 6):


if i == 3:
continue
print(i)

Output:
1
2
4
5

3. pass Statement

Program:

for i in range(1, 4):


if i == 2:
pass
print(i)

Output:
1
2
3

SWITCH STATEMENT
1. Using if-elif-else (Switch Alternative)

Program:

day = 2
if day == 1:
print("Monday")
elif day == 2:
print("Tuesday")
elif day == 3:
print("Wednesday")
else:
print("Invalid day")

Output:
Tuesday

2. Using Dictionary (Switch Alternative)

Program:

def switch(day):
return {
1: "Monday",
2: "Tuesday",
3: "Wednesday"
}.get(day, "Invalid day")
print(switch(3))

Output:
Wednesday

3. Using match-case (Python 3.10+)

Program:

day = 1
match day:
case 1:
print("Monday")
case 2:
print("Tuesday")
case _:
print("Invalid day")

Output:
Monday
1. Odd or Even
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even Number")
else:
print("Odd Number")

2. Palindrome Number
num = int(input("Enter a number: "))
temp = num
rev = 0

while num > 0:


digit = num % 10
rev = rev * 10 + digit
num = num // 10

if temp == rev:
print("Palindrome")
else:
print("Not Palindrome")

Palindrome String:
text = input("Enter a string: ")
rev = ""

i = len(text) - 1

while i >= 0:
rev = rev + text[i]
i -= 1

if text == rev:
print("Palindrome string")
else:
print("Not a palindrome string")

COMBINATION OF NUMBERS AND STRING IN PALINDROME:

choice = int(input("Enter 1 for Number or 2 for String: "))

# Number Palindrome

if choice == 1:
num = int(input("Enter a number: "))
temp = num
rev = 0
while num > 0:
digit = num % 10
rev = rev * 10 + digit
num = num // 10
if temp == rev:
print("Palindrome")
else:
print("Not a palindrome")
# String Palindrome

elif choice == 2:
text = input("Enter a Word: ")
rev = ""
i = len(text) - 1
while i >= 0:
rev = rev + text[i]
i -= 1
if text == rev:
print("Palindrome")
else:
print("Not a palindrome")
# Invalid Choice
else:
print("Invalid choice")

3. Fibonacci Series
n = int(input("Enter number of terms: "))

a=0
b=1
count = 0

while count < n:


print(a, end=" ")
c=a+b
a=b
b=c
count += 1

4. Armstrong Number
num = int(input("Enter a number: "))
temp = num
sum = 0

while num > 0:


digit = num % 10
sum = sum + digit ** 3
num = num // 10

if temp == sum:
print("Armstrong Number")
else:
print("Not Armstrong Number")

5. Factorial of a Number

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


fact = 1

i=1
while i <= num:
fact = fact * i
i += 1

print("Factorial:", fact)

6. Largest and Smallest (3 Numbers)

a = int(input("Enter first number: "))


b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

largest = a
smallest = a

if b > largest:
largest = b
if c > largest:
largest = c

if b < smallest:
smallest = b
if c < smallest:
smallest = c

print("Largest:", largest)
print("Smallest:", smallest)

7. Ascending Order (3 Numbers)


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a > b:
a, b = b, a
if a > c:
a, c = c, a
if b > c:
b, c = c, b

print("Ascending Order:", a, b, c)

8. Descending Order (3 Numbers)


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a < b:
a, b = b, a
if a < c:
a, c = c, a
if b < c:
b, c = c, b

print("Descending Order:", a, b, c)
BUS TICKET CODE
PROGRAM:

print("-----Bus Ticket------")
stops=int(input("enter number of stops: "))
base_fare=10
price_per_stop=2
number_of_person=int(input("How many members: "))
subtotal=base_fare+(stops*price_per_stop)*number_of_person
grandtotal=subtotal*number_of_person
print("\n Passanger Categories: ")
print("[Link]")
print("[Link]")
print("[Link] Citizen")
category=int(input("Select category(1-3): "))

if category==1:
discount=0
finial_bill = subtotal
type_lable="Adult"
else:
if category==2:
discount= subtotal*0.20
finial_bill = subtotal - discount
type_lable="Student"
else:
if category==3:
discount= subtotal*0.50
finial_bill = subtotal - discount
type_lable="Senior Citizen"
else:
discount =0
finial_bill = subtotal
type_lable="Standard (Invalid number)"
print ("\n"+"*"*25)
print(" Final Bill")
print("*"*25)
print(f"Passenger: {type_lable}")
print(f"Stops: {stops}")
print(f"Subtotal: Rs.{subtotal: .2f}")
print(f"Discount: Rs. {discount: .2f}")
print("-"*25)
print(f"Total: Rs.{finial_bill: .2f}")
print("*"*25)

You might also like