2 + 3
5
(2 + 5) * 3 + 7
28
4 / 2
2.0
1 * 2
2
4 / 3
1.3333333333333333
x = 12
y = 32
4 + x
16
x
12
x = 15
x
15
x = x + 1
x
16
%whos
Variable Type Data/Info -------------------------------------- black_reformat function <function black_reformat at 0x10d511820> x int 16 y int 32
3
3
3.0
3.0
s = "Hello, world!"
s
'Hello, world!'
s = 'Корпорация "Майкрософт"'
s
'Корпорация "Майкрософт"'
s = "It's me"
s
"It's me"
type(s)
str
type("Hello")
str
type(2)
int
type(2.0)
float
x = float("12.34")
x
12.34
x + 0.2
12.54
"12.34" + 0.2
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-32-690faa7a9d66> in <module> ----> 1 "12.34" + 0.2 TypeError: can only concatenate str (not "float") to str
float("Hello")
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-33-ff6885467a56> in <module> ----> 1 float("Hello") ValueError: could not convert string to float: 'Hello'
s = input("Как вас зовут? ")
print("Здравствуйте,", s)
Как вас зовут? Илья Здравствуйте, Илья
print(1, 2, 3)
1 2 3
print("Hello", "World")
Hello World
# Напишите код, который запрашивает с клавиатуры целое число
# и возвращает его, умноженное на два
# например, если пользователь ввёл 3, программа должна напечатать 6
number = input("Введите число: ")
print(number * 2)
Введите число: 123 123123
numberber
'123'
a = "Hello"
b = "World"
a + b
'HelloWorld'
"Hello" * 3
'HelloHelloHello'
user_input = input("Введите число: ")
number = int(user_input)
print(number * 2)
Введите число: 12 24
TypeError: 'str' object is not callable
input = "Hello!"
input
'Hello!'
user_input = input("Введите число")
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-46-75b2586fd7d5> in <module> ----> 1 user_input = input("Введите число") TypeError: 'str' object is not callable
3 + 2
5
sqrt(10)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-48-0c2ba68b944d> in <module> ----> 1 sqrt(10) NameError: name 'sqrt' is not defined
import math
math.sqrt(10)
3.1622776601683795
import math as m
m.sqrt(10)
3.1622776601683795
from math import sqrt
sqrt(10)
3.1622776601683795
input
'Hello!'
del input
input
<bound method Kernel.raw_input of <ipykernel.ipkernel.IPythonKernel object at 0x10d424fd0>>
x
12.34
del x
x
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-60-6fcf9dfbd479> in <module> ----> 1 x NameError: name 'x' is not defined
Числа Фибоначчи: $$x_1=1,\quad x_2=1,\quad x_{k+1} = x_k + x_{k-1}, \quad k = 2, 3, \ldots$$
$$1, 1, 2, 3, 5, 8, 13,\ldots$$prev = 1
cur = 1
new = cur + prev
print(new)
prev = cur
cur = new
610
Пример.
x = 1
x = x * 2
print(x)
2048
z = 12