Python Basic Knowledge Summary

Source: Internet
Author: User

Python a.py directly invokes the Python interpreter execution file

CHOMD +x a.py./a.py #修改a. py file properties, for executable, in use./execute a.py File

2, briefly describe the relationship between bits and bytes

1bytes=8bit, 2**8=256, can represent changes in 256,

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 a character,

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

Utf-8 is 3 bytes=24bit.

GBK is 2bytes=16bit.

5, Pyhton single-line comments and multiline comments with what?

Single-line Comment #

Multiline comment "" "" "" three double quotes or three single quotes put the content to be interpreted in the middle, ""

6. What are the considerations for declaring variables?

Universal error avoidance rules, named after a lowercase letter, can be underlined, or numbers,

cannot be named with the Python keyword,

Can indicate the meaning of a variable

8. How do I see the address of a variable in memory?

Z=1
Print (ID (z))
9. What is the purpose of the automatically generated. pyc file when executing a Python program?

. PYC is a binary file after the py file is compiled for accelerated operation

10. Write code
A. Implement user input user name and password, when the user name is seven and the password is 123, the display login is successful, otherwise the login failed!

Name=input (' name: '). Strip ()
Pwd=input (' pwd: '). Strip ()
If name== ' seven ' and pwd== ' 123 ':
Print (' OK ')
Else
Print (' ERROR ')

B. Implement user input user name and password, when the user name is seven and the password is 123, the display login is successful, otherwise the login failed, the failure to allow repeated input three times
While loop:
Count=1
While count<=3:
Name = input (' name: '). Strip ()
PWD = input (' pwd: '). Strip ()

If name== ' seven ' and pwd== ' 123 ':
Print (' OK ')
Else
Print (' ERROR ')
Count + = 1
For loop:
For count in range (3):
Name = input (' name: '). Strip ()
PWD = input (' pwd: '). Strip ()

If name== ' seven ' and pwd== ' 123 ':
Print (' OK ')
Else
Print (' ERROR ')
Count + = 1
C. Implement user input user name and password, when the user name is seven or Alex and the password is 123, the display login is successful, otherwise the login failed, the failure to allow repeated input three times
While loop
Count =1
While count<=3:
Name = input (' Name: ')
PWD = input (' pwd: ')
If name = = ' Seven ' and pwd = = ' 123 ' or name = = ' Alex ' and pwd = = ' 123 ':
Print (' OK ')
else:
Print (' ERROR ')
Count+=1
For loop
For count in range (3)  :
Name = input (' Name: ')
PWD = input (' pwd: ')
If name = = ' Seven ' and pwd = = ' 123 ' or name = = ' Alex ' and pwd = = ' 123 ':
Print (' OK ')
else:
Print (' ERROR ')
Count+=1
11. Write code
A. Using a while loop to implement output 2-3+4-5+6...+100 and

count=2
Num=0
While count<=100:
If Count% 2 ==0:
Num=count+num
Count+=1
Else
Num=num-count
Count+=1
Print (num)

Num=0
Count=1
For count in range (100):
If count% 2 = = 0:
Num=num-count
else:
Num=num+count
Print (num)

C. Using a while loop to implement output 1,2,3,4,5, 7,8,9, 11,12

Count=1
While Count < 13:
If Count!=6:
If Count!=10:
Print (count)

Count+=1
D. Using a while loop to implement all odd numbers in output 1-100

For I in range (101):
If I%2! = 0:
Print (i)

Count=0
While count<100:
if count%2! = 0:
Print (count)
Count + = 1

E. Using a while loop to implement all the even numbers in output 1-100

For I in range (100):
if I%2 = = 0:
Print (i)

Count=0
While count<100:
If count%2 = = 0:
Print (count)
Count+=1

12, the binary representation of the digital 5,10,32,7 is written separately

Print (Bin (5)) binary   0b101
Print (Hex (10)) Hex  0x5
Print (Oct (5)) octal 0o5
14, the existing following two variables, please briefly describe what the relationship between N1 and N2? N1 = 123 N2 = 123

