In :
# http://math-info.hse.ru/
# http://python.math-hse.info/
In :
import os
print(os.getcwd())
C:\Users\student
In :
# Задача
# Дан список
# Хочу: список их квадратов squares

numbers = [1, 2, 15, 3]

squares = []
for number in numbers:
squares.append(number ** 2)
# DON'T:
# squares = squares + [number ** 2]

assert squares == [1, 4, 15 ** 2, 9]
In :
numbers = [1, 2, 15, 3]
squares = [number ** 2 for number in numbers]
assert squares == [1, 4, 15 ** 2, 9]
$$\{x^2 \mid x \in \mathbb N\}$$
In :
passwd = input("Enter password")
if passwd == correct_password:
print("Access granted")
print("Okay")
else:
print("The end")
Access granted
Okay
The end
In :
passwd
Out:
'123456'
In :
Out:
'123456'
In :
Out:
True
In :
type(True)
Out:
bool
In :
5 > 7
Out:
False
In :
"6512".isdigit()
Out:
True
In :
"-6512".isdigit()
Out:
False
In :
x = int(input("Enter number"))
if x > 1000:
print("x is large")
elif x > 100:
print("x is not so large")
else:
print("x is small")
Enter number56
x is small
In :
x = int(input("Enter large even number"))
if x > 1000:
if x % 2 == 0:
print("Okay")
else:
print("x is not even")
else:
print("x is not large")
Enter large even number54
x is not large
In :
x = int(input("Enter large even number"))
if x > 1000 and x % 2 == 0:
print("okay")
else:
print("not okay")
Enter large even number45678
okay
In :
True and True
Out:
True
In :
True and False
Out:
False
In :
True or False
Out:
True
In :
not False
Out:
True
In :
1/0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-24-05c9758a9c21> in <module>()
----> 1 1/0

ZeroDivisionError: division by zero
In :
(1 > 2) and (1 / 0 < 7)
Out:
False
In :
(1 / 0 < 7) and (1 > 2)
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-26-3e8d2bc5a309> in <module>()
----> 1 (1 / 0 < 7) and (1 > 2)

