42
, of type int
.3.1415
, 9e15
, of type float
.True
, False
, of type bool
.print(7**77)
print(2/3)
print(2//3)
print(2%3)
print(2./3.)
print(2.//3.)
118181386580595879976868414312001964434038548836769923458287039207 0.6666666666666666 0 2 0.6666666666666666 0.0
'spam'
, "guido's"
, """multi-lines string"""
, of type str
.(1, 'spam', 4, 'U' )
.[1, [2, 'three'], 4]
, of type list
.{'a': 'val', 3: 'x', 'key': 124}
, of type dict
.None
, of type NoneType
.=
¶a = 10
b, c = 3.14, "hello"
b, c = c, b # swap values
print(a,c)
print(b)
10 3.14 hello
==
¶a, b, c = 10, 3.14, "hello"
print(a==10)
print(3.14==b)
print(c==c)
print(a=="10")
True True True False
list1 = [1, 2, 3]
list2 = list1 # both point to the same collection
list2[1] = 20
print(list1)
print(list2)
[1, 20, 3] [1, 20, 3]
list2 = [100, 200, 300] # list2 points to a new collection
print(list1)
print(list2)
[1, 20, 3] [100, 200, 300]
#
.¶# assign values to a, b, c
a, b, c = 2, 6, 2
print(a,b,c)
2 6 2
# nested tests
if a > 1:
print("a>1")
if b > 5 and c == 2:
print("b>5 and c==2")
a>1 b>5 and c==2
a, b = 1, 2
if a > b:
print('a>b')
elif a == b:
print('a==b')
else:
print('a<b')
a<b
a, max = 1, 5
while a < max:
print(a)
a += 1
1 2 3 4
for elem in [ 'a', 'b', 'c']:
print(elem)
a b c
for elem in range(4):
print(elem)
0 1 2 3
for elem in range(4):
if elem == 2:
continue
print(elem)
0 1 3
for elem in range(6):
if elem == 2:
break
print(elem)
0 1
def divs(x,y):
"""Try / and //."""
res1 = x/y
res2 = x//y
return res1, res2
print(divs(2.,3.))
print(divs(2,3))
(0.6666666666666666, 0.0) (0.6666666666666666, 0)
help(divs)
Help on function divs in module __main__: divs(x, y) Try / and //.
print(divs)
<function divs at 0x7fb24e5780e0>
myprint = print
myprint("Hello world !")
Hello world !
key, value = "length", 10
fmt = "{}: {}"
text = fmt.format(key,value)
print(text)
length: 10
with open("dummy.txt",'w') as file:
file.write("{} {}\n".format(1,2))
file.write("{}\n".format(3))
with open("dummy.txt",'r') as file:
for line in file:
line = line.rstrip('\n')
print(line)
1 2 3
import random
rand_number = random.randint(1, 100)
print(rand_number)
19
:
at the end of if/while/for conditions.