Python Basics Exercises 1

Source: Internet
Author: User

Python Basics Exercises 1

1. Two ways to execute Python scripts

Answer: command line execution, editor execution

2, briefly describe the relationship between bits and bytes

A : Byte byte bit: bit, 1 bytes = 8 bits

3. brief description of the relationship between ASCII, Unicode, Utf-8,GBK


ASCII is the first United States to use the standard Information Interchange code, the case of all the letters, the various symbols in binary notation, a total of 256, adding some Latin characters, 1bytes represents a character,

Unicode is to unify the world language of the use, unified with 2 bytes represents a character, can express 2**16=65556, called the Universal Language, features: fast, but wasted space, can be used in memory processing, compatible with UTF-8,GBK,ASCII,

Utf-8 in order to change this disadvantage of Unicode, the provision of 1 English characters with 1 bytes, 1 Chinese characters with 3 bytes, characteristics, space-saving, slow, use in hard disk data transmission, network data transmission, compared to hard disk and network speed, not reflected,

GBK is a Chinese character encoding, with 2 bytes representing one character)

4. please write "Li Jie" the number of digits that are encoded by Utf-8 and GBK respectively.

Answer: In UTF-8 a English account of 1 bytes, a Chinese account for 3 bytes, where "Li Jie" in 6 bytes GBK One Chinese accounted for 2 bytes, where "Li Jie" accounted for 4 characters.



5. What are the Python single-line comments and multiline comments used separately?


single-line comment with #

Multiline comment with three single or three double quotation marks "' Annotated content '

6. What are the considerations for declaring variables?

A: Variables consist of numbers, letters, underscores, cannot start with a number, cannot use keywords, cannot use python built-in

7, If there is a variable n1 = 5, use the provided method of int, how many bits can be used to get the variable at least?


8, what are the Boolean values respectively?

Answer: True False

9, read the code, please write out the results of a = "ABCD"

b = A.capitalize ()

Print (a)

Print (b)

Please write out the results:

Answer: ABCD (capitalize) is a first-letter uppercase function

10, write code, there are the following variables, please follow the requirements of the implementation of each function name = "AleX"

A. Remove the space on both sides of the value corresponding to the name variable and enter the content after removal

Answer:AleX (Strip) removes the specified string

B. Determine if the value corresponding to the name variable starts with "Al" and outputs the result

Answer: True (StartsWith) with what to start with

c. Determine if the value corresponding to the name variable ends with "X" and outputs the result

Answer: True (EndsWith) with what what end

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

Answer:ApeX (replace) replaces the specified string with the specified string

E. the value corresponding to the name variable is split according to "L" and outputs the result.

Answer: [' A ', ' EX '] (split) is split by the specified string without preserving the specified string

(' A ', ' l ', ' EX ') (partition) is divided by the specified string to preserve the specified string and split into three parts

F. What is the type (optional) to get the value after the E-partition of the previous question ?

Answer: list, tuple

G. capitalize the value corresponding to the name variable and output the result

Answer: ALEX (upper) convert to uppercase

H. lowercase the value corresponding to the name variable and output the result

A :Alex (lower) converted to write

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

Answer:L (v = name[1])

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

Answer:ale (v = name[0:3])

K. Please output the first 2 characters of the value corresponding to the name variable ?

Answer:eX (v = name[-2:])

L. Please output the value of the name variable corresponding to the index where "E" is located?

Answer:2 (v = name.index (' e '))

M. Gets a subsequence that does not contain the last character. such as: Oldboy to obtain the oldbo; Root gets the roo

Answer:le (v = name[1:3])

21. can a string iterate over an object? Can I use a for loop for each element?

Answer: You can

' testtest '  for inch Test:     Print (i)

22, please use the code to achieve:

A. using underscores to stitch each element of a list into a string,li = "Alexericrain"


" Alexericrain "  "_". Join (LI)print(v)

B. Use an underscore to stitch each element of the list into a string, Li = [' Axx ', ' Eric ', ' Rain '] (optional)


" [' Axx ', ' Eric ', ' Rain '] "  = li[3:7'_'. Join (i)print(v)

23. What is the difference between range in Python2 and range in Python3?


The range in Python2 is stored directly in memory

The range in Python2 returns a list that creates the content directly

Range in Python3 is required for the for loop to be stored in memory

The range in Python3 returns an iteration value that is created only one time for a for loop

24. Implement an integer addition calculator such as: content = input (' Please enter content: ') # such as: 5+9 or 5+ 9 or 5 + 9


  first method: I  = input ( '   Please enter a few plus:  "  ) value  = Iv1,v2  = Value.split ( " +   " ) v1  = int (v1) v2  =  int (v2)  print  (V1+v2) The second method: Content  = input ("   Please enter the content:   "  print   (eval (content))  #  eval: evaluates the string str as a valid expression and returns the result of the calculation  

25. How many decimal decimals are included in the calculation of user input? A few letters? Such as:

Content = input (' Please enter content:') # for example:asduiaf878123jkjsfd--‐213928


 content = input ( "  Please enter:  "  ) n  = 0s  = 0  for  i in  Span style= "COLOR: #000000" > content:  print   (i)  if          I.isdecimal (): n  + = 1 if   I.isalpha (): s  + = 1print  ( '   number of decimal digits:  " , N,  "  number of letters:   ", s) 

26. Describe the relationship between Int and 9 and the string such as STR and "Xxoo".

answer:both int and STR are classes

9 is an int class object

Xxoo is an object of the Str class

27. Create Fun Template Program

requirements: Waiting for the user to enter the name, place, hobby, according to the user's name and hobbies to carry out arbitrary reality such as: love and amiable xxx, favorite in XXX place dry xxx


' respectable mom {0}, favorite {1} '  = input ('>>>'= input ('>>>') = Template.format (name,hobby)print (v) 

28, the production of random verification code, not case-sensitive. Process:

--‐ User Execution Program

--‐ Show the user the verification code that needs to be entered

--‐ User-entered values

The user enters the value and displays the value at the same time the actual correct information; otherwise continue to generate random verification code continues to wait for the user input generated random CAPTCHA code example:


defCheck_code ():ImportRandom Checkcode="'     forIinchRange (4): current= Random.randrange (0, 4)        ifCurrent! =i:temp= Chr (Random.randint (65, 90))        Else: Temp= Random.randint (0, 9) Checkcode+=Str (temp)returnCheckcode whileTrue:code=Check_code ()Print(code) v= Input ('>>>')    ifv = =code.lower ():Print('The verification code is correct:', V) Break    Else:        Print('Verification Code Error re-enter!')

29, develop the sensitive word filter program, prompting the user to enter content, if the user entered the content contains special characters: such as "Cang teacher" "Tokyo Hot", then replace the content with * * *


' Ms. Cang, Tokyo Hot, 1231 Tokyo Heat 231 Warehouse teacher '  = v.replace (' cang teacher ','* * * *' = V.replace ( ' Tokyo Hot ','* * *')print(v)

30. Production Form

Circular Prompt user input: User name, password, mailbox ( requires the user to enter a length of not more than three characters, if more than the first 20 characters are valid) if the user input q means no further input, Hide user input in tabular form


s ="' whileTrue:v1= Input ('>>') V2= Input ('>>') v3= Input ('>>') Template='{0}\t{1}\t{2}\n'v=Template.format (v1, V2, v3) s= S +v i= Input ('Q Exit >>')    ifi = ='Q':         BreakPrint(S.expandtabs (20))

Python Basics Exercises 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.