1. Basic
1. String can use "" or ''
print(“Hello World”);
print(‘Hello World’);
2.
\t (tab)
\n (new line)
* 3. escape *
Two, string operations
firstVariable='hello world'
print(firstVariable.upper())
print(firstVariable.lower())
print(firstVariable.title())
Output:
2. Want to know what the method is
help(firstVariable.lower)
Output:
3. How to separate the strings
firstVariable='h e l l o world'
print(firstVariable.split(' '))#按照空格将字符串分隔
result:
4. Connect
a=['aaa','b','cs']
print(' '.join(a))#用空格把他们连接起来
Output:
5.
print('hello'+'world')
print('0'*3)
print('0'+'1')
Output:
6. Calculation
print(1+1)
print(130-2.0)
print(130-2)
print(130/2)
print(130.0/2)#取商
print(2*3)
print(2**3)#2的3次方
print(130%2)#取余
Output:
Three, if statement
If the statement after the if is true, execute
num=3
if num==3:
print("is 3")
result:
if True:
print("It is true")
if False:
print('Nothing is printed')
result:
num=3
if num>0 and num<15:
print('在区间')
num=3
if num>0 or num<15:
print('在区间')
The results are:
num=3
if not num<15:
print('在区间')
The result is empty
Four, Elif and Else statements
Elif statement: must be after the if statement. The elif statement allows you to check multiple expressions of True and execute the code block immediately when one of the conditions evaluates to True.
Similar to else, the elif statement is optional. However, unlike other cases, there can only be one statement at most, and any number of elif statements can follow the if.
Else statement: must be after the if or elif statement. There can be at most one other statement. It will be executed only when all the above "if" and "elif" statements are False
num=3
if num>3:
print(">3")
elif num<3:
print("<3")
else:print('=3')
Output:
dice_value = 1
if dice_value == 1:
print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 2:
print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 3:
print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 4:
print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 5:
print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 6:
print('You rolled a {}. Great job!'.format(dice_value))
else:
print('None of the conditions above (if elif) were evaluated as True')
Output:
Five, list
1、
z=[1,3,45,6]
print(z[1])
print(z[-1])
Result:
2. Split list
z=[1,3,45,6,11,22]
print(z[1:4])#从index为1取到index为4之前的元素(即不包括index为4的)
print(z[:3])#从最开始取3个元素
print(z[1:])#从index为1取到最后
Result:
3. Take the maximum, minimum, length, and sum of the list
z=[1,3,45,6,11,22]
print(min(z),max(z),len(z),sum(z))
Result:
4. Count the number of occurrences of objects in the list
z=[11,3,45,6,11,22]
print(z.count(4))
print(z.count(11))
Result:
5. Return pointer
z=[11,3,45,6,11,22,11,12,11,12,11]
print(z.index(11))#找到元素11的第一个index
print(z.index(11,1))#(a,b)从index为b的位置找到元素为a的第一个index
print(z.index(11,1,5))#从index1位置找到index到5(不包括5)
Results:
6, sort
x = [3, 7, 2, 11, 8, 10, 4]
y = ['Steve', 'Rachel', 'Michael', 'Adam', 'Monica', 'Jessica', 'Lester']
x.sort()
print(x)
y.sort()
print(y)
Result:
7, reverse sort
x = [3, 7, 2, 11, 8, 10, 4]
y = ['Steve', 'Rachel', 'Michael', 'Adam', 'Monica', 'Jessica', 'Lester']
x.sort(reverse=True)
print(x)
y.sort(reverse=True)
print(y)
result: