Advertisement
Erasyl_BKT_505

Python Coursera

Mar 13th, 2025
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.07 KB | None | 0 0
  1. #-------------------list------------------
  2. """""
  3. num_list = [23, 25,12, 543, 345, 123, 43, 64, 23]
  4. list1 = [1, 2, 3, 4, 5]
  5. print(*list1)
  6. print(list1, sep=" ")
  7. list1.insert(0, 6)
  8. list1.append(8)
  9. list1.extend([1, 2, 3, 4])
  10. list1.pop(3)
  11. del list1 [4]
  12. print(list1, sep=" ")
  13. """
  14. #---------------Tuple---------------------
  15. """""
  16. my_tuple = 1, "Erasyl", 5.4, True
  17.  
  18. print(type(my_tuple))
  19. print(my_tuple.count('Erasyl'))
  20. """
  21. #-------------set-------------------
  22. """""
  23. set_a = {1, 2, 3, 4, 5}
  24. print(type(set_a))
  25. set_a.add(6)
  26. set_a.add(5)
  27. set_a.remove(2)
  28. print(set_a)
  29.  
  30. set_b = {4, 5, 6, 7, 8}
  31.  
  32. set_a.union (set_b)
  33. print(set_a.union(set_b))
  34. print(set_a | set_b)
  35. print(set_a.intersection(set_b))
  36. print(set_a & set_b)
  37. print(set_a.difference(set_b))
  38. print(set_a - set_b)
  39. print(set_a.symmetric_difference(set_b))
  40. print(set_a ^ set_b)
  41. """
  42. #---------------Dictionaries----------------------
  43. """"
  44. my_d = {1: 'Test', 'Name': 'Erasyl', }
  45. my_d[2] = 'Test2'
  46. print(my_d)
  47. for key, value in my_d.items():
  48.    print(str(key) + " : " + value)
  49. """
  50.  
  51. #-------------Args and Kwargs-----------------------
  52. """
  53. def sum_of(*args):
  54.    sum = 0
  55.    for x in args:
  56.        sum += x
  57.    return sum
  58. print(sum_of(5, 3, 6))
  59.  
  60. def sum_of_k(**kwargs):
  61.    sum = 0
  62.    for key, value in kwargs.items():
  63.        sum += value
  64.    return sum
  65.  
  66. print(sum_of_k(coffe = 2.99, cake = 4.33, juse = 2.99))
  67. """
  68. #-------------Exceptions-------------------------------
  69. """
  70. def divide_by(a, b):
  71.    return a / b
  72.  
  73. try:
  74.    ans = divide_by(40, 0)
  75. except ZeroDivisionError as e:
  76.    print(e, "we connot divide by zero")
  77. except Exception as e:
  78.    print("Somthing is wrong!", e)
  79. """
  80.  
  81. #--------------------file------------------------------
  82. """
  83. file = open('test.txt', mode= 'r')
  84.  
  85. data = file.readline()
  86. print(data)
  87. file.close()
  88.  
  89. with open('test.txt', mode = 'r') as file:
  90.    data = file.readline()
  91.  
  92. print(data)
  93.  
  94. """
  95. #------------------create file--------------------------
  96. """
  97. with open('newfile.txt', 'w') as file:
  98.    file.write("This is a new file created!")
  99. with open('newfile.txt', 'w') as file:
  100.    file.writelines(["This is a new file created", "\n This is another line to be added"])
  101. with open('newfile.txt', 'a') as file:
  102.    file.writelines(["\nThis is a new file created", "\n This is another line to be added"])
  103. """
  104. #-----------------reading files--------------------------
  105. """
  106. with open('newfile.txt', 'r') as file:
  107.    print(file.read(10))
  108.  
  109. with open('newfile.txt', 'r') as file:
  110.    print(file.readline())
  111.  
  112. with open('newfile.txt', 'r') as file:
  113.    for x in file:
  114.        print(x)
  115. """
  116. #----------------map and filter----------------------------
  117. """
  118. menu = ["espresso", "mocha", "latte", "cppuccion", "cortado", "americano"]
  119.  
  120. def find_coffee(coffee):
  121.    if coffee[0] == 'c':
  122.        return coffee
  123. #map---->
  124. map_coffee = map(find_coffee, menu)
  125. print(map_coffee)
  126. for x in map_coffee:
  127.    print(x)
  128. #filter---->
  129. filter_coffee = filter(find_coffee, menu)
  130. print(filter_coffee)
  131. for x in filter_coffee:
  132.    print(x)
  133.  
  134. """
  135. #----------------class--------------------
  136. """
  137. class Myclass():
  138.    a = 5
  139.    def hello(self):
  140.        print("hello, world!")    
  141.  
  142.  
  143. myc = Myclass()
  144.  
  145. print(myc.a)
  146. print(myc.hello())
  147. """
  148. #----------------class2---------------------
  149. """
  150. class Recipe():
  151.    def __init__(self, dish, items, time) -> None:
  152.        self.dish = dish
  153.        self.items =items
  154.        self.time = time
  155.    
  156.    def contents(self):
  157.        print("The " + self.dish + " has " + str(self.items) + \
  158.              " and takes " + str(self.time) + " min to prepare")
  159.    
  160. pizza = Recipe("Pizza", ["cheese", "bread", "tomato"], 45)
  161. pasta = Recipe("Pasta", ["penne", "sauce"], 55)
  162.  
  163. print(pizza.items)
  164. print(pasta.items)
  165. print(pizza.contents())
  166. """
  167. #-----------------class, instance methods---------------------------
  168. """
  169. class Payslips:
  170.    def __init__(self, name, payment, amount) -> None:
  171.        self.name = name
  172.        self.payment = payment
  173.        self.amount = amount
  174.  
  175.    def pay(self):
  176.        self.payment = "yes"
  177.  
  178.    def status(self):
  179.        if self.payment == "no":
  180.            return self.name + " is not paid yet"
  181.        else:
  182.            return self.name + " is paid " + str(self.amount)
  183.  
  184.  
  185. Erzhan = Payslips("Erzhan", "no", 1000)
  186. Samat = Payslips("Samat", "no", 3000)
  187.  
  188. print(Erzhan.status(), "\n", Samat.status())
  189.  
  190. Samat.pay()
  191.  
  192. print(Erzhan.status(), "\n", Samat.status())
  193. """
  194.  
  195. #------------------Parent classes vs child classes--------------------
  196. """
  197. class Employees:
  198.    def __init__(self, name, last) -> None:
  199.        self.name = name
  200.        self.last = last
  201.  
  202. class Supervisors(Employees):
  203.    def __init__(self, name, last, password) -> None:
  204.        super().__init__(name, last)
  205.        self.password = password
  206.  
  207. class Chefs(Employees):
  208.    def leave_request(self, days):
  209.        return "May I take a leave for " + str(days) + " days"
  210.  
  211. adil = Supervisors("Adil", "A", "apple")
  212.  
  213. enilik = Chefs("Enilik", "E")
  214. juno = Chefs("Juno", "J")
  215.  
  216. print(enilik.leave_request(3))
  217. print(adil.password)
  218. print(enilik.name)
  219. """
  220.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement