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

Basic Python OOP Codes

The document provides a comprehensive guide to basic Python programming concepts, including variables, arithmetic operations, arrays (lists), and loops. It includes examples for declaring variables, performing arithmetic calculations, manipulating lists, and using both for and while loops. Additionally, it covers functions for a simple calculator and various operations like finding averages, minimums, maximums, and counting elements in a list.

Uploaded by

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

Basic Python OOP Codes

The document provides a comprehensive guide to basic Python programming concepts, including variables, arithmetic operations, arrays (lists), and loops. It includes examples for declaring variables, performing arithmetic calculations, manipulating lists, and using both for and while loops. Additionally, it covers functions for a simple calculator and various operations like finding averages, minimums, maximums, and counting elements in a list.

Uploaded by

ianhenry000
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Basic Python running codes covering:

• Variables

• Arithmetic Operations

• Arrays (Lists)

• Loops

VARIABLES IN PYTHON

Example 1: Declaring Variables

# Declaring variables

name = "John"

age = 20

height = 5.8

# Printing variables

print("Name:", name)

print("Age:", age)

print("Height:", height)

Explanation:

• name → String variable

• age → Integer variable

• height → Float variable

• print() → Displays output

Example 2: Taking User Input

name = input("Enter your name: ")

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

print("Hello", name)

print("Next year you will be", age + 1)


ARITHMETIC OPERATIONS

Example 1: Basic Arithmetic

a = 10

b=5

print("Addition:", a + b)

print("Subtraction:", a - b)

print("Multiplication:", a * b)

print("Division:", a / b)

print("Modulus:", a % b)

Example 2: Simple Calculator

num1 = float(input("Enter first number: "))

num2 = float(input("Enter second number: "))

print("Sum:", num1 + num2)

print("Difference:", num1 - num2)

print("Product:", num1 * num2)

print("Quotient:", num1 / num2)

ARRAYS (LISTS IN PYTHON)

Python uses lists instead of traditional arrays.

Example 1: Creating and Accessing a List

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

print("First element:", numbers[0])

print("Last element:", numbers[-1])

print("All elements:", numbers)


Example 2: Adding Elements to a List

fruits = ["Apple", "Banana", "Mango"]

[Link]("Orange")

print("Updated list:", fruits)

Example 3: Sum of List Elements

numbers = [5, 10, 15, 20]

total = sum(numbers)

print("Sum of list:", total)

LOOPS IN PYTHON

A. FOR LOOP

Example 1: Print Numbers 1–5

for i in range(1, 6):

print(i)

Example 2: Loop Through a List

fruits = ["Apple", "Banana", "Mango"]

for fruit in fruits:

print(fruit)

B. WHILE LOOP

Example 1: Print Numbers 1–5

count = 1
while count <= 5:

print(count)

count += 1

Example 2: Sum Using While Loop

num = 1

total = 0

while num <= 5:

total += num

num += 1

print("Total:", total)

COMBINED EXAMPLE (Variables + List + Loop)

numbers = [2, 4, 6, 8, 10]

total = 0

for num in numbers:

total += num

average = total / len(numbers)

print("Total:", total)

print("Average:", average)
Here is a clean calculator program that combines:

• Addition

• Subtraction

• Multiplication

• Division

(Using functions — beginner-friendly in Python)

Complete Calculator Program

# Calculator Program

def add(a, b):

return a + b

def subtract(a, b):

return a - b

def multiply(a, b):

return a * b

def divide(a, b):

if b != 0:

return a / b

else:

return "Cannot divide by zero"

print("Simple Calculator")

print("1. Addition")

print("2. Subtraction")

print("3. Multiplication")

print("4. Division")
choice = input("Choose operation (1-4): ")

a = float(input("Enter 1st number: "))

b = float(input("Enter 2nd number: "))

if choice == "1":

print(f"Result: {add(a, b)}")

elif choice == "2":

print(f"Result: {subtract(a, b)}")

elif choice == "3":

print(f"Result: {multiply(a, b)}")

elif choice == "4":

print(f"Result: {divide(a, b)}")

else:

print("Invalid choice")

Example Output

Simple Calculator

1. Addition

2. Subtraction

3. Multiplication

4. Division

Choose operation (1-4): 3

Enter 1st number: 10

Enter 2nd number: 5

Result: 50
We will cover:

• Average

• Minimum

• Maximum

• Sum

• Count

• Even & Odd

• Largest of Three Numbers

Average of Numbers

# Average of 5 numbers

numbers = []

for i in range(5):

num = float(input("Enter number: "))

[Link](num)

average = sum(numbers) / len(numbers)

print("Numbers:", numbers)

print("Average:", average)

Minimum and Maximum (Using Built-in Functions)

numbers = [10, 45, 23, 67, 12]

print("Numbers:", numbers)

print("Minimum:", min(numbers))

print("Maximum:", max(numbers))
Minimum and Maximum (Without Built-in Functions)

numbers = [10, 45, 23, 67, 12]

minimum = numbers[0]

maximum = numbers[0]

for num in numbers:

if num < minimum:

minimum = num

if num > maximum:

maximum = num

print("Minimum:", minimum)

print("Maximum:", maximum)

Sum of List Elements

numbers = [5, 10, 15, 20]

total = 0

for num in numbers:

total += num

print("Total Sum:", total)

Count How Many Numbers

numbers = [2, 4, 6, 8, 10]

print("Count of numbers:", len(numbers))

Even and Odd Numbers


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

if number % 2 == 0:

print("Even number")

else:

print("Odd number")

Largest of Three Numbers

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

b = float(input("Enter second number: "))

c = float(input("Enter third number: "))

if a >= b and a >= c:

largest = a

elif b >= a and b >= c:

largest = b

else:

largest = c

print("Largest number is:", largest)

Combined Example (All Together)

numbers = []

n = int(input("How many numbers? "))

for i in range(n):

num = float(input("Enter number: "))

[Link](num)

print("Numbers:", numbers)
print("Sum:", sum(numbers))

print("Average:", sum(numbers)/len(numbers))

print("Minimum:", min(numbers))

print("Maximum:", max(numbers))

print("Count:", len(numbers))

You might also like