🗊Презентация Basic programming

Нажмите для полного просмотра!
Basic programming, слайд №1Basic programming, слайд №2Basic programming, слайд №3Basic programming, слайд №4Basic programming, слайд №5Basic programming, слайд №6Basic programming, слайд №7Basic programming, слайд №8Basic programming, слайд №9Basic programming, слайд №10Basic programming, слайд №11Basic programming, слайд №12Basic programming, слайд №13Basic programming, слайд №14Basic programming, слайд №15Basic programming, слайд №16Basic programming, слайд №17Basic programming, слайд №18Basic programming, слайд №19Basic programming, слайд №20Basic programming, слайд №21Basic programming, слайд №22Basic programming, слайд №23Basic programming, слайд №24Basic programming, слайд №25Basic programming, слайд №26Basic programming, слайд №27Basic programming, слайд №28Basic programming, слайд №29Basic programming, слайд №30Basic programming, слайд №31Basic programming, слайд №32Basic programming, слайд №33Basic programming, слайд №34Basic programming, слайд №35Basic programming, слайд №36Basic programming, слайд №37Basic programming, слайд №38Basic programming, слайд №39Basic programming, слайд №40Basic programming, слайд №41Basic programming, слайд №42Basic programming, слайд №43Basic programming, слайд №44Basic programming, слайд №45Basic programming, слайд №46Basic programming, слайд №47Basic programming, слайд №48Basic programming, слайд №49Basic programming, слайд №50Basic programming, слайд №51Basic programming, слайд №52Basic programming, слайд №53Basic programming, слайд №54Basic programming, слайд №55Basic programming, слайд №56Basic programming, слайд №57Basic programming, слайд №58Basic programming, слайд №59Basic programming, слайд №60Basic programming, слайд №61Basic programming, слайд №62Basic programming, слайд №63Basic programming, слайд №64Basic programming, слайд №65Basic programming, слайд №66Basic programming, слайд №67Basic programming, слайд №68Basic programming, слайд №69Basic programming, слайд №70Basic programming, слайд №71Basic programming, слайд №72Basic programming, слайд №73Basic programming, слайд №74

Содержание

Вы можете ознакомиться и скачать презентацию на тему Basic programming. Доклад-сообщение содержит 74 слайдов. Презентации для любого класса можно скачать бесплатно. Если материал и наш сайт презентаций Mypresentation Вам понравились – поделитесь им с друзьями с помощью социальных кнопок и добавьте в закладки в своем браузере.

Слайды и текст этой презентации


Слайд 1





Tirgul 2
Basic programming
Описание слайда:
Tirgul 2 Basic programming

Слайд 2





Overview
Variables
Types : int, float, string
User input
Functions with input and output
The Boolean type and Boolean operations
Conditional operation (if…else…)
Описание слайда:
Overview Variables Types : int, float, string User input Functions with input and output The Boolean type and Boolean operations Conditional operation (if…else…)

Слайд 3





Variables - Motivation
Write a program which calculates the difference in the areas of a square with side = 1.5 and the circle enclosed within it.
Описание слайда:
Variables - Motivation Write a program which calculates the difference in the areas of a square with side = 1.5 and the circle enclosed within it.

Слайд 4





Variables - Motivation
1.5*1.5 –3.14*(1.5/2 )**2
Описание слайда:
Variables - Motivation 1.5*1.5 –3.14*(1.5/2 )**2

Слайд 5





Variables - Motivation
Two problems : 
The expression 1.5*1.5 –3.14*(1.5/2 )**2
 is really difficult to understand :
When you get back to it after one week
When debugging
When the side of the square changes. Should you have an expression per side-length?
Side=1.5 : 1.5*1.5 – 3.14*(1.5/2 )**2
Side=3.7 : 3.7*3.7 – 3.14*(3.7/2 )**2
Side=9 : 9*9 – 3.14*(9/2) )**2
Описание слайда:
Variables - Motivation Two problems : The expression 1.5*1.5 –3.14*(1.5/2 )**2 is really difficult to understand : When you get back to it after one week When debugging When the side of the square changes. Should you have an expression per side-length? Side=1.5 : 1.5*1.5 – 3.14*(1.5/2 )**2 Side=3.7 : 3.7*3.7 – 3.14*(3.7/2 )**2 Side=9 : 9*9 – 3.14*(9/2) )**2

