Advertisement
tuomasvaltanen

Untitled

Feb 19th, 2021 (edited)
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | None | 0 0
  1. # first lecture, Data Analytics with Python
  2. # Python basics + NumPy
  3. print("Spyder says hello!")
  4.  
  5. salary = 3000
  6.  
  7. raised = 1.20
  8.  
  9. salary = salary * raised
  10.  
  11. person = "John Doe"
  12.  
  13. snowing = True
  14.  
  15. number = "123"
  16.  
  17. number_int = int(number)
  18.  
  19. print(salary)
  20. print(person)
  21. print(snowing)
  22.  
  23. apples = 75
  24. people = 8
  25.  
  26. # / for traditional division, // for leaving out the decimals
  27. apples_for_people = apples // people
  28.  
  29. # remainder / modulus is the same as C, %
  30. leftovers = apples % people
  31.  
  32. value = 100
  33.  
  34. value += 50
  35.  
  36. print(value)
  37.  
  38. # easiest way to make string in Python, is to use f-strings
  39. first_name = "John"
  40. surname = "Doe"
  41.  
  42. greetings = f"Hello, my name is {first_name} {surname}"
  43. print(greetings)
  44.  
  45. year = 2021
  46.  
  47. text = f"This year is {year}. Previous year was {year - 1}."
  48. print(text)
  49.  
  50. # A NEW FILE
  51.  
  52. print("If-statements")
  53.  
  54. number = input("Give a number: ")
  55.  
  56. number = int(number)
  57.  
  58. total = number + 62
  59.  
  60. print(number)
  61.  
  62. if number > 0:
  63.     print("Number is greater than zero.")
  64. elif number < 0:
  65.     print("Number is less than zero.")
  66. else:
  67.     print("Number is zero.")
  68.     print("This is an else statement.")
  69.    
  70.  
  71. print("We are now outside the if/elif/else structure")
  72.    
  73. # in C-language, it's something like this
  74. #if(number > 0)
  75. #{
  76. #    print("Number is greater than zero.");
  77. #}
  78.  
  79. # NEW FILE
  80.  
  81. grades = [1, 5, 3, 4, 3, 2]
  82.  
  83. # second grade, note: indexing starts from 0
  84. print(grades[1])
  85.  
  86. # first grade
  87. print(grades[0])
  88.  
  89. # last grade in this list
  90. print(grades[5])
  91.  
  92. # this one doesn't exist
  93. # print(grades[6])
  94.  
  95. # you can change list values by using the index
  96. grades[0] = 3
  97.  
  98. weekdays = ("Monday", "Tuesday", "Wednesday",
  99.             "Thursday", "Friday", "Saturday", "Sunday")
  100.  
  101. # tuple can't be altered without creating the tuple again from scratch
  102. # weekdays[0] = "Funday"
  103.  
  104. # a list sum and print example
  105. shopcart = [99, 49, 299, 149]
  106.  
  107. total_price = sum(shopcart)
  108.  
  109. print(f"Total price of your purchase: {total_price} €")
  110.  
  111. # you can also create lists from user input strings
  112. # original string
  113. #numbers = "5 12 33 45 6"
  114.  
  115. numbers = input("Give some numbers, separate by space: ")
  116.  
  117. # split to string list
  118. number_list = numbers.split()
  119.  
  120. # convert string list to int list
  121. number_list = list(map(int, number_list))
  122.  
  123.  
  124. # NEW FILE
  125.  
  126. day_1 = [-22, -20, -17, -15, -10]
  127. day_2 = [-10, -8, -12, -7, -6]
  128. day_3 = [-32, -28, -29, -27, -30]
  129.  
  130. temperatures = [day_1, day_2, day_3]
  131.  
  132. # simple loops
  133. numbers = [55, 44, 22, 33, 11, 66]
  134.  
  135. for x in range(11):
  136.     print(x)
  137.    
  138.    
  139. for x in range(1, 11):
  140.     print(x)
  141.    
  142.  
  143. for year in range(2000, 2022):
  144.     print(year)
  145.    
  146.  
  147. for n in numbers:
  148.     print(n)
  149.  
  150. # in C-language, it's something like this
  151. #for(int a = 0; a <= 10; a++)
  152. #{
  153. #    print(a)
  154. #}
  155.  
  156. # you can use if -statements in loops too
  157. for x in range(1, 11):
  158.     if x % 2 == 0:
  159.         print(x)
  160.        
  161.        
  162. # if you have a list of lists, you usually need to do this (two loops):
  163. for day in temperatures:
  164.     for t in day:
  165.         print(t)
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement