Python basics and data type exercises

Source: Internet
Author: User

# One, the basic problem.
# 1, describe the variable naming specification (3 points)
‘‘‘
Variable name cannot be a keyword in python
Variable names can be combinations of alphanumeric underscores
Variable names cannot start with a number
‘‘‘
# 2, byte and bit relationship. (2 points)
# 1 bytes byte =8 bit bit
# 3, ' elder brother ' when using utf-8 encoding, what is the number of bits and bytes? What is the number of bits and bytes when using GBK encoding? (2 points)
# s= ' two elder brother '
# res=s.encode (' utf-8 ') #6个字节 * 8 bit bit = 48 positions
# res1=s.encode (' GBK ') #4个字节 * 8 bit bit = 32 positions
# Print (RES)
# Print (RES1)
# 4, the 12 functions of the dictation string, and describes its role. (12 points)
#
# 5, number, string, list, meta-ancestor, dictionary corresponding to the Boolean value of the false what is the difference? (5 points)
‘‘‘
Number: 0 string: ' list: [] tuple: () dictionary: {}
‘‘‘
# 6, the writing Python2 is different from the three in the Python3. (3 points)
‘‘‘
Python2
Xrange
Input
Print printing without parentheses
Python3
Range
Raw_input
Print ()
‘‘‘
# 7, write code, such as the following table, the use of slicing to achieve each function (one point per question, a total of 4 points)
#li = [1,3,2, ' A ', 4, ' B ', 5, ' C ']
# 1) Create a new list by slicing the li list l3,l3 = [' 1,2,4,5]
# S1=li[0::2]
# print (S1)
# 2) Create a new list by slicing the li list l4,l4 = [3, ' A ', ' B ']
# S2=li[1:-1:2]
# Print (S2)
# 3) Create a new list by slicing the li list l5,l5 = [' C ']
# S3=li[-1]
# Print (S3)
# 4) Create a new list by slicing the li list l6,l6 = [' B ', ' A ', 3]
# S4=li[5:0:-2]
# Print (S4)
# 8, combining nested questions.
# A, write code, like the following table, according to the requirements to achieve each function (3 points per question, write a method to get 1 points, write out two methods 3 points. A total of 9 points for this question)
# (each is a single line of code implementation)
#lis = [[' K ', [' qwe ', 20,{' K1 ': [' TT ', 3, ' 1 ']},89], ' AB ']
# 1) Turn the ' TT ' in the list Lis into uppercase (in two ways).
# lis[0][1][2][' K1 ']=lis[0][1][2][' K1 ']= ' TT '
# Print (LIS)
# lis[0][1][2][' K1 '][0]=lis[0][1][2][' K1 '][0].upper ()
# Print (LIS)
# 2) Change the number 3 in the list to the string ' 100 ' (in two ways).
# lis[0][1][2][' K1 '][1]= (str (lis[0][1][2][' K1 '][1]+97))
# Print (LIS)
# lis[0][1][2][' K1 '][1]=lis[0][1][2][' k1 '][1]= ' 100 '
# Print (LIS)
# 3) Change the string ' 1 ' in the list to the number 101 (in two ways).
# lis[0][1][2][' K1 '][2]=lis[0][1][2][' K1 '][2]+ ' 01 '
# Print (LIS)
# lis[0][1][2][' K1 '][2]=str (int (lis[0][1][2][' K1 '][2]) +100)
# Print (LIS)
# b, write code, have the following dictionary, as required to achieve each function (5 points)
#dic = {' K1 ': ' v1 ', ' K2 ': [' Alex ', ' SB '], (1,2,3,4,5): {' K3 ': [' 2 ', ', ' Wer ']}}
# 1) Add an element ' 23 ' to the last face of the value corresponding to ' K2 '.
# dic[' K2 '].append (' 23 ')
# Print (DIC)
# 2) Insert an element ' a ' in the first position of the value corresponding to ' K2 '.
# dic[' K2 '].insert (0, ' a ')
# Print (DIC)
# 3) Add (1,2,3,4,5) The corresponding value of a key value to ' K4 ', ' v4 '.
# dic[(1,2,3,4,5)].setdefault (' K4 ', ' v4 ')
# Print (DIC)
# 4) Add the corresponding value (1,2,3,4,5) to a key-value pair (all-in-one), ' OK '.
# dic[(1,2,3,4,5)].setdefault ((+), ' OK ')
# Print (DIC)
# 5) Change the ' wer ' of the value corresponding to ' K3 ' to ' QQ '.
# dic[(1,2,3,4,5)][2]= ' QQ '
# Print (DIC)
# 9, conversion problem (4 points).
#
# How is the conversion between int and STR, and what is the result of the transformation? Is there a condition?
# Str->int Int (' 5 ')
#int->str Str (5)
# How is the conversion between INT and bool, and what is the result of the transformation? Is there a condition?
# int:0 1
# 0-->false
# 1-->true
# How are the conversions between STR and bool, and what is the result of the conversion? Is there a condition?
# empty string is False '-->fales
# non-empty string is True ' Hello '-->true
# can STR and list be converted? How to convert?
#str List Str.split ()
# list-->str '. Join (list)
# 10, achieve the following results (5 points).
# 1) There is a list of li = [' Alex ', ' Wusir ', ' rain '] construct a string by manipulating the list s= ' Alexwusirrain '

# s= '. Join (LI)
# print (s)
# 2) There is a list of li = [' Alex ', ' Wusir ', ' rain '] construct a string by manipulating the list s= ' Alex*wusir*rain '
# s1= ' * '. Join (LI)
# print (S1)
# 3) There is a string s = ' Alexwusirlex ', constructed by manipulating the string to construct a list li = [' A ', ' Exwusirlex ']
# s = ' Alexwusirlex '
# s1=s.split (' l ', 1)
# print (S1)

# 4) There is a string s = ' Alex Wusir ', construct a list by manipulating the string li = [' Alex ', ' Wusir ']
# s= ' Alex Wusir '
# Print (S.split ())

# 5) There is a string s = ' Alex ' constructs a string by manipulating the string S1 = ' a_l_e_x '
# s= ' Alex '
# s1= ' _ '. Join (s)
# print (S1)

# 11, respectively, using the while loop, and the for loop prints the results of the 1-2+3-4+5.......+99. (10 points)
# count=0
# for I in range (1,100):
# if I% 2 = = 0:
# count-=i
# Else:
# count+=i
#print (count)

# 12, use range to print 100,99,98, .... 1,0 (2 min)
# for I in Range (100,-1,-1):
# Print (i)

# 13, calculate the number of the index in user input is odd and the corresponding element is number (no number is 0) (6 points)
# count=0
# info=input (' Please enter content: '). Strip ()
# for I in range (len (info)):
# if I% 2 = = 1 and info[i].isdigit ():
# count+=1
# Print (count)

# 14, Supplemental code (continue writing under existing code): (6 points)
# with the following value li= [11,22,33,44,55,77,88,99,90], all values greater than 66 are saved to the first key in the dictionary, and values less than 66 are saved to the value of the second key.
# li = [11,22,33,44,55,77,88,99,90]
# result = {}
# for row in Li:
# if row > 66:
# if ' K1 ' not in result:
# result[' K1 ']=[]
# Else:
# result[' K1 '].append (row)
# Else:
# if ' K2 ' not in result:
# result[' K2 ']=[]
# Else:
# result[' K2 '].append (Row)
# Print (Result)
#方法二
# Result.setdefault (' K1 ', [])
# Result.setdefault (' K2 ', [])
# for I in Li:
# If I > 66:
# result[' K1 '].append (i)
# Else:
# result[' K2 '].append (i)
# Print (Result)
# 15, find the elements in the list Li, remove the spaces for each element, and find all the elements that start with ' a ' or ' a ' and end With ' C ', add to a new list, and then loop through the new list. (6 points)
# li = [' Taibai ', ' alexc ', ' AbC ', ' Egon ', ' Ritian ', ' wusir ', ' AQC ']
# new=[]
# for I in Li:
# S=i.strip ()
# if s[0].upper () = = ' A ' and s[-1]== ' C ':
# new.append (i)
# for X in New:
# Print (x)

# 16, Implementing an Integer addition Calculator: (6 points)
# such as: content = input (' Please enter content: ') # If user input: 5+8+7 .... (add at least two numbers), then split and then calculate, adding the final result to this dictionary (replace none):
# dic={' final calculation result ': None}.
# info=input (' >>> '). Strip ()
# dic={' final result ': None}
# info_list = info.split (' + ')
# num=0
# for I in Info_list:
# I=int (i)
# num+=i
# dic[' The final result ']=num
# Print (DIC)

# 17, write the program: simulate the company HR input employee account password procedures. (10 points)
# 1), the employee's account password is stored in this data type:
# 2) Illegal character Template: board =/[' Zhang San ', ' Li Xiaoxi ', ' King of two leper ']
# 3) HR input user name, password (sustainable input, if you want to terminate the program, then enter the user name when entering Q or Q exit program), in the HR input user name, detect if this user name has board inside the illegal characters,
# If there are illegal characters, then replace the illegal characters with the same number of * (such as Wang er), and then add to the user_list, if there is no illegal characters, then added directly to User_list, each time the addition of the successful, the new user name, the password was added.

# user_list = [
# {' username ': ' Barry ', ' Password ': ' 1234 '},
# {' username ': ' Alex ', ' Password ': ' Asdf '},
# ]
# board = [' Zhang San ', ' Li Xiaoxi ', ' King two leper ']
# while 1:
# username=input (' username '). Strip ()
# if username.upper () = = ' Q ': Break
# password=input (' password '). Strip ()
# for I in board:
# If I in Username:
# username=username.replace (i, ' * ' *len (i))
# user_list.append ({' username ': username, ' password ':p assword})
# print ({' username ': username, ' password ':p assword})
# Print (user_list)

Python basics and data type exercises

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: 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.