Слайд 6





Variables - Motivation
Wouldn’t it be much more readable, modular, easy to modify in this format :
side = 1.5, PI = 3.14
square_area = side*side
radius = side/2
circle_area = PI*r2
answer = square_area – circle_area
Описание слайда:
Variables - Motivation Wouldn’t it be much more readable, modular, easy to modify in this format : side = 1.5, PI = 3.14 square_area = side*side radius = side/2 circle_area = PI*r2 answer = square_area – circle_area

Слайд 7





Variables
Variables let us define “memory units” which can “remember” values.
Variables have 2 main components : 
name
value
Описание слайда:
Variables Variables let us define “memory units” which can “remember” values. Variables have 2 main components : name value

Слайд 8





Variables
Variables have 2 main functionalities : 
Set their value 
number_of_apples = 3
Get their values 
tomorrow_apples = number_of_apples + 1
Описание слайда:
Variables Variables have 2 main functionalities : Set their value number_of_apples = 3 Get their values tomorrow_apples = number_of_apples + 1

Слайд 9





Variables – Naming conventions
Use lower case letter
number,apples
Separate multiple words with underscore
word_and_more_words
Use meaningful names for names (don’t be shy to open a dictionary)
z = x/y ???
words_per_page = words_in_book/number_of_pages 
Use capitals for constants (variables which do not change their value after first initialization)
PI = 3.14, ERROR_MESSAGE = ‘You had an error’
Описание слайда:
Variables – Naming conventions Use lower case letter number,apples Separate multiple words with underscore word_and_more_words Use meaningful names for names (don’t be shy to open a dictionary) z = x/y ??? words_per_page = words_in_book/number_of_pages  Use capitals for constants (variables which do not change their value after first initialization) PI = 3.14, ERROR_MESSAGE = ‘You had an error’

Слайд 10





Types
Can we perform the following command ? 
x = 3 + 5 
And this one ? 
x = 3 + “hello”
Why not? 3 and ‘hello’ are not of the same category. The name Python gives to the categories which differentiate between objects such as 3 and ‘hello’  are called type.
Описание слайда:
Types Can we perform the following command ? x = 3 + 5 And this one ? x = 3 + “hello” Why not? 3 and ‘hello’ are not of the same category. The name Python gives to the categories which differentiate between objects such as 3 and ‘hello’ are called type.

Слайд 11





Types
int (Integer) : represent an Integer number (מספר שלם). 
E.g. 1024, 13, 92,0
float : represent a fractional number. 
E.g. : 0.0, 15.62545, 3.14
str (String) : represent text, a list of characters. Defined between a couple of apostrophe or quotes (equivalent). 	
E.g. ‘hello’, “hello”, ‘13’
Описание слайда:
Types int (Integer) : represent an Integer number (מספר שלם). E.g. 1024, 13, 92,0 float : represent a fractional number. E.g. : 0.0, 15.62545, 3.14 str (String) : represent text, a list of characters. Defined between a couple of apostrophe or quotes (equivalent). E.g. ‘hello’, “hello”, ‘13’

Слайд 12





Types
The type() function receives a value and return its type.
type(3)  int
type(3.0)  float
type('3.0')  str
What happens when we mix types?
type(1 + 0.5)  float
type(1 + 'some string')  ?
Описание слайда:
Types The type() function receives a value and return its type. type(3)  int type(3.0)  float type('3.0')  str What happens when we mix types? type(1 + 0.5)  float type(1 + 'some string')  ?

Слайд 13





Types
What happens when we mix types?
type(1 + 'some string')
TypeError: unsupported operand type(s) for +: 'int' and 'str'
This is an error message which tells us we have tried to execute a command not supported by the language.
Описание слайда:
Types What happens when we mix types? type(1 + 'some string') TypeError: unsupported operand type(s) for +: 'int' and 'str' This is an error message which tells us we have tried to execute a command not supported by the language.

Слайд 14





Error message
Error messages are our friends, they help us detect bugs in our program and point out how to fix them. 
When you get an error  “keep calm and read the error message”.
Описание слайда:
Error message Error messages are our friends, they help us detect bugs in our program and point out how to fix them. When you get an error “keep calm and read the error message”.

Слайд 15





Error message - Example
>>> x = 49
>>> x/(49**0.5 - 7)
Traceback (most recent call last):
  File "C:/my_python/test.py", line 9, in <module>
    x/(49**0.5 - 7)
