0% found this document useful (0 votes)
3 views4 pages

Python basics learning programs

Basic programs in python

Uploaded by

Ramashish
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)
3 views4 pages

Python basics learning programs

Basic programs in python

Uploaded by

Ramashish
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/ 4

# Program 1: Hello World!

# This program simply prints "Hello, World!" to the console.

print("Hello, World!")

# ---

# Program 2: Basic Arithmetic Operations

# This program demonstrates addition, subtraction, multiplication, and division.

num1 = 10

num2 = 5

print(f"Addition: {num1 + num2}") # Output: Addition: 15

print(f"Subtraction: {num1 - num2}") # Output: Subtraction: 5

print(f"Multiplication: {num1 * num2}") # Output: Multiplication: 50

print(f"Division: {num1 / num2}") # Output: Division: 2.0

# ---

# Program 3: Get User Input and Print It

# This program asks the user for their name and then greets them.

name = input("Enter your name: ")

print(f"Hello, {name}!")

# ---

# Program 4: Calculate Area of a Rectangle

# This program takes length and width as input and calculates the area.

# Note: input() returns a string, so we convert it to a float.

length = float(input("Enter the length of the rectangle: "))

width = float(input("Enter the width of the rectangle: "))

area = length * width


print(f"The area of the rectangle is: {area}")

# ---

# Program 5: Check if a Number is Even or Odd

# This program uses an if-else statement to check divisibility by 2.

number = int(input("Enter an integer: "))

if number % 2 == 0:

print(f"{number} is an even number.")

else:

print(f"{number} is an odd number.")

# ---

# Program 6: Sum of Numbers in a List

# This program uses a loop to iterate through a list and sum its elements.

numbers_list = [10, 20, 30, 40, 50]

total_sum = 0

for num in numbers_list:

total_sum += num

print(f"The sum of the numbers in the list {numbers_list} is: {total_sum}")

# ---

# Program 7: Find the Largest Number in a List

# This program iterates through a list to find the maximum value.

numbers = [34, 12, 56, 89, 7, 23]

largest_number = numbers[0] # Assume the first element is the largest initially

for num in numbers:

if num > largest_number:


largest_number = num

print(f"The largest number in {numbers} is: {largest_number}")

# ---

# Program 8: Simple Countdown with a While Loop

# This program demonstrates a while loop for a countdown.

count = 5

print("Starting countdown:")

while count > 0:

print(count)

count -= 1 # Decrement count by 1

print("Blast off!")

# ---

# Program 9: Create a Simple Function to Square a Number

# This program defines a function that takes a number and returns its square.

def square(num):

return num * num

input_num = 7

result = square(input_num)

print(f"The square of {input_num} is: {result}")

# ---

# Program 10: Reverse a String

# This program takes a string and prints its reversed version.

original_string = input("Enter a string to reverse: ")

reversed_string = original_string[::-1] # Python slicing trick for reversing a string


print(f"The reversed string is: {reversed_string}")

You might also like