0% found this document useful (0 votes)
0 views

Basic Python Program Examples

The document contains various basic Python program examples, including printing messages, performing arithmetic operations, checking conditions, and working with lists. It demonstrates the use of functions, user input, and control flow statements like if-else. Additionally, it covers topics such as string manipulation, list operations, and determining prime numbers.

Uploaded by

dmz166046
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Basic Python Program Examples

The document contains various basic Python program examples, including printing messages, performing arithmetic operations, checking conditions, and working with lists. It demonstrates the use of functions, user input, and control flow statements like if-else. Additionally, it covers topics such as string manipulation, list operations, and determining prime numbers.

Uploaded by

dmz166046
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Basic Python Program Examples

1. Hello World
print("Hello, world!")

2. Simple Addition
a = 5
b = 3
sum = a + b
print("Sum:", sum)

3. Check Even or Odd


num = 4
if num % 2 == 0:
print(num, "is even")
else:
print(num, "is odd")

4. Find the Largest of Three Numbers


a = 10
b = 20
c = 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")

5. Simple Function to Greet


def greet(name):
print("Hello,", name)

greet("Alice")
Basic Python Programs Using
Functions, print, input, and len
1. Greeting User by Name
def greet_user():
name = input("Enter your name: ")
print("Hello,", name + "!")

greet_user()

2. Calculate Length of a String


def string_length():
text = input("Enter a string: ")
length = len(text)
print("The length of the string is:", length)

string_length()

3. Print a Message Multiple Times


def repeat_message():
message = input("Enter a message: ")
times = int(input("How many times to print? "))
for _ in range(times):
print(message)

repeat_message()

4. Check if Input String is Empty


def check_empty():
text = input("Enter something: ")
if len(text) == 0:
print("You entered an empty string.")
else:
print("You entered:", text)

check_empty()
5. Print User Info
def user_info():
name = input("Enter your name: ")
age = input("Enter your age: ")
print("Name:", name)
print("Age:", age)
print("Name length:", len(name))

user_info()

6. Calculate the age of a person


def calculate_age():
birth_year = int(input("Enter your birth year (e.g., 1990): "))
current_year = 2024
age = current_year - birth_year
print("You are", age, "years old.")

calculate_age()

7. Prime number or not


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

if num <= 1:
print("Not a prime number")
else:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
print("Not a prime number")
break
else:
print("Prime number")

8.Create and Print a List


fruits = ["apple", "banana", "cherry"]
print("Fruits list:", fruits)

9. Add Elements to a List


numbers = [1, 2, 3]
numbers.append(4)
print("Updated list:", numbers)

10. Remove Element from a List


colors = ["red", "green", "blue"]
colors.remove("green")
print("After removal:", colors)

11. Sort a List


nums = [5, 2, 9, 1]
nums.sort()
print("Sorted list:", nums)

12. Find the Largest Number in a List


numbers = [10, 25, 8, 30]
print("Maximum value:", max(numbers))

13. List Comprehension (Square of Numbers)


nums = [1, 2, 3, 4]
squares = [x**2 for x in nums]
print("Squares:", squares)

14. Count Occurrences of an Element


letters = ["a", "b", "a", "c", "a"]
count_a = letters.count("a")
print("Number of 'a':", count_a)

15. Reverse a List


names = ["Alice", "Bob", "Charlie"]
names.reverse()
print("Reversed list:", names)

16. Boolean Values Directly


is_sunny = True
is_raining = False

print("Is it sunny?", is_sunny)


print("Is it raining?", is_raining)

Programs using if else statements

Check if a Number is Positive or Negative

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

if num >= 0:
print("Positive number")
else:
print("Negative number")

Check if a Number is Even or Odd

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

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

Check if Age is Eligible to Vote

age = int(input("Enter your age: "))

if age >= 18:


print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
Check if a Character is a Vowel or Consonant
ch = input("Enter a letter: ").lower()

if ch in 'aeiou':
print("Vowel")
else:
print("Consonant")

You might also like