ZeroDivisionError: float division by zero
Remember - “keep calm and read the error message”
Описание слайда:
Error message - Example >>> x = 49 >>> x/(49**0.5 - 7) Traceback (most recent call last): File "C:/my_python/test.py", line 9, in <module> x/(49**0.5 - 7) ZeroDivisionError: float division by zero Remember - “keep calm and read the error message”

Слайд 16





Error message - Example
Traceback (most recent call last):
  File "C:/my_python/test.py", line 2, in <module>
    x/(49**0.5 - 7)
ZeroDivisionError: float division by zero
Описание слайда:
Error message - Example Traceback (most recent call last): File "C:/my_python/test.py", line 2, in <module> x/(49**0.5 - 7) ZeroDivisionError: float division by zero

Слайд 17





Error message - Example
Traceback (most recent call last):
  File "C:/my_python/test.py", line 2, in <module>
    x/(49**0.5 - 7)
ZeroDivisionError: float division by zero
Описание слайда:
Error message - Example Traceback (most recent call last): File "C:/my_python/test.py", line 2, in <module> x/(49**0.5 - 7) ZeroDivisionError: float division by zero

Слайд 18





Error message - Example
Traceback (most recent call last):
  File "C:/my_python/test.py", line 2, in <module>
    x/(49**0.5 - 7)
ZeroDivisionError: float division by zero
Описание слайда:
Error message - Example Traceback (most recent call last): File "C:/my_python/test.py", line 2, in <module> x/(49**0.5 - 7) ZeroDivisionError: float division by zero

Слайд 19





Error message - Example
Traceback (most recent call last):
  File "C:/my_python/test.py", line 2, in <module>
    x/(49**0.5 - 7)
ZeroDivisionError: float division by zero
Описание слайда:
Error message - Example Traceback (most recent call last): File "C:/my_python/test.py", line 2, in <module> x/(49**0.5 - 7) ZeroDivisionError: float division by zero

Слайд 20





Error message - Example
Traceback (most recent call last):
  File "C:/my_python/test.py", line 2, in <module>
    x/(49**0.5 - 7)
ZeroDivisionError: float division by zero
Описание слайда:
Error message - Example Traceback (most recent call last): File "C:/my_python/test.py", line 2, in <module> x/(49**0.5 - 7) ZeroDivisionError: float division by zero

Слайд 21





Error message - Example
Traceback (most recent call last):
  File "C:/my_python/test.py", line 2, in <module>
    x/(49**0.5 - 7)
ZeroDivisionError: float division by zero
Описание слайда:
Error message - Example Traceback (most recent call last): File "C:/my_python/test.py", line 2, in <module> x/(49**0.5 - 7) ZeroDivisionError: float division by zero

Слайд 22





But what if we do want to mix types?
my_apples = 3
print('I have ' + my_apples + ' apples')
TypeError: Can't convert 'int' object to str implicitly
The error message tells us we have tried to convert an int to a str but we cannot do this implicitly. 
So let’s do it explicitly.
Описание слайда:
But what if we do want to mix types? my_apples = 3 print('I have ' + my_apples + ' apples') TypeError: Can't convert 'int' object to str implicitly The error message tells us we have tried to convert an int to a str but we cannot do this implicitly. So let’s do it explicitly.

Слайд 23





Converting types (casting)
int, float and str are not only names of types but also names of functions which convert between types.
Example : 
str(5)  '5'
float(5)  5.0
int('5')  5
Описание слайда:
Converting types (casting) int, float and str are not only names of types but also names of functions which convert between types. Example : str(5)  '5' float(5)  5.0 int('5')  5

Слайд 24





Converting types – int(),float()
Converts string representing numbers to the represented numbers
int('5')5
Cannot  convert strings not representing an int :
int('5.5'), int('Hello')
Converts float to int by rounding the number down.
int(5.9)5
Converts string representing numbers to the represented numbers
float('5.5')5.5
Cannot  convert strings not representing a float:
float('Hello')
Converts int to float by treating it as a round number.
float(5)5.0
Описание слайда:
Converting types – int(),float() Converts string representing numbers to the represented numbers int('5')5 Cannot convert strings not representing an int : int('5.5'), int('Hello') Converts float to int by rounding the number down. int(5.9)5 Converts string representing numbers to the represented numbers float('5.5')5.5 Cannot convert strings not representing a float: float('Hello') Converts int to float by treating it as a round number. float(5)5.0

