Python (from dictionary data ~ type) of the test questions

Python basic data type exams 

a. Basic questions:
1. Briefly variable naming convention

1. The variables can contain only letters, digits, and underscores, and the numbers can not begin
2. Variable not use the keywords
3. Variables can not use capital letters as much as possible, because the capital is a constant default Python

2. the relationship between byte and bit

8 = 1 byte

3. when the 'brother' use utf-8 encoding, the number of bits and bytes accounted for, how much? When use gbk encoding, when the number of bits and bytes accounted for how much?

utf-8:
representing the number of bits is 6 bytes, 48
gbk:
representing the number of bits is 4 bytes, 32

4. 12 dictation string functions, and describe its function.

1.append (): increase in a character, is added at the end of the default
2.insert (): insert a character, () which can acquaintance, insertion position, the insertion position of the index subscript
3.replace (): replacement, (old, new), the new characters replace the old character
4.split (): division, while the quasi-string can be replaced by a list of
5.isdigit (): whether the numbers, the return is a BOOL value
6.isalpha (): whether the alphabet, returns a BOOL value
7.isalnum (): whether the letters or numbers, returns a BOOL value
8.index (): find the position () of is the index, if there is no character in the index, it will error
9.find (): Find a location, () is the index of the same, if the character label does not exist, None
10.upper (): The character capitalized
11.lower (): The character lowercase
12. strip (): remove about blank, default is space () can be written in the characters you want to delete.
13.rstrip (): remove spaces on the right, the default is a space () can be written in the characters you want to delete.
14.lstrip (): remove the left of the space character, the default is space () can be written in the characters you want to delete.
15.center (): center

5. numbers, strings, lists, tuples, dictionaries corresponding respectively boolean value False?

Digital:
. 1 -> True
0 -> False
List:
[] -> False
[ '', '' ......] -> True
String:
'' -> False
'' -> True
tuples:
() -> False
(. 1) -> True
dictionary:
{} -> False
{ 'a'} -> True

6. the writing Python2 and in three different Python3.

1.
When using print 2.python2 can be added (), or may not add (), but 3 must be added, if not being given
3. When the input variable values, python2 give different data depending on the output of type, all three types of default string str
4.python2 compiler using the Unicode format default, default is 3. 8-UTF

7. the write code, the following list, each function realized microtome
li = [1, 3,2-, 'a',. 4, 'B',. 5, 'C']
. 1) by slicing li list to form a new list L3, L3 = [1,2,4,5]

L3 = li [0 : -1: 2]
Print (L3)

2) formed by slicing the new list of l4 li list, l4 = [. 3, 'a', B]

l4 = li [. 1:. 6: 2]
Print (l4)

. 3 ) to form a new list l5, l5 = [ 'c' ] by slicing li list

L5 = li [-1]
Print (L5)

. 4) is formed by slicing a new listing of l6 li list, l6 = [ 'b ',' a ',. 3]

L6 = Li [-3: 0: -2]
Print (L6)

8. The composition nested question:

a write code, the following list, each function according to requirements. (Two methods each question)
LIS = [[ 'K', [ 'QWE', 20 is, { 'K1': [ 'TT',. 3, '. 1'



Write code, following the dictionary, as required to achieve each function. dic = { 'k1': ' v1', 'k2': [ 'alex', 'ab'], (1,2,3,4,5): { 'k3': [ '2', 100, ' wer ']}}





























1) adding a value corresponding to the rearmost 'k2' element '23 is'

DIC [' k2 ']. The append ('23')
Print (DIC)

2) the 'first position corresponding to a value of k2' inserted an element 'a'

DIC [ 'K2']. INSERT (0, 'a')
Print (DIC)

. 3) adding (1,2,3,4,5) corresponding to the value of a key-value pair 'k4', 'K4'

DIC [(1,2,3,4,5)]. setDefault ( 'K4', 'K4')
Print (DIC)

. 4) adding (1,2,3,4,5) corresponding to the value of a key-value pair (l, 2,3), 'OK'

DIC [(1,2,3,4,5)]. setDefault ((l, 2,3), 'OK')

. 5) the 'k3' value corresponding to 'wer' change 'QQ' (!!!)

DIC [(1,2,3,4,5)] [ 'K3'] [2] = 'QQ'
Print (DIC)


9. the conversion topic:

how to convert between int and str, what is the result of the conversion? There are no conditions?

Convert between int and str is unconditional.
int (123) -> str ( 123): '123'

How to convert between int and bool, what is the result of the conversion? There are no conditions?

bool conversion between int and unconditional.
When int = 0, int (0) -> False
When int = 0 Shi, int (1) -!> True

between bool and how to transform str, what is the result of the conversion? There are no conditions?

. str conversion bool unconditionally
when str = '' str -> False
when = STR '' STR -> True

STR can be transformed with the list? How to convert?

str can be converted to the List
str.split () -> Strings to list
str.join () -> list translated into strings

10. The following results were achieved:
1) a list of li = [ 'alex', ' wusir ',' rain '] by operating the configuration list a string = S' alexwusirrain '

Li = [' Alex ',' wusir ',' rain ']
S =' '
for I in Li:
S = S + I
Print ( S)