ZeroDivisionError: division by zero
In :
to_be = False
to_be or not to_be
Out:
True
In :
0.1 + 0.2 == 0.3
Out:
False
In :
0.1 + 0.2
Out:
0.30000000000000004
In :
from decimal import Decimal
In :
Decimal("0.1") + Decimal("0.2") == Decimal("0.3")
Out:
True
In :
Decimal("0.1")
Out:
Decimal('0.1')
In :
Decimal(0.1)
Out:
Decimal('0.1000000000000000055511151231257827021181583404541015625')
In :
from fractions import Fraction as Fr
In :
Fr(1, 2) + Fr(1, 3)
Out:
Fraction(5, 6)
In :
%whos
Variable           Type       Data/Info
---------------------------------------
Decimal            type       <class 'decimal.Decimal'>
Fr                 ABCMeta    <class 'fractions.Fraction'>
Fraction           ABCMeta    <class 'fractions.Fraction'>
decimal            module     <module 'decimal' from 'C<...>conda3\\lib\\decimal.py'>
number             int        3
numbers            list       n=4
os                 module     <module 'os' from 'C:\\Pr<...>\\Anaconda3\\lib\\os.py'>
passwd             str        123456
squares            list       n=4
to_be              bool       False
x                  int        45678
In :
a = Fraction(1, 3)
b = Fraction(2, 7)
In :
type(a)
Out:
fractions.Fraction
In :
a > b
Out:
True
In :
a / b
Out:
Fraction(7, 6)
In :
print("Incorrect password, try again")
print("Access granted")
Incorrect password, try again
Incorrect password, try again
Incorrect password, try again
Incorrect password, try again
Incorrect password, try again
Incorrect password, try again
Incorrect password, try again
Access granted
In :
while True:
break
print("Incorrect password, try again")
print("Access granted")
Incorrect password, try again
Incorrect password, try again
Access granted
In :
authenticated = False
for i in range(3):
authenticated = True
break
print("Incorrect password, try again")
if authenticated:
print("Access granted")
else:
Access granted
$$ax^2 + bx +c =0$$
In :
from math import sqrt
In :
a = 1
b = -5
c = 6
D = b ** 2 - 4 * a * c
sqrt_D = sqrt(D)
x1 = (-b + sqrt_D) / (2 * a)
x2 = (-b - sqrt_D) / (2 * a)
print(x1, x2)
3.0 2.0
In :
def quadratic_roots(a, b, c):
D = b ** 2 - 4 * a * c
sqrt_D = sqrt(D)
x1 = (-b + sqrt_D) / (2 * a)
x2 = (-b - sqrt_D) / (2 * a)
return (x1, x2)
In :
Out:
(1.0, -6.0)
In :
def plus_1(x):
print("Invoked plus_odin")
print("x =", x)
return x + 1
In :
x = (plus_1(4) + plus_1(6)) * plus_1(3)
Invoked plus_odin
x = 4
Invoked plus_odin
x = 6
Invoked plus_odin
x = 3
In :
def print_hello(name):
print("Hello,", name)
In :
def return_hello(name):
return "Hello, " + name
In :
s = print_hello("Harry")
Hello, Harry
In :
print(s)
None
In :
s = return_hello("Harry")
In :
s
Out:
'Hello, Harry'
In :
greetings = return_hello("Harry") + "!"
In :
greetings
Out:
'Hello, Harry!'
In :
print_hello("Harry")
Hello, Harry
In :
return_hello("Harry")
Out:
'Hello, Harry'
In :
x = 12
y = 75
def foo(x):
y = x ** 2
return y
print(foo(1))
print("x =", x)
print("y =", y)
1
x = 12
y = 75
In :
def bar(x):
return x + y
In :
y = 14
print(bar(7))
y = 98
print(bar(7))
21
105
In :
y = 99
def bar(x):
z = x + y
y = 5
return z
In :
y = 7
print(bar(0))
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-84-ae6a23964982> in <module>()
1 y = 7
----> 2 print(bar(0))

<ipython-input-83-5456c48e7e00> in bar(x)
1 y = 99
2 def bar(x):
----> 3     z = x + y
4     y = 5
5     return z

UnboundLocalError: local variable 'y' referenced before assignment
In :
def change_x():
global x
x = x + 1
In :
x = 5
change_x()
print(x)
change_x()
print(x)
6
7
In :
def hello(last_name, first_name, title="Dr."):
print("Hello, ", title, first_name, last_name)
In :
hello("Potter", "Harry",  "Mr.")
Hello,  Mr. Harry Potter
In :
hello("Potter", "Harry")
Hello,  Dr. Harry Potter
In :
hello(first_name="Harry",
last_name="Potter")
Hello,  Dr. Harry Potter
In :
hello(title="Mr.",
first_name="Harry",
last_name="Potter")
Hello,  Mr. Harry Potter
In :
def hello(last_name, first_name="Ivan", title="Dr."):
print("Hello, ", title, first_name, last_name)
In :
hello("Ivanov", title="Prof.")
Hello,  Prof. Ivan Ivanov
In :
x = 1 + \
2
In :
x
Out:
3
In :
x = (1 +
2)
In :
x = 128768757.
y = 128768757.
x is y
Out:
False
In :
def foo(x, y=None):
if y is not None:
return x * y
else:
return x
In :
foo(4)
Out:
4
In :
foo(4, 2)
Out:
8
In :
def foo(x):
print("Hello")
def foo(x, y):
print("World")
In :
foo(7)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-113-afa180a57233> in <module>()
----> 1 foo(7)

TypeError: foo() missing 1 required positional argument: 'y'
In :
def foo(x):
return bar(x) * 2
def bar(x):
return x + 1
In :
foo(5)
Out:
12
In [ ]: