Skip to content

Commit ed06535

Browse files
committed
python for beginners
1 parent 5e4e5de commit ed06535

111 files changed

Lines changed: 1383 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
if ( n := len([1, 2, 3,4])) > 3:
2+
print(f"List has {n} elements. Expected <= 3.")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
l = [3, 37, 69, 71, 21]
2+
3+
#---Normally---
4+
# for i in range(len(l)):
5+
# print(f"Item at index {i} is {l[i]}")
6+
#-------OR----
7+
# index = 0
8+
# for i in l:
9+
# print(f"Item at index {index} is {i}")
10+
# index += 1
11+
12+
#----Using enumerate:
13+
14+
for index, i in enumerate(l):
15+
print(f"Item at index {index} is {i}")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
try:
2+
x = int(input("Enter a number: "))
3+
print(f"You entered: {x}")
4+
except:
5+
print(f"Please enter a valid integer.")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# finally is mainly used in the context of exception handling in Python.
2+
# mainly under functions.
3+
4+
def divide_numbers(a, b):
5+
try:
6+
result = a / b
7+
except ZeroDivisionError:
8+
print("Error: Division by zero is not allowed.")
9+
return None
10+
else:
11+
print(f"{a} divided by {b} gives: {result}")
12+
finally:
13+
print("Execution of divide_numbers completed.")
14+
15+
divide_numbers(10, 2)
16+
17+
divide_numbers(10, 0)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#----- works for strings ----
2+
fruits = ['apple', 'banana', 'cherry']
3+
joined_fruits = ' _ '.join(fruits)
4+
print(joined_fruits)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#-- using a normal function ---
2+
3+
# def square():
4+
# n = int(input("Enter the number to get it's square: "))
5+
# print(f"Square of {n} is: {n * n}")
6+
# square()
7+
8+
#--- using lambda function ---
9+
10+
square = lambda n = int(input("Enter the number to get it's square: ")): print(f"Square of {n} is: {n * n}")
11+
#--- here lambda takes 'n' :and returns/prints the square of 'n' ---
12+
square()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
l = [1, 3, 5, 7, 9]
2+
3+
#---------using for loop----
4+
# squared_l = []
5+
# for i in l:
6+
# squared_l.append(i ** 2)
7+
# print(squared_l)
8+
9+
#---------using list comprehension----
10+
squared_l_comp = [i ** 2 for i in l]
11+
print(squared_l_comp)
12+
13+
add_3_in_l = [i + 3 for i in l]
14+
print(add_3_in_l)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def math_quiz():
2+
print("Welcome to the Math Quiz!")
3+
print("You will be asked a simple math question.")
4+
print("What is 2 + 2")
5+
x = int(input("Please enter your answer: "))
6+
# Using match-case to handle different operations
7+
match x:
8+
case 4:
9+
print("Correct! 2 + 2 equals 4.")
10+
case _:
11+
print("Incorrect.")
12+
13+
math_quiz()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dict1 = {"a": 1, "b": 4, "c": 5}
2+
dict2 = {"b": 3, "d": 6}
3+
# Merging two dictionaries
4+
merged_dict = dict1 | dict2
5+
print("Merged Dictionary:", merged_dict)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
a = int(input("Enter a number: "))
2+
b = int(input("Enter another number: "))
3+
4+
if b == 0:
5+
raise ZeroDivisionError("Division by zero is not allowed.")
6+
else:
7+
result = a / b
8+
print(f"The result of {a} divided by {b} is {result}.")

0 commit comments

Comments
 (0)