In [1]:
# http://math-info.hse.ru/
# http://python.math-hse.info/
In [2]:
import os
print(os.getcwd())
C:\Users\student
In [4]:
# Задача
# Дан список 
# Хочу: список их квадратов 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 [5]:
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 [7]:
correct_password = "123456"
passwd = input("Enter password")
if passwd == correct_password:
    print("Access granted")
    print("Okay")
else:
    print("Access denied")
print("The end")
Enter password123456
Access granted
Okay
The end
In [8]:
passwd
Out[8]:
'123456'
In [9]:
correct_password
Out[9]:
'123456'
In [10]:
passwd == correct_password
Out[10]:
True
In [11]:
type(True)
Out[11]:
bool
In [12]:
5 > 7
Out[12]:
False
In [13]:
"6512".isdigit()
Out[13]:
True
In [14]:
"-6512".isdigit()
Out[14]:
False
In [15]:
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 [18]:
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 [19]:
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 [20]:
True and True
Out[20]:
True
In [21]:
True and False
Out[21]:
False
In [22]:
True or False
Out[22]:
True
In [23]:
not False
Out[23]:
True
In [24]:
1/0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-24-05c9758a9c21> in <module>()
----> 1 1/0

ZeroDivisionError: division by zero
In [25]:
(1 > 2) and (1 / 0 < 7)
Out[25]:
False
In [26]:
(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 [28]:
to_be = False
to_be or not to_be
Out[28]:
True
In [29]:
0.1 + 0.2 == 0.3
Out[29]:
False
In [30]:
0.1 + 0.2 
Out[30]:
0.30000000000000004
In [32]:
from decimal import Decimal
In [33]:
Decimal("0.1") + Decimal("0.2") == Decimal("0.3")
Out[33]:
True
In [34]:
Decimal("0.1")
Out[34]:
Decimal('0.1')
In [35]:
Decimal(0.1)
Out[35]:
Decimal('0.1000000000000000055511151231257827021181583404541015625')
In [38]:
from fractions import Fraction as Fr
In [39]:
Fr(1, 2) + Fr(1, 3)
Out[39]:
Fraction(5, 6)
In [40]:
%whos
Variable           Type       Data/Info
---------------------------------------
Decimal            type       <class 'decimal.Decimal'>
Fr                 ABCMeta    <class 'fractions.Fraction'>
Fraction           ABCMeta    <class 'fractions.Fraction'>
correct_password   str        123456
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 [41]:
a = Fraction(1, 3)
b = Fraction(2, 7)
In [42]:
type(a)
Out[42]:
fractions.Fraction
In [43]:
a > b
Out[43]:
True
In [44]:
a / b
Out[44]:
Fraction(7, 6)
In [45]:
correct_password = "12345"
password = input("Enter password")
while password != correct_password:
    print("Incorrect password, try again")
    password = input("Enter password")
print("Access granted")
Enter passwordjhkhl
Incorrect password, try again
Enter passwordkljhlkjg
Incorrect password, try again
Enter passwordhlkjhlkjh
Incorrect password, try again
Enter passwordlkglkg
Incorrect password, try again
Enter passwordgglkjglkg
Incorrect password, try again
Enter passwordkljhlkjgl
Incorrect password, try again
Enter passwordlkhglg
Incorrect password, try again
Enter password12345
Access granted
In [47]:
correct_password = "12345"
while True:
    password = input("Enter password")
    if password == correct_password:
        break
    print("Incorrect password, try again")
print("Access granted")
Enter passwordhgjg
Incorrect password, try again
Enter passwordkljlkg
Incorrect password, try again
Enter password12345
Access granted
In [50]:
correct_password = "12345"
authenticated = False
for i in range(3):
    password = input("Enter password")
    if password == correct_password:
        authenticated = True
        break
    print("Incorrect password, try again")
if authenticated:
    print("Access granted")
else:
    print("Access denied")
Enter password12345
Access granted
$$ax^2 + bx +c =0$$
In [51]:
from math import sqrt
In [52]:
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 [53]:
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 [55]:
quadratic_roots(1, 5, -6)
Out[55]:
(1.0, -6.0)
In [60]:
def plus_1(x):
    print("Invoked plus_odin")
    print("x =", x)
    return x + 1
In [61]:
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 [62]:
def print_hello(name):
    print("Hello,", name)
In [63]:
def return_hello(name):
    return "Hello, " + name
In [66]:
s = print_hello("Harry")
Hello, Harry
In [68]:
print(s)
None
In [69]:
s = return_hello("Harry")
In [70]:
s
Out[70]:
'Hello, Harry'
In [71]:
greetings = return_hello("Harry") + "!"
In [72]:
greetings
Out[72]:
'Hello, Harry!'
In [73]:
print_hello("Harry")
Hello, Harry
In [74]:
return_hello("Harry")
Out[74]:
'Hello, Harry'
In [76]:
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 [80]:
def bar(x):
    return x + y
In [81]:
y = 14
print(bar(7))
y = 98
print(bar(7))
21
105
In [83]:
y = 99
def bar(x):
    z = x + y
    y = 5
    return z
In [84]:
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 [85]:
def change_x():
    global x
    x = x + 1
In [86]:
x = 5
change_x()
print(x)
change_x()
print(x)
6
7
In [89]:
def hello(last_name, first_name, title="Dr."):
    print("Hello, ", title, first_name, last_name)
In [97]:
hello("Potter", "Harry",  "Mr.")
Hello,  Mr. Harry Potter
In [98]:
hello("Potter", "Harry")
Hello,  Dr. Harry Potter
In [99]:
hello(first_name="Harry", 
      last_name="Potter")
Hello,  Dr. Harry Potter
In [100]:
hello(title="Mr.",
      first_name="Harry", 
      last_name="Potter")
Hello,  Mr. Harry Potter
In [101]:
def hello(last_name, first_name="Ivan", title="Dr."):
    print("Hello, ", title, first_name, last_name)
In [102]:
hello("Ivanov", title="Prof.")
Hello,  Prof. Ivan Ivanov
In [103]:
x = 1 + \
    2
In [104]:
x
Out[104]:
3
In [105]:
x = (1 +
     2)
In [108]:
x = 128768757.
y = 128768757.
x is y
Out[108]:
False
In [109]:
def foo(x, y=None):
    if y is not None:
        return x * y
    else:
        return x
In [110]:
foo(4)
Out[110]:
4
In [111]:
foo(4, 2)
Out[111]:
8
In [112]:
def foo(x):
    print("Hello")
def foo(x, y):
    print("World")
In [113]:
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 [119]:
def foo(x):
    return bar(x) * 2
def bar(x):
    return x + 1
In [118]:
foo(5)
Out[118]:
12
In [ ]: