x = 10
7 // 3
2
7 % 3
1
s = 'Hello, \nHow are \tyou?'
print(s)
Hello, How are you?
s[0]
s[8]
'H'
# slice
s[3:6]
'lo,'
s[3:]
'lo, \nHow are \tyou?'
s[:6]
'Hello,'
s.find('asd')
-1
s.split(',')
['Hello', ' \nHow are \tyou?']
s.split()
['Hello,', 'How', 'are', 'you?']
xs = [10,20,30,40]
xs[2]
30
xs[1:3]
[20, 30]
len(s)
21
len(xs)
4
# if statement
# 6 comparisons: <, >, ==, !=, <=, >=
a = 10
if a > 10:
print('more than 10')
else:
print('less than or equal to 10')
less than or equal to 10
a = 5
if a > 10:
print('more than 10')
score = 79
if score >= 90:
print('A')
elif score >= 80 and score < 90:
print('B')
elif score >= 70 and score < 80:
print('C')
elif score >= 60 and score < 70:
print('D')
else:
print('F')
C
# for-loop
xs = [10,20,30,40]
for x in xs:
print(x)
10 20 30 40
xs = [10,20,30,40]
# accumulator pattern
sum = 0
for x in xs:
sum = sum + x
print(sum)
100
xs = [10,20,30,40]
# accumulator pattern
sum = 1
for x in xs:
sum = sum * x
print(sum)
240000
def name_of_month(month):
s = ['January','February','March']
return s[month-1]
name_of_month(2)
'February'
print("Helo",end='')
print(' how')
Helo how