Слайд 25





User input
To make a program interactive we can ask the user for some inputs and act upon them. 
The function input(s) :
Prints to the screen s
Halts the program execution and waits for the user to insert some input and press enter
Return a string representing the user’s input
Описание слайда:
User input To make a program interactive we can ask the user for some inputs and act upon them. The function input(s) : Prints to the screen s Halts the program execution and waits for the user to insert some input and press enter Return a string representing the user’s input

Слайд 26





User input - Example
square_side = input('Insert side length: ') 
# Wait for user …
3
- The value of square_side is 3
area = square_side * square_side 
Will this work?
Описание слайда:
User input - Example square_side = input('Insert side length: ') # Wait for user … 3 - The value of square_side is 3 area = square_side * square_side Will this work?

Слайд 27





User input - Example
area = square_side * square_side 
'3'*'3'
TypeError: can't multiply sequence by non-int of type 'str'
Input returns a string, and we can’t multiply string by string. So what do we do? Convert types
Описание слайда:
User input - Example area = square_side * square_side '3'*'3' TypeError: can't multiply sequence by non-int of type 'str' Input returns a string, and we can’t multiply string by string. So what do we do? Convert types

Слайд 28





User input - Example
square_side = float(input('Insert side length: '))
# Wait for user …
3
area = square_side * square_side 
The value of area is 9.0
Описание слайда:
User input - Example square_side = float(input('Insert side length: ')) # Wait for user … 3 area = square_side * square_side The value of area is 9.0

Слайд 29





Functions with input
def function_name(param1, param2,…,paramN):
	#indented code is here
	#as usual
Описание слайда:
Functions with input def function_name(param1, param2,…,paramN): #indented code is here #as usual

Слайд 30





Functions with input
When we call a function with input parameters, we can use the parameters’ value inside the function using their name.
Описание слайда:
Functions with input When we call a function with input parameters, we can use the parameters’ value inside the function using their name.

Слайд 31





Functions with input
Описание слайда:
Functions with input

Слайд 32





Functions with input
Описание слайда:
Functions with input

Слайд 33





Functions with input
Описание слайда:
Functions with input

Слайд 34





A word about scopes
Описание слайда:
A word about scopes

Слайд 35





A word about scopes
Описание слайда:
A word about scopes

Слайд 36





A word about scopes
Описание слайда:
A word about scopes

Слайд 37





A word about scopes
Описание слайда:
A word about scopes

Слайд 38





A function with more than 1 input
def get_details(name, password):
print('Name is :' + name + ', Password is:' + password )
get_details('John', '1234')
Описание слайда:
A function with more than 1 input def get_details(name, password): print('Name is :' + name + ', Password is:' + password ) get_details('John', '1234')

Слайд 39





Functions’ parameters default value
Sometimes ….
A function has an obvious use case that will be utilized most of the time
You have prepared a good option for the user but don’t want to force her to use it 
In such cases, you can define a default value to the function’s parameters. A value that will be used if no other value is specified.
Описание слайда:
Functions’ parameters default value Sometimes …. A function has an obvious use case that will be utilized most of the time You have prepared a good option for the user but don’t want to force her to use it In such cases, you can define a default value to the function’s parameters. A value that will be used if no other value is specified.

Слайд 40





Functions’ parameters default value
def shoresh(number, root=2):
print(number ** (1/root))
The first parameter, number, has no default value. Hence every call to the function must indicate its value. 
The second parameter, root, has a default value. Hence if we don’t indicate its value it will get the default declared value, 2.
Описание слайда:
Functions’ parameters default value def shoresh(number, root=2): print(number ** (1/root)) The first parameter, number, has no default value. Hence every call to the function must indicate its value. The second parameter, root, has a default value. Hence if we don’t indicate its value it will get the default declared value, 2.

Слайд 41





Functions’ parameters default value
def shoresh(number, root=2):
print(number ** (1/root))
shoresh(64) # Here we didn’t indicate the second variable, hence the default value was used
>> 8
shoresh(64, 3) # Here we indicated the second variable, hence its value was used and not the default
>> 4
Описание слайда:
Functions’ parameters default value def shoresh(number, root=2): print(number ** (1/root)) shoresh(64) # Here we didn’t indicate the second variable, hence the default value was used >> 8 shoresh(64, 3) # Here we indicated the second variable, hence its value was used and not the default >> 4

