# http://math-info.hse.ru/
# http://python.math-hse.info/
import os
print(os.getcwd())
C:\Users\student
# Задача
# Дан список
# Хочу: список их квадратов 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]
numbers = [1, 2, 15, 3]
squares = [number ** 2 for number in numbers]
assert squares == [1, 4, 15 ** 2, 9]
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
passwd
'123456'
correct_password
'123456'
passwd == correct_password
True
type(True)
bool
5 > 7
False
"6512".isdigit()
True
"-6512".isdigit()
False
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
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
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
True and True
True
True and False
False
True or False
True
not False
True
1/0
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) <ipython-input-24-05c9758a9c21> in <module>() ----> 1 1/0 ZeroDivisionError: division by zero
(1 > 2) and (1 / 0 < 7)
False
(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
to_be = False
to_be or not to_be
True
0.1 + 0.2 == 0.3
False
0.1 + 0.2
0.30000000000000004
from decimal import Decimal
Decimal("0.1") + Decimal("0.2") == Decimal("0.3")
True
Decimal("0.1")
Decimal('0.1')
Decimal(0.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')
from fractions import Fraction as Fr
Fr(1, 2) + Fr(1, 3)
Fraction(5, 6)
%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
a = Fraction(1, 3)
b = Fraction(2, 7)
type(a)
fractions.Fraction
a > b
True
a / b
Fraction(7, 6)
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
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
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
from math import sqrt
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
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)
quadratic_roots(1, 5, -6)
(1.0, -6.0)
def plus_1(x):
print("Invoked plus_odin")
print("x =", x)
return x + 1
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
def print_hello(name):
print("Hello,", name)
def return_hello(name):
return "Hello, " + name
s = print_hello("Harry")
Hello, Harry
print(s)
None
s = return_hello("Harry")
s
'Hello, Harry'
greetings = return_hello("Harry") + "!"
greetings
'Hello, Harry!'
print_hello("Harry")
Hello, Harry
return_hello("Harry")
'Hello, Harry'
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
def bar(x):
return x + y
y = 14
print(bar(7))
y = 98
print(bar(7))
21 105
y = 99
def bar(x):
z = x + y
y = 5
return z
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
def change_x():
global x
x = x + 1
x = 5
change_x()
print(x)
change_x()
print(x)
6 7
def hello(last_name, first_name, title="Dr."):
print("Hello, ", title, first_name, last_name)
hello("Potter", "Harry", "Mr.")
Hello, Mr. Harry Potter
hello("Potter", "Harry")
Hello, Dr. Harry Potter
hello(first_name="Harry",
last_name="Potter")
Hello, Dr. Harry Potter
hello(title="Mr.",
first_name="Harry",
last_name="Potter")
Hello, Mr. Harry Potter
def hello(last_name, first_name="Ivan", title="Dr."):
print("Hello, ", title, first_name, last_name)
hello("Ivanov", title="Prof.")
Hello, Prof. Ivan Ivanov
x = 1 + \
2
x
3
x = (1 +
2)
x = 128768757.
y = 128768757.
x is y
False
def foo(x, y=None):
if y is not None:
return x * y
else:
return x
foo(4)
4
foo(4, 2)
8
def foo(x):
print("Hello")
def foo(x, y):
print("World")
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'
def foo(x):
return bar(x) * 2
def bar(x):
return x + 1
foo(5)
12