Skip to content

Commit 84b2e1e

Browse files
committed
python: classes 1
0 parents  commit 84b2e1e

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

introduction/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Calculator

introduction/calculator.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#Inputs - accept data from the user
2+
#User will choose the desired operation
3+
#Operation - perform the operation
4+
#Output - display the result to the user
5+
6+
7+
def calculator():
8+
print("Welcome to the calculator.")
9+
print("Select operation: ")
10+
11+
print("1. Addition (+)")
12+
print("2. Subtraction (-)")
13+
print("3. Multiplication (*)")
14+
print("4. Division (/)")
15+
16+
choice = input("Enter choice (1/2/3/4): ") #variable
17+
18+
if choice in ('1', '2', '3', '4'):
19+
num1 = float(input("Enter first number: "))
20+
num2 = float(input("Enter second number: "))
21+
22+
if choice == '1':
23+
result = num1 + num2
24+
operation = 'Addition'
25+
elif choice == '2':
26+
result = num1 - num2
27+
operation = 'Subtraction'
28+
elif choice == '3':
29+
result = num1 * num2
30+
operation = 'Multiplication'
31+
elif choice == '4':
32+
if num2 == 0:
33+
print("Error: Division by zero")
34+
return
35+
result = num1 / num2
36+
operation = 'Division'
37+
print(f"{operation} of {num1} and {num2} is {result}")
38+
else:
39+
print("Select a valid operation.")
40+
41+
42+
#Run the calculator function
43+
calculator()
44+
45+
# PS D:\Python-Classes\introduction> python3 calculator.py
46+
# Welcome to the calculator.
47+
# Select operation:
48+
# 1. Addition (+)
49+
# 2. Subtraction (-)
50+
# 3. Multiplication (*)
51+
# 4. Division (/)
52+
# Enter choice (1/2/3/4): 1
53+
# Enter first number: 30
54+
# Enter second number: 45
55+
# Addition of 30.0 and 45.0 is 75.0
56+
# PS D:\Python-Classes\introduction>

introduction/intro.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
print("Tell them am here")
2+
3+
x = 1 #variable x is assigned the value 1
4+
print(x)
5+
6+
if x > 1:
7+
print("x is greater than 1.")
8+
else:
9+
x = 1
10+
print("x is equal to 1.")
11+
12+
############################### Data types
13+
# Strings - text
14+
# integers - whole numbers
15+
# floats - decimal numbers
16+
# booleans - True or False
17+
# lists - ordered collection of items "[1, 2, 3]"
18+
# tuples - ordered collection of items that cannot be changed "(1, 2, 3)"
19+
# dictionaries - unordered collection of items in key:value pairs "{key: value}"
20+
# sets - unordered collection of unique items "{1, 2, 3}": subset, superset, union, intersection, difference.
21+
22+
# Strings
23+
def print_name():
24+
name = "John"
25+
print("Your name is", name)
26+
27+
print_name() # function call
28+
29+
30+
def sum():
31+
a = 5
32+
b = 3
33+
print(a + b)
34+
35+
sum() # function call
36+
37+
# Integers
38+
age = 25
39+
40+
if age >= 18:
41+
print(age, ",You are an adult.")
42+
else:
43+
print(age, "You are a minor.")
44+
45+
# Floats
46+
height = 5.9
47+
print("Your height is", height)

introduction/lists.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
mylist = [1, 2, 3, 4, 5]
2+
print(mylist)
3+
4+
age = [] #Age of students
5+
age.append(25)
6+
age.append(30)
7+
age.append(35)
8+
age.append(40)
9+
10+
print(age[0])
11+
print(age[1])
12+
print(age[2])
13+
print(age[3])
14+
15+
print(age)
16+
names = ["John", "Jane", "Doe"]
17+
18+
print(names[0])
19+
20+
21+
for i in names:
22+
print(i)

0 commit comments

Comments
 (0)