In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you the "python basic data type exercises", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn what are the "python basic data type exercises" this article.
Topic [1]: format output exercise. Complete the following exercises in an interactive state.
Screenshot of the running result:
Topic [2]: format output exercise. Complete the following exercises in the .py file
Code:
Num = 100print ('% d to hex is% x'% (num,num)) print ('% d to hex is% X'% (num,num)) print ('% d to hex is% # x'% (num,num)) print ('% d to hex is% # X'% (num,num)) from math import piprint ('value of Pi is:% .4f'% pi) students = [{'name':'zhangsan','age':20} {'name':' lisi', 'age': 19}, {' name': 'wangwu',' age': 19}] print ('name: s, age: d'% (students [0] ['name'], students [0] [' age'])) print ('name:%-10s, age:%-10d'% (students [1] [' name']) Students [1] ['age']) print (' name: s, age: d'% (students [2] ['name'], students [2] [' age']) for student in students: print ('% (name) s is% (age) d years old'% student)
Run:
Title [3]: Caesar encryption:
Principle and function:
Encrypt and decrypt by moving the letters by a certain number of digits
All letters in plaintext are offset backward (or forward) from the alphabet at a fixed step and replaced with ciphertext.
For example, when the step size is 3, An is replaced by D, B is replaced by E, and so on, X is replaced by A.
Code:
Import string#ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'#ascii_lowercase =' abcdefghijklmnopqrstuvwxyz'#ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'def kaisa (s, k): lower = string.ascii_lowercase upper = string.ascii_uppercase before = string.ascii_letters after = lower [k:] + lower [: K] + upper [: K] + upper [: K] table =' .maketrans (before,after) return s.translate (table) s = 'Python isa great programming language. I like itinerary printing (kaisa (SMagne3))
Run:
Topic [4]: it is rated by eight films with a score of 1-10.
1) record the Douban score in a dictionary and output the dictionary.
2) now there are two new films and their ratings (Chinese Captain: 7.0, Galaxy cram class: 6.2). Add this film review to the dictionary, and output all the movie names in the dictionary at the same time.
3) find out the film with the highest score in the dictionary in 2).
Code and run results:
1 >
Films = {'Shawshank's Redemption': 9.7, 'wrestle! Father': 9.0,Forrest Gump: 9.5Magic'me and my Motherland': 8.0,8.5,8.5,Thousand and thousand fathom: 9.3, Zootopia: 9.2Jet: climber: 6.5} print (films)
2 >
Films_new = {'Chinese Captain': 7.0 'Galaxy cram': 6. 2} insert dict.update () function print ("all movie titles:", films.keys ()) into the dictionary of films.update (films_new) #)
3 >
Print ("Film with the highest score:", max (films,key=films.get))
Topic [5]: programming implementation: generate 2 groups of random 6-digit digital verification codes, 10000 in each group, and can not be repeated in each group. Output the number of CAPTCHA repeats for these two groups.
Code and run results:
Import randomcode1 = [] # Storage check code list code2 = [] t = 0 # Mark number of duplicate check codes dict= {} # first set of check codes for i in range (10000): X =''for j in range (6): X = x + str (random.randint (0) 9) the numeric check code generated by code1.append (x) # is appended to the list # the second set of check codes for i in range (10000): X =''for j in range (6): X = x + str (random.randint (0) 9) the numeric check code generated by code2.append (x) # is appended to the list # find duplicate for i in range (len (code1)): for j in range (len (code2)): # traverse if for all check codes of code1 and code2 (code1 [I] = = code2 [j]): t = tweak 1 # if the same one exists Then tweak 1 if t > 0: repeat [code 1 [I]] = t # if the number of repeats is greater than 0, use t to indicate the number of them, and store them in dictionary # to output all duplicate check codes and their number for key in dict: print (key + ":" + str (keys]))
Intercept a few pictures:
Topic [6]: statistical English sentence "Life is short, we need Python." The number of times each character appears in the.
Code and run results:
# remove spaces and convert to list Then convert it to the dictionary str = 'Life is short, we need Python.'list = [] list2 = [] dict= {} I = 0for w in str: if walled characters': list.append (w) # remove the spaces of the str string and put it in the list list for w in list: C = list.count (w) # the number of current characters returned by the count () function dict [w] = c # for character w, the number of characters represented by c Stored in dictionary print (dict) # output dictionary
Topic [7]: enter an English sentence and output the longest word and its length.
Tip: you can use the split method to separate the words in English sentences and store them in the list for post-processing.
Code and run results:
Test0 ='It is better to live a beautiful life with all one''s'\ 'strength than to comfort oneself with ordinary and precious thingsroom.roomtest1 = test0.replace (',','). Replace ('.,') # replace the space sum of "," in the sentence with spaces. " Test2 = test1.split () # separate the words in the English sentence into the list maxlen = max (len (word) for word in test2) # find the maximum length of the word length value C = [word for word in test2 if len (word) = = maxlen] # find the maximum length of the word corresponding to the word print ("the longest word is:" {} "with {} letters" .format (C [0], maxlen))
The above is all the contents of the article "what are the exercises for the basic data types of python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.