Слайд 42





Function’s return value
Many times we want functions to not only perform some functionality, but also to return a result. 
Using the return keyword, a function is able to return a value.
Описание слайда:
Function’s return value Many times we want functions to not only perform some functionality, but also to return a result. Using the return keyword, a function is able to return a value.

Слайд 43





Function’s return value
def always_return_5():
	return 5
	print('hi')
Описание слайда:
Function’s return value def always_return_5(): return 5 print('hi')

Слайд 44





Function’s return value
def always_return_5():
	return 5
	print('hi')
print(3 + always_return_5())
>>> 8
Описание слайда:
Function’s return value def always_return_5(): return 5 print('hi') print(3 + always_return_5()) >>> 8

Слайд 45





Function calling a function
We can use the return value of one function as another function’s input.

def per_week(per_day=1):
   return per_day * 7

def per_year(how_many_per_week):
   return how_many_per_week * 52

print('Apples per year : ' + str(per_year(per_week())))
What happens here?
Описание слайда:
Function calling a function We can use the return value of one function as another function’s input. def per_week(per_day=1): return per_day * 7 def per_year(how_many_per_week): return how_many_per_week * 52 print('Apples per year : ' + str(per_year(per_week()))) What happens here?

Слайд 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 how_many_per_week * 52

print('Apples per year : ' + str(per_year(per_week())))
per_week()is called with no value and so gets the default value, 1, hence its return value is 7.
Описание слайда:
Function calling a function def per_week(per_day=1): return per_day * 7 # return 7 def per_year(how_many_per_week): return how_many_per_week * 52 print('Apples per year : ' + str(per_year(per_week()))) per_week()is called with no value and so gets the default value, 1, hence its return value is 7.

Слайд 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 how_many_per_week * 52 # return 364


print('Apples per year : ' + str(per_year(7)))
per_year()is called with the value 7 and so returns the value 364
Описание слайда:
Function calling a function def per_week(per_day=1): return per_day * 7 # return 7 def per_year(how_many_per_week): return how_many_per_week * 52 # return 364 print('Apples per year : ' + str(per_year(7))) per_year()is called with the value 7 and so returns the value 364

Слайд 48





Function calling a function
We can use the return value of one function as another function’s input.
def per_week(per_day =1):
   return per_day * 7 # return 7

def per_year(how_many_per_week):
    return how_many_per_week * 52 # return 364


print('Apples per year : ' + str(per_year(7)))
    
>>> Apples per year : 364
Описание слайда:
Function calling a function We can use the return value of one function as another function’s input. def per_week(per_day =1): return per_day * 7 # return 7 def per_year(how_many_per_week): return how_many_per_week * 52 # return 364 print('Apples per year : ' + str(per_year(7))) >>> Apples per year : 364

Слайд 49





Multiple outputs functions
To return more than one value, separate return values by comma
def diff_and_ratio(num1, num2):
    return num1-num2, num1/num2
diff, ratio = diff_and_ratio(1, 5)
print(diff)
print(ratio)
Описание слайда:
Multiple outputs functions To return more than one value, separate return values by comma def diff_and_ratio(num1, num2): return num1-num2, num1/num2 diff, ratio = diff_and_ratio(1, 5) print(diff) print(ratio)

Слайд 50





None
None is a special value which is used to represent absence of value.
Every function which does not return value explicitly, return None implicitly.
Описание слайда:
None None is a special value which is used to represent absence of value. Every function which does not return value explicitly, return None implicitly.

Слайд 51





None - example
def print_hi():
   print('hi')
x = print_hi() # x is assigned 		the value None
print(x)
>>>hi
>>>None
Описание слайда:
None - example def print_hi(): print('hi') x = print_hi() # x is assigned the value None print(x) >>>hi >>>None

Слайд 52





The Boolean type
Like int, str and float , Boolean is another Python type.
Boolean can get only one of two values : 
True
False 
type(True)
>>> <class 'bool'>
Описание слайда:
The Boolean type Like int, str and float , Boolean is another Python type. Boolean can get only one of two values : True False type(True) >>> <class 'bool'>

Слайд 53





