Python programs
1. Variables and Data Types
Program 1: Using Integer, Float, and String
python
# Define variables
int_var = 10 # Integer
float_var = 25.75 # Float
str_var = "Python Programming" # String
# Print values
print("Integer:", int_var)
print("Float:", float_var)
print("String:", str_var)
Output:
Integer: 10
Float: 25.75
String: Python Programming
Program 2: Checking Variable Types
python
# Define variables
a = 15
b = 10.5
c = "Hello"
# Print types
print("Type of a:", type(a))
print("Type of b:", type(b))
print("Type of c:", type(c))
Output:
Type of a: <class 'int'>
Type of b: <class 'float'>
Type of c: <class 'str'>
2. If Statement
Program 3: Check Positive Number
python
num = 7
if num > 0:
print("The number is positive.")
Output:
The number is positive.
Program 4: Check Even Number
python
num = 4
if num % 2 == 0:
print("The number is even.")
Output:
The number is even.
3. If-Else Statement
Program 5: Odd or Even Check
python
num = 9
if num % 2 == 0:
print("Even Number")
else:
print("Odd Number")
Output:
Odd Number
Program 6: Check Positive or Negative
python
num = -5
if num >= 0:
print("Positive number")
else:
print("Negative number")
Output:
Negative number
4. If-Elif-Else Statement
Program 7: Grading System
python
marks = 85
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
elif marks >= 60:
print("Grade: C")
else:
print("Grade: D")
Output:
Grade: B
Program 8: Compare Three Numbers
python
a, b, c = 10, 20, 15
if a > b and a > c:
print("a is the largest")
elif b > a and b > c:
print("b is the largest")
else:
print("c is the largest")
Output:
b is the largest
5. For Loop
Program 9: Print Numbers 1 to 5
python
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
Program 10: Print Even Numbers
python
for i in range(2, 11, 2):
print(i)
Output:
2
4
6
8
10
6. While Loop
Program 11: Print Numbers 1 to 5
python
i = 1
while i <= 5:
print(i)
i += 1
Output:
1
2
3
4
5
Program 12: Sum of First 5 Numbers
python
i, total = 1, 0
while i <= 5:
total += i
i += 1
print("Sum:", total)
Output:
Sum: 15
Program 13: Multiplication Table
This program prints the multiplication table for a given number.
python
num = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
Example Output:
Enter a number: 5
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50
Program 14: Pattern Printing
This program prints a simple triangle pattern.
python
rows = 5
for i in range(1, rows+1):
print("* " * i)
Output:
*
* *
* * *
* * * *
* * * * *
Program 15: Iterative Factorial
This program calculates the factorial of a number using iteration.
python
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial *= i
print("Factorial of", num, "is", factorial)
Example Output:
Enter a number: 5
Factorial of 5 is 120
************************************************************************