Python Learning-day 2

Source: Internet
Author: User
Tags python script

1. Two ways to execute a python script
1) Call interpreter Python + absolute path + file name
2) Call interpreter Python + relative path + file name

2. Briefly describe the relationship between bits and bytes
8 bits are 1 bytes

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 different languages of the world, 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 out the number of digits of "Li Jie" that are encoded with utf-8 and GBK respectively.
utf-8:48 bit
Gbk:32 bit

What is the representation of 5.Pyhton single-line and multiline comments respectively?
Single-line Comment: #
Multiline Comment: "" "

6. What are the considerations for declaring variables?
Python, like most other languages, has local variables and global variables, but it does not have a distinct variable declaration. The variable is generated by first assignment and automatically dies out when it goes out of scope.
Python does not allow referencing a variable that is not assigned, or an exception is thrown.

7. If the following variable n1=5, use the supply method of int, how many bits can be represented by the minimum number of variables?
5-bit output to 0b101
v2 = Bin (int (5))
Print (v2)

8. What are the Boolean values?
True False

9. Read the code and write out the results
A = "Alex"
B=a.capitalize ()
Print (a)
Print (b)
Output results
Alex
Alex

10. Write the code, with the following variables, follow the requirements for each function
# a = "Alex"
# b=a.capitalize ()
# Print (a)
# print (b)
name = "Alex"
# A. Remove the space on either side of the value corresponding to the name variable and enter the removed content
N1=name.strip ()
Print (n1)
#b. Determine if the value corresponding to the name variable starts with "Al" and outputs the result
n2= Name.startswith ("Al")
Print (n2)
#c. Determine if the name variable corresponds to end with "x" and output the result
N3=name.endswith ("x")
Print (n3)
# D. Replace the "L" with the name variable with "p" and output the result
N4=name.replace ("L", "P")
Print (N4)
#e. The value corresponding to the name variable is split according to "L" and output
n5= Name.split ("L")
Print (N5)
#f. What type of
#列表
#g is obtained after the previous question, E, is divided. capitalizes the value of the name variable and outputs the result
N6=name.upper ()
Print (N6)
#h. The value corresponding to the name variable is lowercase and the output
N7=name.lower ()
Print (N7)
#i. Output the 2nd character of the value corresponding to the name variable
n8=name[ 2]
Print (n8)
#请输出name变量对应的值得前3个字符
N9=name[0:3]
Print (N9)
#请输出name变量对应的值得后2个字符
N10=name[-1:-2 ]
Print (N10)
#请输出name变量对应的值中 the index position where "E" is located
N11=name.find ("E")
Print (N11)
#获取子序列, only the last character, as Oldboy gets Oldbo;root Roo
n12=input (">>>")
L=len (n12)
N13=n12[0:l-1]
Print (n13)

21. Can a string iterate over an object? Can I use a for loop for each element?
Strings can iterate
#将文件按照索引打印出来
Test = input (">>>>")
For item in test:
Print (item)

22. Please use code to implement
# A. Use an underscore to stitch each element of the list into a string, Li = "Alexericrain"
Li=[' A ', ' l ', ' e ', ' x ', ' e ', ' r ', ' I ', ' C ', ' R ', ' a ', ' I ', ' n ']
Li1= '-'. Join (LI)
Print (LI1)
############################################################################
# B. Use an underscore to stitch each element of the list into a string, Li = [' Alex ', ' Eric ', ' Rain ']
Li2 = [' Alex ', ' Eric ', ' Rain ']
Li3= '. Join (LI2)
Print (LI3)
############################################################################

Range differences in range and Python3 in 23.python2
The range in the Python2 generates the sequence directly in memory
A range is generated in the Python3, which actually saves memory space

24. How to implement an integer addition calculator, such as 5+9 or 5 +9 or 5 + 9
Content = input (' Please enter content: ')
Print (Content.replace (","))

25. How many decimal decimals are counted for user input? A few letters?
Content = input (' Please enter content: ')
Import re
#字母
y = Len (Re.findall (R ' [A-z] ', content))
Print (' The number of letters appearing: ', y)
#数字
z = Len (re.findall (R ' [0-9] ', content))
Print (' Number of occurrences: ', z)

26. Briefly describe the relationship between int and 9 and strings such as STR and "Xxoo"
Relationship of classes and objects

27. Create Fun Templates
Requirements: Waiting for the user to enter a name, place, hobby, according to the user's name and hobbies to implement any
Test = "Dear amiable {0}, favorite in {1}, place {2}"
name= {input ("Enter Name")}
place= {input ("location")}
Doing={input ("Event")}
Print (Test.format (name,place,doing))

28. Make random verification code, case-insensitive
####################################################################################
# def Check_code ():
# import Random
# Checkcode = ""
# for I in range (4):
# current = Random.randrange (0,4)
# if current!=i:
# temp = Chr (Random.randint (65,90))
# Else:
# temp = Random.randint (0,9)
# Checkcode +=str (temp)
# return Checkcode ()
# while True:
# code = Check_code ()
# Print (code)
# app = input ("Please Enter Verification Code:")
# if code.upper () = = App.upper ():
# print ("input correct")
# break
# Else:
# Print ("Input error")
# s = input ("Do you want to re-enter? ")
# if s = = "No":
# break
# Else:
# continue
########################### #摘抄至网络 #############################################

29. Develop the sensitive Word filter program, prompt the user to enter content, if the user input content contains special characters: "Teacher Cang", "Tokyo Hot", then replace the content with * * *
V=input (">>>")
V1=v.replace ("Cang teacher", "* * *")
V2=v1.replace ("Tokyo Hot", "* * *")
Print (v2)

30. Making Forms
Loop Prompt user input: User name, password, mailbox (requires the user to enter a length of not more than 20 characters, if more than the first 20 characters are valid)
If the user enters Q or Q for no further input, the content entered by the user is displayed in a tabular format.

S= ""
While True:
Name= input ("Please enter user name:")
If name = = "Q" or name== "Q":
Break
If Len (name) >=20:
name = Name[0:20]
PWD = input ("Please enter password:")
If Len (pwd) >=20:
name = Pwd[0:20]
Email=input ("Please enter your email address:")
If Len (email) >=20:
name = Email[0:20]
Template = "{0}\t{1}\t{2}\n"
V=template.format (Name,pwd,email)
S +=V
Print (S.expandtabs (20))

# # # #转自海峰老师文章, thks ...

Python Learning-day 2

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.