Содержание
- 2. Overview Variables Types : int, float, string User input Functions with input and output The Boolean
- 3. Variables - Motivation Write a program which calculates the difference in the areas of a square
- 4. Variables - Motivation 1.5*1.5 –3.14*(1.5/2 )**2 Area = 1.5*15 r = 1.5/2 Area = pi*r2 ?
- 5. Variables - Motivation Two problems : The expression 1.5*1.5 –3.14*(1.5/2 )**2 is really difficult to understand
- 6. Variables - Motivation Wouldn’t it be much more readable, modular, easy to modify in this format
- 7. Variables Variables let us define “memory units” which can “remember” values. Variables have 2 main components
- 8. Variables Variables have 2 main functionalities : Set their value number_of_apples = 3 Get their values
- 9. Variables – Naming conventions Use lower case letter number,apples Separate multiple words with underscore word_and_more_words Use
- 10. Types Can we perform the following command ? x = 3 + 5 And this one
- 11. Types int (Integer) : represent an Integer number (מספר שלם). E.g. 1024, 13, 92,0 float :
- 12. Types The type() function receives a value and return its type. type(3) ? int type(3.0) ?
- 13. Types What happens when we mix types? type(1 + 'some string') TypeError: unsupported operand type(s) for
- 14. Error message Error messages are our friends, they help us detect bugs in our program and
- 15. Error message - Example >>> x = 49 >>> x/(49**0.5 - 7) Traceback (most recent call
- 16. Error message - Example Traceback (most recent call last): File "C:/my_python/test.py", line 2, in x/(49**0.5 -
- 17. Error message - Example Traceback (most recent call last): File "C:/my_python/test.py", line 2, in x/(49**0.5 -
- 18. Error message - Example Traceback (most recent call last): File "C:/my_python/test.py", line 2, in x/(49**0.5 -
- 19. Error message - Example Traceback (most recent call last): File "C:/my_python/test.py", line 2, in x/(49**0.5 -
- 20. Error message - Example Traceback (most recent call last): File "C:/my_python/test.py", line 2, in x/(49**0.5 -
- 21. Error message - Example Traceback (most recent call last): File "C:/my_python/test.py", line 2, in x/(49**0.5 -
- 22. But what if we do want to mix types? my_apples = 3 print('I have ' +
- 23. Converting types (casting) int, float and str are not only names of types but also names
- 24. Converting types – int(),float() Converts string representing numbers to the represented numbers int('5')?5 Cannot convert strings
- 25. User input To make a program interactive we can ask the user for some inputs and
- 26. User input - Example square_side = input('Insert side length: ') # Wait for user … 3
- 27. User input - Example area = square_side * square_side '3'*'3' TypeError: can't multiply sequence by non-int
- 28. User input - Example square_side = float(input('Insert side length: ')) # Wait for user … 3
- 29. Functions with input def function_name(param1, param2,…,paramN): #indented code is here #as usual The name of the
- 30. Functions with input When we call a function with input parameters, we can use the parameters’
- 31. Functions with input def print_hello_user(user_name): print('hello ' + user_name) print_hello_user('John') Function definition Function body (implementation) Function
- 32. Functions with input def print_hello_user(user_name): print('hello ' + user_name) print_hello_user('John') 1) When we call the function
- 33. Functions with input def print_hello_user(user_name): print('hello ' + user_name) print_hello_user('John') >>> hello John print_hello_user('Doe') >>> hello
- 34. A word about scopes def print_hello_user(user_name): print('hello ' + user_name) print_hello_user('John') print('Good bye ' + user_name)
- 35. A word about scopes def print_hello_user(user_name): print('hello ' + user_name) print_hello_user('John') print('Good bye ' + user_name)
- 36. A word about scopes ROOT = 2 def square_root(number): print(number**(1/ROOT)) square_root(4) >>> 2.0 A new scope
- 37. A word about scopes x = 2 def example(): x = 5 print(x) example() print(x) >>>
- 38. A function with more than 1 input def get_details(name, password): print('Name is :' + name +
- 39. Functions’ parameters default value Sometimes …. A function has an obvious use case that will be
- 40. Functions’ parameters default value def shoresh(number, root=2): print(number ** (1/root)) The first parameter, number, has no
- 41. Functions’ parameters default value def shoresh(number, root=2): print(number ** (1/root)) shoresh(64) # Here we didn’t indicate
- 42. Function’s return value Many times we want functions to not only perform some functionality, but also
- 43. Function’s return value def always_return_5(): return 5 print('hi') return means we terminate the function’s run and
- 44. Function’s return value def always_return_5(): return 5 print('hi') print(3 + always_return_5()) >>> 8 The function returns
- 45. Function calling a function We can use the return value of one function as another function’s
- 46. Function calling a function def per_week(per_day=1): return per_day * 7 # return 7 def per_year(how_many_per_week): return
- 47. Function calling a function def per_week(per_day=1): return per_day * 7 # return 7 def per_year(how_many_per_week): return
- 48. Function calling a function We can use the return value of one function as another function’s
- 49. Multiple outputs functions To return more than one value, separate return values by comma def diff_and_ratio(num1,
- 50. None None is a special value which is used to represent absence of value. Every function
- 51. None - example def print_hi(): print('hi') x = print_hi() # x is assigned the value None
- 52. The Boolean type Like int, str and float , Boolean is another Python type. Boolean can
- 53. Boolean expressions Boolean expressions are expressions which use Boolean operators to evaluate a value of True
- 54. Boolean operators type(5 > 7) >>>
- 55. Boolean expressions 7 == 4 ? ? (7 != 2) == (5 > 4) ? ?
- 56. Boolean expressions 7 == 4 ? False (7 != 2) == (5 > 4) ? True
- 57. Complex Boolean operators Take few Boolean operators and evaluate a new Boolean value from them. and
- 58. Complex Boolean operators Truth table
- 59. Conditional operation We do not always want to execute all the lines in our code. Sometimes
- 60. Conditional operation - if How do we implement this notion in Python? if boolean_expression: #Code to
- 61. Conditional operation - if For example : num = float(input('Insert a number‘)) if num != 0
- 62. Conditional operation - if num = float(input('Insert a number')) if num != 0 : print(9/num) if
- 63. Conditional operation - else num = float(input('Insert a number')) if num != 0 : print(9/num) else:
- 64. Conditional operation - elif And what if we had some more options to choose from? If
- 65. Conditional operation - elif if now == 'Morning': print('Good morning!') elif now == 'Noon': print('Good noon')
- 66. Nested if What operations could be included inside an if block? Any operations we like :
- 67. Nested if - example if now == 'morning': if 'y' == input('Are you hungry?'): print('Bon appetit!')
- 68. Nested if - example if now == 'morning': if 'y' == input('Are you hungry?'): print('Bon appetit!')
- 69. split() The method split() returns a list of all the words in the string, using a
- 70. Example Calculate the circumference (היקף) of a circle or square according to user request. Let’s break
- 71. Example – break it up into functions calculate_circle_ circumference() calculate_rectangle_ circumference() is_valid_shape_choice(choice) get_user_input() calculater_user_choice_circumference() error_safe_circumference() Then
- 72. PI = 3.14 CHOICE_CIRCLE = 'C' CHOICE_RECTANGLE = 'R' MESSAGE_INPUT_REQUEST = 'Choose shape(C,R): ' MESSAGE_OUTPUT_REPORT =
- 73. def calculate_circumference(shape): # runs 5’th if shape == CHOICE_CIRCLE : return calculate_circle_circumference() elif shape == CHOICE_RECTANGLE:
- 75. Скачать презентацию