N1,N2 is a different variable name, but the values are the same, pointing to the same memory address,

16, the existing following two variables, please briefly describe what the relationship between N1 and N2? N1 = 123456 N2 = n1n2 assigned to N1. Point to the same memory address,

17, 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?

N1=5
Print (Int.bit_length (n1)) Results 3

18, what are the Boolean values respectively?

False. Ture, or 1 and 0

20, 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 to be removed
Print (Name.strip ())

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

Print (Name.startswith (' al '))
C. Determine if the value corresponding to the name variable ends with "X" and outputs the result
Print (Name.endswith (' x '))
D. Replace "L" in the value corresponding to the name variable with "P", and output the result

Print (Name.replace (' l ', ' P '))   
E. The value corresponding to the name variable is split according to "L" and outputs the result.

Print (Name.split (' e '))   [' Al ', ' x ']  list

G. Capitalize the value corresponding to the name variable and output the result
Print (Name.upper ())  print (Name.lower ())
I. Please output the 2nd 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[:3])
K. Please output the first 2 characters of the value corresponding to the name variable?
L. Please output the value of the name variable corresponding to the index where "E" is located?
Print (Name.index (' e '))
21. Can strings be iterated? If possible, use the for loop for each element?

Dependent indexes
Name = "Alex "
For I in range (len (name)):
Print (Name[i])