Boolean expressions
Boolean expressions are expressions which use Boolean operators to evaluate a value of True or False.
For example > is a Boolean operator. Its Boolean evaluation is “Is the object on the right larger than the object on the left?”
5 > 7  is a Boolean expression because it uses a Boolean operator. Its value is False.
Описание слайда:
Boolean expressions Boolean expressions are expressions which use Boolean operators to evaluate a value of True or False. For example > is a Boolean operator. Its Boolean evaluation is “Is the object on the right larger than the object on the left?” 5 > 7 is a Boolean expression because it uses a Boolean operator. Its value is False.

Слайд 54





Boolean operators
Описание слайда:
Boolean operators

Слайд 55





Boolean expressions
7 == 4  ?
(7 != 2) == (5 > 4)  ?
type(5 > 7) == type(8 < 3)  ?
Описание слайда:
Boolean expressions 7 == 4  ? (7 != 2) == (5 > 4)  ? type(5 > 7) == type(8 < 3)  ?

Слайд 56





Boolean expressions
7 == 4  False
(7 != 2) == (5 > 4)  True
type(5 > 7) == type(8 < 3)  True
Описание слайда:
Boolean expressions 7 == 4  False (7 != 2) == (5 > 4)  True type(5 > 7) == type(8 < 3)  True

Слайд 57





Complex Boolean operators
Take few Boolean operators and evaluate a new Boolean value from them.
and and or evaluate 2 Boolean expressions
not evaluates 1 Boolean expression
The return value of complex Boolean operators could be represented in a Truth table – a table that lists al the combination of truth value of input variables and their evaluated output
Описание слайда:
Complex Boolean operators Take few Boolean operators and evaluate a new Boolean value from them. and and or evaluate 2 Boolean expressions not evaluates 1 Boolean expression The return value of complex Boolean operators could be represented in a Truth table – a table that lists al the combination of truth value of input variables and their evaluated output

Слайд 58





Complex Boolean operators 
Truth table
Описание слайда:
Complex Boolean operators Truth table

Слайд 59





Conditional operation
We do not always want to execute all the lines in our code. Sometimes we want to execute some lines only if a certain condition is maintained.
For example : Divide 9 by user’s input.
We get the number from the user. 
Only if the number is different than 0, we can divide 9 by it.
Описание слайда:
Conditional operation We do not always want to execute all the lines in our code. Sometimes we want to execute some lines only if a certain condition is maintained. For example : Divide 9 by user’s input. We get the number from the user. Only if the number is different than 0, we can divide 9 by it.

Слайд 60





Conditional operation - if
How do we implement this notion in Python?
if boolean_expression:
#Code to perform if the #boolean_expression is True
#(Note the indentation under the if 
#block).
Описание слайда:
Conditional operation - if How do we implement this notion in Python? if boolean_expression: #Code to perform if the #boolean_expression is True #(Note the indentation under the if #block).

Слайд 61





