python learning series day3-python basics

4.19
String and internal functions
Once a string is created, it cannot be modified.
Jobs:
1. Two ways to execute Python script
1.exe + file name
2.python

Relationship 2, briefly bits, bytes of
an 8 bit byte

3. Briefly describe the relationship between ascii, unicode, utf-8, gbk
ASCII: 2 bytes
unicode: representation of all languages
utf8: compressed version of Unicode, one Chinese character 3 bytes
gbk: one Chinese character 2 bytes
len in python3 calculates characters, python2 means calculate bytes

4. Please write "Li Jie". The number of digits occupied by utf-‐8 and gbk encoding is
6 bytes = 48 bits
4 bytes

5. What do Pyhton single-line comments and multi-line comments use?
#
'' '' '' / "" "" ""

6. What are the precautions for declaring variables?
Numbers are underlined, numbers do not start, keywords are generally not recommended

7. If there is a variable n1 = 5, please use the method provided by int. How many binary digits can be used to represent the variable?
3 / bit_length

8. What are the Boolean values?
True and False

9. Read the code, please write the execution result
a = "alex"
b = a.capitalize ()
print (a)
print (b)
Please write the output result:
alex
Alex

10. Write code with the following variables, please implement each function according to the requirements
name = "aleX"

a. Remove the space around the value corresponding to the name variable, and enter the content after removal

name = “aleX”
v = name.strip()
print(v)

b. Determine whether the value corresponding to the name variable starts with "al" and output the result

name = “aleX”
v = name.startswith(‘al’)
print(v)

c. Determine whether the value corresponding to the name variable ends with "X" and output the result

name = “aleX”
v = name.endswith(‘X’)
print(v)

d. Replace "l" in the value corresponding to the name variable with "p", and output the result

name = “aleX”
v = name.replace(‘l’,‘p’)
print(v)

e. Divide the value corresponding to the name variable according to "l" and output the result.

name = “aleX”
v = name.split(‘l’)
print(v)

f. Excuse me, what type of value (optional) is obtained after splitting in the previous question?
List

g. Capitalize the value corresponding to the name variable and output the result

name = “aleX”
v = name.upper()
print(v)

h. Lowercase the value corresponding to the name variable and output the result

name = “aleX”
v = name.lower()
print(v)

i. Please output the second character of the value corresponding to the name variable?

name = “aleX”
v = name[1]
print(v)

j. Please output the first 3 characters of the value corresponding to the name variable?

name = “aleX”
v = name[0:3]
print(v)

k. Please output the last 2 characters of the value corresponding to the name variable?

	name = "aleX"
	v = name[-2:]
	print(v)

l. Please output the index position of "e" in the value corresponding to the name variable?

name = “aleX”
v = len(name)
for i in range(v):
if name[i] == ‘e’:
print(i,name[i])
else:
continue

m. Get the subsequence, only the last character is not included. Such as: oldboy gets oldbo; root gets roo

name = “aleX”
print(name[0:-1])

21. Is the string an iterable object? If possible, please use for loop for each element?
Iterable object = can be iterated by for loop to get
for i in something:
print (i)
class: str, int, ,,
object: created according to class

22. Please use the code to implement:
a. Use underscore to stitch each element of the list into a string, li = "alexericrain"
v = '_'. Join (li)
b. Use underscore to stitch each element of the list into characters String, li = ['alex', 'eric', 'rain'] (optional)

li = [‘alex’, ‘eric’, ‘rain’]
v= ‘_’.join(li)
print(v)

23. What is the difference between the range in Python2 and the range in Python3?
Created immediately in python2, iterated in python3, only created when used otherwise expressed in range () form

24. Implement an integer addition calculator:
such as: content = input ('Please enter content:')

For example: 5 + 9 or 5+ 9 or 5 + 9

name = ‘5+9’
v1,v2 = name.split(’+’)
v1 = int(v1)
v2 = int(v2)
print(v1+v2)

25. Calculate how many decimals are there in user input? How many letters?
For example: content = input ('Please enter content:')
#For example: asduiaf878123jkjsfd-‐213928

content = input('请输入内容:计算十进制小数个数和字母个数 ')
num = 0
zimu = 0
for n in content:
    if n.isdecimal() == True:
        num+=1
    elif n.isalpha() == True:
        zimu+=1
    else:
        pass
print ('数字个数 ',(num))
print ('字母个数',zimu)

26. Briefly describe the relationship between numbers such as int and 9 and strings such as str and "xxoo"?
Classes and objects

27. Make fun template program
Requirements: Wait for the user to enter the name, location, and hobbies, and carry out any reality according to the user's name and hobbies. For
example: love and love xxx, most like doing xxx in xxx
test = "respect and love {0 }, Favorite to do {2} at {1} place
name = input ("please enter your name:")
place = input ("please enter the place you often go to:")
like = input ("please enter you Normal hobbies: ")
v = test.format (name, place, like)
print (v)

28. Make a random verification code, not case sensitive.
Process:
-
user program execution
-
the display to enter a verification code to the user
-
user-entered value
and indicates the user input values are the same realistic correct information; otherwise, continue to generate random codes continues to wait for user input
generates a random verification Code code example:

 def check_code():
    import random
    check_code = ''
    for i in range(4):
        current = random.randrange(0, 4)
        if current != i:
            temp = chr(random.randint(65, 90))
        else:
            temp = random.randint(0, 9)
        check_code += str(temp)
    return check_code
while True:
    code = check_code()
    print (code)
    v = input('请输入验证码>>>>')
    v1 = v.upper()
    if v1 == code:
        print ('验证码正确')
        break
    else:
        print('输入错误')
        break

29. Develop a sensitive word filtering program to prompt the user to enter content. If the user input contains special characters:
such as
"Cang Teacher" and
"Tokyo Hot", replace the content with


shuju = input ('Please enter characters')
shuju = shuju.replace ('Cang teacher', '***')
print (shuju)

30. Make a form to prompt the user to input: user name, password, mailbox (requires the user to enter a length of no more than 20 characters, if it exceeds, then only the first 20 characters are valid)
If the user enters q or Q, it means no more input, will User input is hidden in the form of a table

s = ""
while True:
    v1 = input('请输入你的名字')
    v2 = input('请输入你的密码')
    v3 = input('请输入你的邮箱')
    template = "{0}\t{1}\t{2}\n"
    v = template.format(v1,v2,v3)
    s = s + v
    if v1 == "q" or v2 == "q" or v3 == "q" or v1 == "Q" or v2 == "Q" or v3 == "Q":
        break
print(s.expandtabs(20))
Published 4 original articles · praised 0 · visits 61

Guess you like

Origin blog.csdn.net/weixin_45583352/article/details/105638898