print('Hello World')
Hello World
pi = 3.14159265359 # decimal
name = 'Philipp' # text
age = 31 # integer
sky_is_blue = True # boolean
print(pi)
3.14159265359
x, y = 10, 5
print(x)
10
print(y)
5
pi = 'Philipp'
print(pi)
Philipp
x, y = y, x
x
5
print(non_existent_variable)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-11-5ebcdcc221cf> in <module>() ----> 1 print(non_existent_variable) NameError: name 'non_existent_variable' is not defined
ten_millions = 10_000_000
ten_millions
10000000
small = .25
small
0.25
sci_thousand = 10e2
sci_thousand
1000.0
counter = 0
pricey_car = 'Mercedes'
income = 120_000
class = 'Mercedes'
File "<ipython-input-2-2daee69c2fcb>", line 1 class = 'Mercedes' ^ SyntaxError: invalid syntax
pricey@@car
File "<ipython-input-3-ffd8cc768d68>", line 1 pricey@@car ^ SyntaxError: invalid syntax
1y_income = 120_000
File "<ipython-input-4-2b923f9c6b91>", line 1 1y_income = 120_000 ^ SyntaxError: invalid syntax
pi = 3.14159265359 # reassing it back
type(pi)
float
type(name)
str
type(age)
int
type(sky_is_blue)
bool
A = 6
B = 5
A + B
11
A - B
1
A / B
1.2
A * B
30
Exponent
2**3
8
3**2
9
Integer division
10 / 2
5.0
9 // 4 # way faster
2
10.0 // 4
2.0
2.0
Reminer
10 % 3
1
count = 0
count +=1
count
1
count -=1
count
0
count +=1
count *=2
count
2
count **= 2
count
4
count /=2
count
2.0
count //= 2
count
1.0
10.0 // 4
2.0
(2 + 10) / 2
6.0
10 / (1 + 1)
5.0
text1 = 'This is a so-called “speakeasy”'
text2 = "This is Sam’s Tavern"
text3 = ''' This is Sam’s Tavern.
"Welcome everyone!" - is written on the door.
'''
print('Hello\nWorld!')
Hello World!
'Hello ' + 'World'
'Hello World'
'Hello' * 3
'HelloHelloHello'
'Hello World'.upper()
'HELLO WORLD'
'Hello World'.lower()
'hello world'
'hello world'.title()
'Hello World'
'Hello world'.replace('world', 'planet')
'Hello planet'
'hello'.rjust(10, ' ')
' hello'
'hello'.ljust(10, ' ')
'hello '
'999'.zfill(10)
'0000000999'
Format method
'Hello {} world and our blue {}'.format('beautiful','planet')
'Hello beautiful world and our blue planet'
'{0} {2} {1} and our {2} {3}!'.format('Hello','World','Beautiful', 'Planet')
'Hello Beautiful World and our Beautiful Planet!'
'Hello {adj} world!'.format(adj='beautiful')
'Hello beautiful world!'
F-strings
adj = 'beautiful'
f'Hello {adj} world!'
'Hello beautiful world!'
name = 'pHILIPP'
f'Hello mr {name.title()}'
'Hello mr Philipp'
Legacy formatting (do not use)
name = 'David'
print('Hello mr. %s' % name)
Hello mr. David
Formatting mini-language
pct = .345
value = 45500
f'Price grew by {pct:.1%} or {value:,}'
'Price grew by 34.5% or 45,500'
"Hello World"[0]
'H'
"Hello World"[0:5]
'Hello'
"World" in "Hello World!"
True
"Planet" in "Hello World!"
False
'World' == 'World'
True
pi == pi
True
"World" in "Hello World!"
True
pi != pi
False
not (5 > 4)
False
(5 > 4) | (6 < 5)
True
(5 > 4) & (6 < 5)
False
(5 > 4) ^ (5 < 6)
False
float("2.5")
2.5
int("45")
45
int(4.521)
4
float(5)
5.0
int(True)
1
float(False)
0.0
str(True)
'True'
bool(0)
False
bool('Hello')
True
bool(115.5)
True
Can't convert
int("2.5")
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-135-b44af7edf778> in <module>() ----> 1 int("2.5") ValueError: invalid literal for int() with base 10: '2.5'
int("Hello")
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-136-b6a8c1f9fea6> in <module>() ----> 1 int("Hello") ValueError: invalid literal for int() with base 10: 'Hello'
people = 2
days = 7
plane_ticket_pp = 180
hotel_price_day = 85
meal_pp = 13
total_budget = 1000
total_travel = people * plane_ticket_pp
total_hotel = hotel_price_day * days
total_meal = 3 * days * people
total = total_travel + total_hotel + total_meal
exceeded = total > total_budget
exceeded
False
report = f'Estimated travel price is ${total:,} and represents {(total / total_budget):%} of existing travel budget.\nEstimate exceeds travel budget: {exceeded}.'
print(report)
Estimated travel price is $997 and represents 99.700000% of existing travel budget. Estimate exceeds travel budget: False.