Python basic grammar (1)

2018.05.05_day11
1. There are two ways to execute a Python script. The
first is to enter "python file name" on the command line.
The second is to open the script file from the interpreter and run it.
2. Briefly describe the relationship between bits and bytes A
bit is the smallest unit of computer storage. A bit is either "0" or "1", and 8 bits are equal to a byte.
3. Briefly describe the relationship between ascii, unicode, utf-8, and gbk.
ASCII code is mainly used to process English characters, but Chinese cannot be processed.
unicode means that a character can be represented by at least 16 bits.
How much can be expressed in utf-8 is how much can be expressed, saving memory.
Two bytes of gbk represent
4. Please write the number of bits occupied by "Li Jie" encoded in utf-8 and gbk respectively.
UTF-8, 6*8=48 bits, 1 byte in English, 3 bytes in Chinese
GBK, 4*8=32 bits, regardless of Chinese and English, 2 bytes

#GBK is compatible after expansion based on the national standard GB2312 The standard of GB2312 (it seems that it is not a national standard). GBK encoding is specially used to solve Chinese encoding, which is double-byte. Both Chinese and English are double-byte.
#UTF-8 encoding is a multi-byte encoding used to solve international characters. It uses 8 bits (that is, one byte) for English and 24 bits (three bytes) for Chinese. For forums with more English characters, use UTF-8 to save space. In addition, if foreigners visit your GBK webpage, you need to download Chinese language pack support. This problem does not occur when visiting UTF-8 encoded web pages. can be accessed directly.
#GBK contains all Chinese characters; UTF-8 contains characters that all countries in the world need to use.
#People often ask which code is better to write UTF-8 or GBK for web pages. According to personal needs, if you mainly develop Chinese programs and your customers are mainly Chinese, you should use GBK, because UTF-8 encoded Chinese uses three bytes, saving space with GBK.
#If you do English website development, use utf-8, because utf-8 only occupies one byte in Chinese and English. GBK Chinese and English are also two bytes, and foreign customers need to download language packs to access GBK.
#If your website is in Chinese, but there are many foreign users, it is best to use UTF-8.
#UTF-8 encoded text can be displayed on various browsers that support UTF8 character set in various countries.
#For example, if it is UTF8 encoding, Chinese can also be displayed on English IE of foreigners without the need for them to download the Chinese language support package of IE. Therefore, for forums with more English, each character occupies 2 bytes when using GBK, but only one byte when using UTF-8 English.
#UTF8 is an international code, its versatility is better, foreigners can also browse the forum, GBK is a national code, and its versatility is worse than UTF8, but the database occupied by UTF8 is larger than GBK~

5, Python single-line comments and multi-line comments respectively for what?
Use "#"
for a single line and triple quotation marks for multiple lines (single and double can be used)
6. What are the precautions for declaring variables?
1. The variable name can only include: letters, numbers, underscores
2, cannot start with a number
3, cannot be a keyword.
4. Do not repeat with built-in things (class name, method name)
7. If the following variable n1 = 5, please use the method provided by int to get the minimum number of binary bits that the variable can be represented by?
n1.bit_length()
8. What are the Boolean values?
True/False
9. Read the code, please write the execution result
a="alex"
b=a.capitalize()#capitalize() returns the first letter uppercase, the rest are lowercase strings
print(a)
print(b)
output result:
alex
Alex
10 , write code, there are the following variables, please implement each function as required
name = "aleX"
a. Remove the spaces on both sides of the value corresponding to the name variable, and enter the removed content
name.lstrip().rstrip()# lstrip() returns a string with spaces at the beginning removed, and rstrip() returns a string with the end of the string removed
. b. Determine whether the value corresponding to the name variable starts with "al", and output the result
name.starswith("al")# satarswith("X") determines whether it starts with "X" and returns True/False, and endswith("X") determines whether it ends with "X".
c. Determine whether the value corresponding to the name variable ends with "X"
name.endswith("X")
d. Replace the "l" in the value corresponding to the name variable with "p"
name.replace("l", "p ")
e. Split the value corresponding to the name variable according to "l", and output the result
print(name.split("l"))
f. Excuse me, what type of value is obtained after splitting by e in the previous question?
list
g.
print(name.upper())
h. Lowercase the value corresponding to the name variable and output the result
print(name.lower())
i. Please output the second character of the value corresponding to the name variable
print(name[1] )
j. Please output the first 3 characters of the value corresponding to the name variable
print(name[0:3])
k. Please output the last 3 characters of the value corresponding to the name variable
print(name[-3:])
k.1 Reverse the name and output
print(name[::-1])
l. Please output the index position of "e" in the value corresponding to the name variable
print(name.index("e"))
m. Get the subsequence, only Does not include the last character
print(name[:-1])
21. Is a string an iterable object? If possible please use a for loop for each element?
for item in name
print("item")
22. Please use code to implement:

23. What is the difference between range in Python2 and range in Python3?
The range in Python 2 is created in memory at runtime, and in 3, it is created in memory when it is used, saving memory.
24. Implement an integer addition calculator:
such as:
25. Calculate how many decimal integers are there in the content entered by the user? How many letters?

test = input( " Please input: " )
char_count = 0
int_count = 0
for item in test:
    if item.isalpha():
        char_count = char_count+1
    elif item.isdigit():
        int_count = int_count+1
 print ( "The number of characters is: " ,char_count)
 print ( "The number of numbers is: " ,int_count)

26. Briefly describe the relationship between numbers such as int and 9, and strings such as str and "xxoo"?
int/str represents the type, 9/"xxoo" is a specific value
27. Make a fun template program
Requirement: wait for the user to input name, location, hobby, realization
For example: Dear xxx, I like to do xxx in xxx place the most

name = input( " Please enter your name: " )
dress = input( " Please enter where you like to go: " )
hoppy = input( " Please enter your hobby: " )
 print ( " Dear " +name+ " , " + " Favorite in " +dress+ " Dry " +hoppy)

28. Make random verification codes, case insensitive

def check_code():
    import random
    checkcode = ""
    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)
        checkcode += str(temp)
    return checkcode
while True:
    code = check_code()
    print("________________")
    print(code)
    user_original_code = input( " Please enter the verification code: " )
    user_code = user_original_code.upper()
     if user_code== code:
         print ( "The verification code is correct! " )
         print ( " ________________ " )
         break 
    else :
         print ( "The verification code is not entered correctly! " )
         print ( " ________________ " )

 

29. Develop a filter program for sensitive words, prompting the user to input content, if the user input content contains special characters:
such as "Teacher Cang", the content will be replaced by "***"

info = input( " Please enter a paragraph: " )
special_word = [ " Teacher Cang " , " Tokyo " ]
 print ( " Sentence before conversion: " ,info)
 for item in special_word:
     if info.find(item)!= -1 :
        substitute = "*"*len(item)
        info = info.replace(item,substitute)
 print ( "The converted sentence: " ,info)

 

 

30. Make a form
Prompt the user to input in a loop: user name, password, email (the length of the user input should not exceed 20, and only the first 20 will be taken if the user enters it).
If the user enters q or Q, it means that the user is not inputting, and the user input content is printed in a table.

user_str_sum=""
while True:
    quitgame = input( " Enter Q or q to exit, and press any key to continue: " )
     if quitgame != " q "  and quitgame != " Q " :
        name = input( " Please enter your name: " )[0:20 ]
        pw = input( " Please enter the password: " )[0:20 ]
        email = input( " Please enter the email address: " )[0:20 ]
        user_str =name+"\t"+pw+"\t"+email+"\t"+"\n"
        user_str_sum = user_str_sum+user_str
    else:
        break
print(user_str_sum.expandtabs(20))

 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325382920&siteId=291194637