For I in Name:
Print (i)
22, please use the code to achieve: the use of an underscore each element of the list is stitched into a string, Li = [' Alex ', ' Eric ', ' Rain ']
Print (' _ '. Join ([' Alex ', ' Eric ', ' Rain '])    Alex_eric_rain
22, write code, such as the following table, according to the requirements of the implementation of every function Li = [' Alex ', ' Eric ', ' Rain ']
Li = [' Alex ', ' Eric ', ' Rain ']
Print (Len (LI))
B. Append the element "seven" to the list and output the added list
Li.append (' seven ')      
Print (LI)
[' Alex ', ' Eric ', ' Rain ', ' seven ']
C. Insert the element "Tony" in the 1th position of the list and output the added list
Li.insert (0, ' TONY ')
Print (LI)
D. Please modify the element in the 2nd position of the list to "Kelly" and output the modified list
li[1]= (' Killy ')
Print (LI)
E. Remove the Element "Eric" from the list and output the modified list
Li = [' Alex ', ' Eric ', ' Rain ']
Li.pop (1)
Print (LI) [' Alex ', ' rain ']
Deletion of names
Li = [' Alex ', ' Eric ', ' Rain ']
Li.remove (' Alex ')
Print (LI)
F. Delete the 2nd element in the list and output the value of the deleted element and the list after the element is deleted
Li = [' Alex ', ' Eric ', ' Rain ']
Print (Li.pop (1))
Print (LI)

Eric
[' Alex ', ' rain ']
H. Delete the 2nd to 4th element in the list and output the list after the element is deleted
Li = [' Alex ', ' Eric ', ' Rain ']
Del Li[1:]
Print (LI) K. Use the Enumrate output list element and ordinal (ordinal starting from 100)

Li = [' Alex ', ' Eric ', ' Rain ']
For k,v in Enumerate (li,1):
Print (K,V)
23, write code, such as the following table, please follow the functional requirements to achieve each function Li = ["Hello", ' Seven ', ["Mon", ["H", "Kelly"], ' all ', 123, 446]
A. Please output "Kelly"

Li = ["Hello", ' Seven ', ["Mon", ["H", "Kelly"], ' all '], 123, 446]
Print (li[2][1][1])

B. Please use the index to find the ' all ' element and modify it to "all"
li[2][2]= ' All '
Print (LI)
24, write code, there are the following tuple, according to the requirements of the implementation of each function Tu = (' Alex ', ' Eric ', ' Rain ')
A. Calculating the tuple length and outputting
Tu = (' Alex ', ' Eric ', ' Rain ')
Print (Len (TU))
B. Gets the 2nd element of a tuple and outputs

Print (tu[1])
E. Use the index of the for, Len, and range output tuples

For I in range (len):
Print (Tu.index (tu[i))
F. Use the Enumrate output element ancestor elements and ordinal number (ordinal starting from 10)

For k,v in Enumerate (tu,10):
Print (K,V)
Ten Alex
Eric
Rain

For k in Enumerate (tu,10):
Print (k)
(' Alex ')
(One, ' Eric ')
("Rain")


25, there are the following variables, please implement the required functions
Tu = ("Alex", [One, one, a, {"K1": ' V1 ', "K2": ["Age", "name"], "K3": (11,22,33)}, 44])
C. What is the value of the "K2" in the TU variable? Can it be modified? If so, add an element "Seven" to it.

Tuples, you cannot

D. What is the value of "K3" in the TU variable? Can it be modified? If so, add an element "Seven" to it.
List, you can
Tu = ("Alex", [One, one, a, {"K1": ' V1 ', "K2": ["Age", "name"], "K3": (11,22,33)}, 44])
res=tu[1][2][' K2 '].append (' seven ')

Print (TU)
26. Dictionaries
DiC = {' K1 ': "v1", "K2": "V2", "K3": [11,22,33]}
For I in DIC:
Print (i)
B. Please loop out all the value

For I in DIC:
Print (Dic[i])
C. Please loop out all the keys and value
For I in DIC:
Print (I,dic[i])

D. Add a key-value pair in the dictionary, "K4": "V4", and output the added dictionary

dic[' K4 ']= ' v4 '
Print (DIC)
E. In the modified dictionary, the value of "K1" corresponds to "Alex", Output the modified dictionary

dic[' K1 ']= ' Alex '
Print (DIC)
F. Please append an element 44 to the value of the K3, and output the modified dictionary

dic[' K3 '].append (44)
Print (DIC)

G. Insert element 18 in the 1th position of the K3 corresponding value, output the modified dictionary

dic[' K3 '].insert (0,44)
Print (DIC)
27. Conversion
A. Converting a string s = "Alex" to a list
s = "Alex"

Print (list (s))
B. Convert the string s = "Alex" to Cheng Yuanju

s = "Alex"
Print (tuple (s))

B. Convert the list li = ["Alex", "seven"] to the Narimoto group

Li = ["Alex", "seven"]
Print (Tuple (LI))
C. Convert meta-progenitor tu = (' Alex ', ' seven ') to a list

Tu = (' Alex ', ' seven ')
Li=list (TU)
Print (LI)

D. Convert the list li = ["Alex", "seven"] to a dictionary and the dictionary key is incremented by 10

dic={}
For k,v in Enumerate (li,10):----Li to put in front, sequence number in the back
Dic[k]=v
Print (DIC)
27, transcoding n = "Old boy"
A. Convert the string to UTF-8 encoded byte and output, then convert the byte to Utf-8 encoded string, and then output n = "Old boy"
A=n.encode (' Utf-8 ')
Print (a)
B=a.decode (' Utf-8 ')
Print (b)
B. Convert the string to GBK encoded byte and output, then convert the byte to GBK encoded string, and then output
A=n.encode (' GBK ')
Print (a)
B=a.decode (' GBK ')
Print (b)
28. For all numbers within 1-100

Count=1
Sum=0
For I in range (100):
Sum=sum+count
Count+=1
Print (sum)

Count=1
Sum=0
While count<=100:
Sum+=count
Count+=1
Print (sum)
29. Element classification
There is a value set [11,22,33,44,55,66,77,88,99,90] that holds all values greater than 66 in the first key of the dictionary,
Saves a value less than 66 to the value of the second key.
That is: {' K1 ': All values greater than 66, ' K2 ': All values less than 66}

LI=[11,22,33,44,55,66,77,88,99,90]
dic1={
' Max ': [],
' min ': []
}
For I in Li:
If I <66:
dic1[' min '].append (i)
Else
dic1[' Max '].append (i)
Print (DIC1)

---{' max ': [11, 22, 33, 44, 55]}, [[]

Python Basic Knowledge Summary

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: info-contact@alibabacloud.com 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.