Conditional operation - if
For example : 
num = float(input('Insert a number‘))
if num != 0 :
print(9/num)
But what if the number does equal 0? We still want to let the user know.
Описание слайда:
Conditional operation - if For example : num = float(input('Insert a number‘)) if num != 0 : print(9/num) But what if the number does equal 0? We still want to let the user know.

Слайд 62





Conditional operation - if
num = float(input('Insert a number'))
if num != 0 :
    print(9/num)
if num == 0 :
    print('Cannot divide by 0')
This is not a natural way to present our intention. What we would usually say is : if  the number is different than 0 divide, else print some message to the user.
Python lets us use such structure using the else keyword.
Описание слайда:
Conditional operation - if num = float(input('Insert a number')) if num != 0 : print(9/num) if num == 0 : print('Cannot divide by 0') This is not a natural way to present our intention. What we would usually say is : if the number is different than 0 divide, else print some message to the user. Python lets us use such structure using the else keyword.

Слайд 63





Conditional operation - else
num = float(input('Insert a number'))
if num != 0 :
    print(9/num)
else:
    print('Cannot divide by 0')
else should appear directly under an if block with the same indention.
Описание слайда:
Conditional operation - else num = float(input('Insert a number')) if num != 0 : print(9/num) else: print('Cannot divide by 0') else should appear directly under an if block with the same indention.

Слайд 64





Conditional operation - elif
And what if we had some more options to choose from?
If condition1 then result1, 
if not, than if condition2 then result2
…
if not, than if conditionN then resultN
If none of the above then result_Final

Use elif!  (=else if)
Описание слайда:
Conditional operation - elif And what if we had some more options to choose from? If condition1 then result1, if not, than if condition2 then result2 … if not, than if conditionN then resultN If none of the above then result_Final Use elif! (=else if)

Слайд 65





Conditional operation - elif
if now == 'Morning':
    print('Good morning!')
elif now == 'Noon':
    print('Good noon')
else:
    print('It must be evening')
The first elif should appear directly under an if block with the same indention. 
As many elif’s as you wish can follow.
elif can be terminated by a single else, or not at all.
Описание слайда:
Conditional operation - elif if now == 'Morning': print('Good morning!') elif now == 'Noon': print('Good noon') else: print('It must be evening') The first elif should appear directly under an if block with the same indention. As many elif’s as you wish can follow. elif can be terminated by a single else, or not at all.

Слайд 66





Nested if
What operations could be included inside an if block? Any operations we like :
print
input
… and – another if!
An if inside another if is called nested if – it opens a new block with its own indentation.
Описание слайда:
Nested if What operations could be included inside an if block? Any operations we like : print input … and – another if! An if inside another if is called nested if – it opens a new block with its own indentation.

Слайд 67





Nested if - example
if now == 'morning':
    if 'y' == input('Are you hungry?'):
		print('Bon appetit!')
    else:
		print('Some other time than')
elif now == 'Noon':
    print('Good noon')
else:
    print('Good night')
Описание слайда:
Nested if - example if now == 'morning': if 'y' == input('Are you hungry?'): print('Bon appetit!') else: print('Some other time than') elif now == 'Noon': print('Good noon') else: print('Good night')

Слайд 68





Nested if - example
if now == 'morning':
    if 'y' == input('Are you hungry?'):
		print('Bon appetit!')
    else:
		print('Some other time than')
elif now == 'Noon':
    print('Good noon')
else:
    print('Good night')
Описание слайда:
Nested if - example if now == 'morning': if 'y' == input('Are you hungry?'): print('Bon appetit!') else: print('Some other time than') elif now == 'Noon': print('Good noon') else: print('Good night')

Слайд 69





split()
The method split() returns a list of all the words in the string, using a given string as the separator (default is whitespace)
# a = 'hello';  b = 'world'
>>> a,b = 'hello world'.split()
# a = 'hell'; b = 'w'; c = 'rld'
>>> a,b,c = 'hello world'.split('o')
Описание слайда:
split() The method split() returns a list of all the words in the string, using a given string as the separator (default is whitespace) # a = 'hello'; b = 'world' >>> a,b = 'hello world'.split() # a = 'hell'; b = 'w'; c = 'rld' >>> a,b,c = 'hello world'.split('o')

Слайд 70





Example
Calculate the circumference (היקף) of a circle or square according to user request.
Let’s break the problem into parts : 
1.	Get user input
2.	Validate if is it either a circle or a rectangle
If it is not print an error message and do not continue
3(a). If it is a circle
Ask for the radius, calculate circumference
3 (b). If it is a square
Ask for the side’s length, calculate circumference
4.	Report to user the calculated result
Описание слайда:
Example Calculate the circumference (היקף) of a circle or square according to user request. Let’s break the problem into parts : 1. Get user input 2. Validate if is it either a circle or a rectangle If it is not print an error message and do not continue 3(a). If it is a circle Ask for the radius, calculate circumference 3 (b). If it is a square Ask for the side’s length, calculate circumference 4. Report to user the calculated result

Слайд 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 call the function to run the program:
error_safe_circumference()
Описание слайда:
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 call the function to run the program: error_safe_circumference()

Слайд 72


Basic programming, слайд №72
Описание слайда:

Слайд 73


Basic programming, слайд №73
Описание слайда:

Слайд 74





Summary
Today we have learned :
How to use variable
What are types and how to convert between them
How to receive an input from a user
How to use functions which get input and return output
Conditional operation : if, elif, else
Описание слайда:
Summary Today we have learned : How to use variable What are types and how to convert between them How to receive an input from a user How to use functions which get input and return output Conditional operation : if, elif, else



Теги Basic programming
Похожие презентации
Mypresentation.ru
Загрузить презентацию