2) a list of li = [ 'alex', ' wusir', 'rain'] by operating the configuration list a string = S '* Alex * wusir Rain'

Li = [ 'Alex', 'wusir', ' rain '




3) the string s = 'alexwusirrain', the character string configured by a list of operating = Li [ 'A', 'exwusirlex']

S = 'alexwusirrain'
Li = s.split ( 'L')
Print (Li)

. 4 ) the string s = 'alex wusir', the character string configured by a list of operating = Li [ 'Alex', 'wusir']

S = 'Alex wusir'
Li = s.split ( '')
Print (Li)

. 5 ) the string s = 'alex' by operation of the string a string configured S1 'a_l_e_x' =

s = 'alex'
S1 = '_'. the Join (S)
Print (S1)

11. the use of a while loop, respectively, and printing for loop 5 + 1-2 + 3-4 ..... +99 result of

the while loop:

I =. 1
SUM = 0
while I <100:
! IF I = 2% 0:
SUM = SUM + I
elif I % 2 == 0:
= sum sum - i
i = i + 1
print (sum)

for loop:

SUM = 0
for I in range (100):
IF I = 2% 0:!
SUM = SUM + I
elif I% 2 == 0:
SUM = SUM - I
Print (SUM)

12. The printing using the range 100 , 99, 98, ...... 1,0

for I in Range (100, -1, -1):
Print (I)

13. the user input content is calculated index is odd and the corresponding elements of a digital number (the number is not zero)

#while
SUM = 0 # count the number of
I =. 1
Message INPUT = ( 'INPUT to some User Message Please:')
the while I <len (Message):
IF I% 2 == 0 :
IF Message [-I. 1] .isdigit () == True:
SUM = SUM +. 1
I = I +. 1
the else:
I = I +. 1
Print (SUM)

#for
SUM = 0
Content = INPUT ( '<<<')
for I in Range (len (Content)):
IF I. 1 and% 2 == Content [I] .isdigit ():
SUM = SUM +. 1
Print ( SUM)

14. a
has the following values li = [11,22,33,44,55,77,88,99,90], all values greater than the stored first key 66 to a dictionary, the
values less than 66 to a value stored in the second key.

= Li [11,22,33,44,55,77,88,99,90]
Result = {}
A = []
B = []
for Li in Row:
IF Row> 66:
a.append (Row)
elif Row <66:
b.append (Row)
for I in Range (len (A)):
A [I] STR = (A [I])
for I in Range (len (B)):
B [I] = STR (B [I])
A1 = ','. the Join (A)
= B1 ','. the Join (B)
result.setdefault (A1)
result.setdefault (B1)
Print (Result)

15. A
look li element in the list, each element of the space is removed, and to find the 'A' or 'a' at the beginning, and to 'c' of all the elements at the end and add to a new list,
the last cycle print this new list
li = [ 'taibai', ' alexC', 'AbC', 'egon', ' Ritian ',' Wusir ',' AQC ']


Li = [' Taibai ',' alexC ',' AbC ',' Egon ',' Ritian ',' Wusir ',' AQC ']
A = []
for I in Range (len (Li)):
Li [I] = Li [I] .strip ()
IF (Li [I] .startswith ( 'A') or Li [I] .startswith ( 'A')) and Li [I ] .endswith ( 'C'):
a.append (Li [I])
the else:
Continue
Print (A)

16. A calculator implemented an integer addition:
as:
Content = INPUT ( "Please enter Content: ') # user input: 5 + 7 + 8 .... (enter at least two numbers together),
Then divided in calculations, the final calculation result added to this dictionary (Alternatively None)
DIC = { 'end result': None}

DIC = { 'end result': None}
Content = INPUT ( 'Enter content: ')
SUM = 0
CON = content.split (' + ')
for I in Range (len (CON)):
SUM = int (CON [I]) + SUM
SUM = STR (SUM)
DIC [' final calculation the results'] = SUM
Print (dic)

17. the writing process: analog hr company employees account password entry program.

1) employees account password stored in such data types:
user_list = [
{ 'username': 'Barry', 'password': '1234'},
{ 'username': 'Alex', 'password': 'asdf '},
.......
]

2) illegal characters template: board = [' John Doe ',' four Li ',' king pock '


Will replace illegal characters into the same number of * (such as the king, pock replaced by ****), and then added to the uer_list, if no illegal characters,
after directly added to user_list, each added successfully, just print out add the user name and password.

= user_list [{ 'username': 'Barry' 'password': '1234'}, { 'username': 'Alex', 'password': 'asdf'}]
Board = [ 'John Doe', 'Li four, '' the king two pock ']

the while 1:
username = the iNPUT (' Please enter your user name: ')
password = the iNPUT (' strong password: ')
STOP = the iNPUT (' whether to terminate input '?)
iF stop.upper () == 'Q':
BREAK
the else:
for I in Range (len (Board)):
IF username in Board [I]:
L = len (username)
username = username.replace (username, L * '*'



user_list.append(m)
print(user_list)
if stop.upper() == 'Q':
m = {}
m['username'] = username
m['password'] = password
user_list.append(m)
print(user_list)













Guess you like

Origin www.cnblogs.com/if-it-is-possible/p/11441735.html