├── __pycache__ └── string.cpython-311.pyc ├── dictionary comprehension.py ├── zip function.py ├── reduce library.py ├── cropping ex.py ├── dictionary.py ├── upper fanction.py ├── filter.py ├── map.py ├── string.py ├── 434.py ├── comments & Docs.py ├── list comprehension.py ├── list fanction.py ├── image cropping.py ├── for-range.py ├── exception handling.py ├── oop-review.py ├── class.py └── game.py /__pycache__/string.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AbdullahBakir97/Python-Basics/HEAD/__pycache__/string.cpython-311.pyc -------------------------------------------------------------------------------- /dictionary comprehension.py: -------------------------------------------------------------------------------- 1 | students = {'ahmad':60,'ali':70,'hassan':80} 2 | 3 | result = {k:v*2 for (k,v) in students.items()} 4 | print(result) 5 | -------------------------------------------------------------------------------- /zip function.py: -------------------------------------------------------------------------------- 1 | a = ("John", "Charles", "Mike") 2 | b = ("Jenny", "Christy", "Monica") 3 | 4 | x = zip(a, b) 5 | 6 | #use the tuple() function to display a readable version of the result: 7 | 8 | print(tuple(x)) 9 | -------------------------------------------------------------------------------- /reduce library.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | from functools import reduce 4 | 5 | numbers = list(range(1,101)) 6 | 7 | 8 | def mysum(x,y): 9 | return x+y 10 | 11 | 12 | 13 | result = reduce(mysum,numbers) 14 | print(result) 15 | -------------------------------------------------------------------------------- /cropping ex.py: -------------------------------------------------------------------------------- 1 | from PIL import image 2 | 3 | image = "monster" 4 | 5 | img = image.open(image) 6 | 7 | box = (250, 250, 750, 750) 8 | 9 | img2 = img.crop(box) 10 | 11 | img2.save('myimage_cropped') 12 | 13 | img.show() 14 | -------------------------------------------------------------------------------- /dictionary.py: -------------------------------------------------------------------------------- 1 | d = {'ahmad':70 , 'ali':80} 2 | d['ali'] 3 | 4 | d['ali'] = 90 5 | d 6 | 7 | d.keys() 8 | 9 | d.values() 10 | 11 | d.items() 12 | 13 | d = {'ahmad':70 , 'ali':80} 14 | 15 | for x,y in d.items(): 16 | print(x,y) 17 | 18 | -------------------------------------------------------------------------------- /upper fanction.py: -------------------------------------------------------------------------------- 1 | 2 | def myfunc(names): 3 | result = [] 4 | for n in names: 5 | result.append(n) 6 | 7 | print((result)) 8 | 9 | 10 | 11 | 12 | 13 | myfunc.upper(['mahmoud','ahmad','ali','mostafa']) 14 | -------------------------------------------------------------------------------- /filter.py: -------------------------------------------------------------------------------- 1 | numbers = list(range(1,11)) 2 | 3 | 4 | def myFilter(n): 5 | if n%2==0: 6 | return n 7 | 8 | 9 | result = list(filter(myFilter,numbers)) 10 | print(result) 11 | 12 | 13 | result = list(map(myFilter,numbers)) 14 | print(result) 15 | 16 | 17 | -------------------------------------------------------------------------------- /map.py: -------------------------------------------------------------------------------- 1 | ''' 2 | names = ['ahmed','ali','hassan','mohamed'] 3 | 4 | 5 | def myFunc(n): 6 | return len(n) 7 | 8 | 9 | result = list(map(myFunc,names)) 10 | print(result) 11 | ''' 12 | 13 | 14 | 15 | numbers = list(range(1,11)) 16 | 17 | 18 | def myFunc(n): 19 | return n*n 20 | 21 | result = list(map(myFunc,numbers)) 22 | print(result) 23 | -------------------------------------------------------------------------------- /string.py: -------------------------------------------------------------------------------- 1 | 2 | x = m 3 | 4 | x+x 5 | 6 | x*5 7 | 8 | fname = 'mahmoud' 9 | lname = 'ahmed' 10 | print(f"my full name is {fname} {lname}") 11 | 12 | f = f"my full name is {fname} {lname}" 13 | 14 | f.title() 15 | 16 | f.upper() 17 | 18 | f.lower() 19 | 20 | f.islower() 21 | 22 | f.isupper() 23 | 24 | f.istitle() 25 | 26 | f.replace('ahmed','ali') 27 | 28 | f.split(' ') 29 | 30 | -------------------------------------------------------------------------------- /434.py: -------------------------------------------------------------------------------- 1 | ''' 2 | x = 5 3 | y = 6 4 | z = 7 5 | 6 | if all([x==5 , y==6 , z==7]): 7 | print('----') 8 | ''' 9 | 10 | ''' 11 | x = 0 12 | while x < 10 : 13 | print(x) 14 | x += 1 15 | ''' 16 | 17 | start = input('Enter Start : ') 18 | end = input('Enter End : ') 19 | print(type(start)) 20 | for x in range(start,end): 21 | for y in range(1,11): 22 | print(f"{x} x {y} = {x*y}") 23 | -------------------------------------------------------------------------------- /comments & Docs.py: -------------------------------------------------------------------------------- 1 | # comments & Docs 2 | 3 | 4 | class Calc: 5 | ''' simple calculator class ''' 6 | 7 | def sum(self,x,y): #BUG issue TODO 8 | ''' 9 | function to sum 2 values , x , y 10 | x : int 11 | y : int 12 | return : int 13 | 14 | ''' 15 | result = x+y 16 | return result 17 | 18 | 19 | c = Calc() 20 | print(c.sum(2,3)) 21 | -------------------------------------------------------------------------------- /list comprehension.py: -------------------------------------------------------------------------------- 1 | ''' 2 | names = ['ahmad','ali';'hassan';'mahamed'] 3 | 4 | #1 5 | result = [] 6 | for n in names: 7 | result.append(len(n)) 8 | 9 | print(result) 10 | 11 | 12 | #2 13 | 14 | result2 = [len(n) for n in names] 15 | print(result2) 16 | 17 | ''' 18 | 19 | #even = [n for n in range(1,101) if n%2 ==0 ] 20 | #print(even) 21 | 22 | even = [f'{n} even' if n%2==0 else f'{n} odd' for n in range(1,101)] 23 | print(even) 24 | -------------------------------------------------------------------------------- /list fanction.py: -------------------------------------------------------------------------------- 1 | l = [1,2,3,4,5,6] 2 | l[0] 3 | 4 | l[-1] 5 | 6 | l[-2] 7 | 8 | l[0:3] 9 | 10 | l[4:] 11 | 12 | l[:3] 13 | 14 | l.append(1000) 15 | 16 | l 17 | 18 | l.insert(0,100) 19 | 20 | l 21 | 22 | l.sort() 23 | l 24 | 25 | l.sort(reverse=True) 26 | l 27 | 28 | l.sort() 29 | l.reverse() 30 | l 31 | 32 | min(l) 33 | 34 | max(l) 35 | 36 | l.remove(1000) 37 | l 38 | 39 | l[0] = 500 40 | l 41 | #tubel 42 | #cant be much edited 43 | t = (1,2,3,4,5,6,True,) 44 | min(t) 45 | 46 | max(t) 47 | #and counting 48 | 49 | 50 | l = [1,2,3,4,5,6,True,'ahmed'] 51 | -------------------------------------------------------------------------------- /image cropping.py: -------------------------------------------------------------------------------- 1 | import os 2 | from PIL import Image 3 | 4 | # crooping size 5 | width = input('Enter Cropping width : ') 6 | height = input('Enter Cropping height : ') 7 | 8 | 9 | # open folder 10 | os.chdir('images') 11 | 12 | 13 | # putput folder 14 | output_folder = input('Enter Folder Name : ') 15 | os.makedirs(output_folder, exist_ok=True) 16 | 17 | 18 | # loob over each image 19 | for image in os.listdir('.'): 20 | if image.endswith(('.png','.jpg','.jpeg')): 21 | im = image.open(image) 22 | im1 = im.crop((0, 0, width, height)) 23 | im1.save(f"{output_folder}/{image}") 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /for-range.py: -------------------------------------------------------------------------------- 1 | ''' 2 | def mysum(x=0,y=0): 3 | print(x+y) 4 | 5 | 6 | 7 | mysum(5,10) 8 | ''' 9 | ''' 10 | # defination 11 | def mysum(x,*vartuple): 12 | result = x 13 | for i in vartuple: 14 | result += i 15 | print(result) 16 | 17 | 18 | # call 19 | mysum(5,10,5,6,8,5) 20 | ''' 21 | ''' 22 | f = 0 23 | print(f) 24 | 25 | def do(): 26 | global f 27 | f = 5 28 | print(f) 29 | 30 | do() 31 | print(f) 32 | ''' 33 | 34 | 35 | def mysum(x,y): 36 | return x+y 37 | 38 | print(mysum(5,10)) 39 | 40 | 41 | mysum2 = lambda x,y:x+y 42 | print(mysum2(5,10)) 43 | 44 | 45 | d = (lambda x,y:x+y)(5,10) 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /exception handling.py: -------------------------------------------------------------------------------- 1 | # exception handling 2 | 3 | # if-else 4 | import math 5 | 6 | age = input('Enter Age') 7 | if age.replace('.',',').isnumeric(): 8 | if float(age) > 0: 9 | print(round(100/float(age)),2) 10 | print(math.floor(100/float(age))) 11 | print(math.ceil(100/float(age))) 12 | else: 13 | print('please enter number > 0') 14 | else: 15 | print('please enter number') 16 | 17 | print('procesing') 18 | 19 | ############################################# 20 | #try-except 21 | try: 22 | age = int(input('Enter Age:')) 23 | print(100/age) 24 | # processing x 25 | 26 | # close 27 | 28 | ''' 29 | #all exceptions 30 | except Exception: 31 | print('please enter number and number > 0') 32 | ''' 33 | 34 | except ValueError: 35 | print('please enter number and number > 0') 36 | # close 37 | 38 | 39 | except ZeroDivisionError: 40 | print('please enter number and number > 0') 41 | # close 42 | 43 | 44 | else: # no excptions 45 | print('in else') 46 | 47 | 48 | 49 | 50 | finally: # always 51 | print('closing ....') 52 | 53 | 54 | print('procesing') 55 | 56 | -------------------------------------------------------------------------------- /oop-review.py: -------------------------------------------------------------------------------- 1 | ''' 2 | student: 3 | - create student (name) -> welcome 4 | -add mark 5 | -get avg 6 | ''' 7 | 8 | ''' 9 | class student: 10 | def __init__(self,name): 11 | print (f'welcome {name} ') 12 | self.name = name 13 | self.marks = [] 14 | 15 | 16 | def add_mark(self,mark): 17 | self.marks.append(mark) 18 | print(self.marks) 19 | 20 | def get_avg(self): 21 | print (self.marks) 22 | avg = sum(self.marks)/len(self.marks) 23 | print(f"{self.name} = {avg}") 24 | 25 | 26 | s1 = student ('ahmad' ' ' 'mahmoud') 27 | s2 = student('Ali') 28 | 29 | s1.add_mark(50) 30 | s1.add_mark(70) 31 | s2.add_mark(80) 32 | s2.add_mark(70) 33 | 34 | 35 | s1.get_avg() 36 | s2.get_avg() 37 | ''' 38 | 39 | #s1.create_student('ahmed','mahmoud') 40 | 41 | 42 | ''' 43 | Bank: 44 | - create clint (name,age,gender) 45 | - deposit 46 | - withdraw 47 | - show details 48 | 49 | ''' 50 | class User: 51 | 52 | 53 | def __init__(self,name,age,gender): 54 | print(f'welcome {name}') 55 | self.name = name 56 | self.age = age 57 | self.gender = gender 58 | 59 | def show_details(self): 60 | print(f"Name : {self.name}") 61 | print(f"Age : {self.age}") 62 | print(f"Gender : {self.gender}") 63 | print(f"Balance : {self.balance}") 64 | 65 | 66 | class Bank(User): 67 | def __init__(self,name,age,gender): 68 | super().__init__(name,age,gender) 69 | self.balance=0 70 | 71 | 72 | def deposit(self,amount): 73 | self.balance += amount 74 | print(f"your current balance : {self.balance}") 75 | 76 | def withdraw(self,amount): 77 | if amount > self.balance: 78 | print(f'insuffecient balance : {self.balance}') 79 | return 80 | 81 | self.balance -= amount 82 | print(f"your current balance : {self.balance}") 83 | 84 | 85 | 86 | c1 = Bank('Ali',30,'Male') 87 | 88 | c1.deposit(500) 89 | c1.deposit(1000) 90 | 91 | c1.withdraw(800) 92 | c1.withdraw(1000) 93 | 94 | c1.show_details() 95 | 96 | 97 | -------------------------------------------------------------------------------- /class.py: -------------------------------------------------------------------------------- 1 | ''' 2 | class str 3 | '' "" ''' ''' 4 | 5 | def upper 6 | def lower 7 | def split 8 | def replace 9 | def append 10 | def insert 11 | 12 | ''' 13 | 14 | ''' 15 | # encopsulation : self c1,c2 16 | class Calc: #c1,c2 17 | def mysum(self,x,y): 18 | print(x+y) 19 | 20 | 21 | def mymul(self,x,y): 22 | print(x*y) 23 | 24 | 25 | # constructor 26 | def __init__(self,name): 27 | print(f'welcome{name}') 28 | 29 | 30 | c1 = Calc('ahmed') 31 | #c1.mysum(5,10) 32 | 33 | 34 | #c2 = Calc() 35 | #c2.mymul(10,30) 36 | ''' 37 | 38 | ''' 39 | class Calc: 40 | def __init__(self): 41 | print(f'welcome {name}') 42 | 43 | def mysum(self,x,y): 44 | print(x+y) 45 | 46 | def mymul(self,x,y): 47 | print(x*y) 48 | 49 | class SciCalc(Calc): 50 | 51 | def power(self,x,y): 52 | print(x**y) 53 | 54 | 55 | s = SciCalc('ahmed') 56 | 57 | s.mysum(3,4) 58 | s.mymul(3,4) 59 | s.power(3,4) 60 | ''' 61 | 62 | 63 | 64 | # inheritance : B,C,A,D 65 | class A: 66 | def do(self): 67 | print('in A') 68 | 69 | class B(A): 70 | pass 71 | 72 | class C: 73 | def do(self): 74 | print('in C') 75 | 76 | 77 | class D(C,B): 78 | pass 79 | 80 | j = D() 81 | j.do() 82 | 83 | 84 | 85 | 86 | ''' 87 | s = 'ahmed' 88 | l = [1,2,3,4] 89 | t = (1,2,3,4) 90 | 91 | len(s) 92 | len(l) 93 | len(t) 94 | 95 | 96 | # polymorphism 97 | class str: 98 | def len 99 | 100 | class list : inherit 101 | def len 102 | 103 | class tuple : inherit 104 | def len 105 | 106 | ''' 107 | 108 | 109 | ''' 110 | class Calc # main super parent 111 | 112 | class SciCalc(Calc) # derived sub child 113 | ''' 114 | 115 | class Calc : 116 | # property 117 | total = 0 # class vlaue 118 | 119 | def sum(self,x,y): 120 | print(x+y) 121 | 122 | 123 | c = Calc() 124 | print(c.total) 125 | #c.sum(3,4) 126 | 127 | c.total = 100 # instance-object value 128 | print(c.total) 129 | 130 | del c.total 131 | print(c.total) 132 | 133 | del c.total 134 | print(c.total) 135 | 136 | 137 | -------------------------------------------------------------------------------- /game.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Game : 3 | - start : welcome , options 4 | - options to exit 5 | - enter game number 6 | - play gain 7 | ''' 8 | 9 | class Game: 10 | def __init__(self): 11 | while True: 12 | print(''' 13 | welcome To Our Game , Enter Game Number 14 | 1 : Names With Length 15 | 2 : Filter starts with 16 | 3 : From To Multiplication Table 17 | 4 : to exit 18 | ''') 19 | user_choice = int(input('Enter Game Number : ')) 20 | if user_choice == 1 : 21 | names = input('Enter Names Seperatd by , : ') 22 | names_list = names.split(',') 23 | self.name_with_length(names_list) 24 | 25 | 26 | elif user_choice == 2 : 27 | names = input('Enter Names Seperatd by , : ') 28 | char = input('Enter Char : ') 29 | names_list = names.split(',') 30 | self.filter_starts_with(names_list,char) 31 | 32 | elif user_choice == 3 : 33 | start = int(input('Enter Start Number : ')) 34 | end = int(input('Enter End Number : ')) 35 | self.from_to_multiplication_table(start,end) 36 | 37 | elif user_choice == 4 : 38 | return 39 | 40 | 41 | play_again = input('Press any char to play agin , n to exit') 42 | if play_again == 'n': 43 | break 44 | 45 | def name_with_length(self,names): 46 | new_names = [] 47 | for n in names: 48 | new_names.append(len(n)) 49 | print(new_names) 50 | 51 | def filter_starts_with(self,names,char): 52 | new_names = [] 53 | for n in names: 54 | if n.startswith(char): 55 | new_names.append(n) 56 | print(new_names) 57 | 58 | 59 | def from_to_multiplication_table(self,start,end): 60 | for x in range(start,end+1): 61 | for y in range(1,11): 62 | print(f"{x} X {y} = {x*y}") 63 | print('----------') 64 | 65 | 66 | g = Game() 67 | 68 | 69 | 70 | 71 | 72 | #from_to_multiplication_table(5,10) 73 | 74 | #filter_starts_with(['ahmed','hassam','mohamed','ali'],'a') 75 | 76 | #name_with_length(['ahmed','ali','hassan']) 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | --------------------------------------------------------------------------------