Похожие презентации:
Переменные. Простые примеры (лекция 1)
1. Переменные
Лекция 12.
3. Простые примеры
>>> print('Hello world!')Hello world!
>>> print 'Hello world!‘
SyntaxError
4. Простые примеры
>>> x = 6>>> print(x)
6
>>> y = x * 7
>>> print(y)
42
5. Типы ошибок
СинтаксическиеЛогические
Семантические
6. Что не так с этим кодом?
>>> primt 'Hello world!'File "", line 1
primt 'Hello world!'
^ SyntaxError: invalid syntax
7. Упражнение
Where in the computer is a variable such as “x”stored after the following Python line finishes?
x = 123
a) Central processing unit
b) Main Memory
c) Secondary Memory
d) Input Devices
e) Output Devices
8. Упражнение
What will the following program print out:x = 43
x=x+1
print(x)
a) 43
b) 44
c) x + 1
d) Error because x = x + 1 is not possible
mathematically
9. Значения и типы
>>> print(4)4
>>> print(1,000,000)
100
10. Упражнение
>>> message = 'And now for somethingcompletely different‘
>>> n = 17
>>> pi = 3.1415926535897931
>>> print(n)
17
>>> print(pi)
3.141592653589793
11. Имена переменных
>>> 76trombones = 'big parade'SyntaxError: invalid syntax
>>> more@ = 1000000
SyntaxError: invalid syntax
>>> class = 'Advanced Theoretical Zymurgy'
SyntaxError: invalid syntax
12. Зарезервированные слова
13. Операторы
>>>print(1)>>>x = 2
>>>print(x)
12
14. Операторы и операнды
20+32hour-1
hour*60+minute
minute/60
5**2
(5+9)*(15-7)
15. Операторы и операнды
>>> minute = 59>>> minute/60
0.9833333333333333
>>> minute//60
0
16. Порядок операндов
Exponentiation has the next highest precedence, so2**1+1 is 3, not 4, and 3*1**3 is 3, not 27
Multiplication and Division have the same
precedence, which is higher than Addition and
Subtraction, which also have the same precedence.
So 2*3-1 is 5, not 4, and 6+4/2 is 8, not 5
Operators with the same precedence are evaluated
from left to right. So the expression 5-3-1 is 1, not 3,
because the 5-3 happens first and then 1 is subtracted
from 2
17. Операторы по модулю
>>> quotient = 7 // 3>>> print(quotient)
2
>>> remainder = 7 % 3
>>> print(remainder)
1
18. Операторы работы со строками
>>> first = 10>>> second = 15
>>> print(first+second)
25
>>> first = '100'
>>> second = '150'
>>> print(first + second)
100150
>>> first = 'Test '
>>> second = 3
>>> print(first * second)
Test Test Test
19. Ввод
>>> inp = input()Some silly stuff
>>> print(inp)
Some silly stuff
>>> name = input('What is your name?\n')
What is your name?
Chuck
>>> print(name)
Chuck
20. Ввод
>>>prompt = 'What is the airspeed velocity of anunladen swallow?\n'
>>>speed = input(prompt)
What is the airspeed velocity of an unladen
swallow?
17
>>> int(speed)
17
>>> int(speed) + 5
22
21. Комментарии
>>>v = 5 # assign 5 to v22. Упражнение
Write a program that uses input to prompt a userfor their name and then welcomes them.
Enter your name: Chuck
Hello Chuck!
23. Упражнение
Write a program to prompt the user for hours andrate per hour to compute gross pay.
Enter Hours: 35
Enter Rate: 2.75
Pay: 96.25
24. Упражнение
Assume that we execute the following assignmentstatements:
width = 17
height = 12.0
For each of the following expressions, write the value of
the expression and the type (of the value of the
expression).
width//2
width/2.0
height/3
1+2*5