Getting the basic exercise recording python

Python script execution in two ways:

  1, configured environment variables, python * .py

  2, into the python interpreter python performed directly

Description of bits, bytes relationship:

  1,1-byte (Byte) = 8 bit (bit) 

DESCRIPTION ascii, unicode, utf-8, gbk relationship:

     1, ascii: a first coding method, using a byte is 8 bits to represent a character, a maximum of 2 ** 8 = 256 characters.

  2, unicode: commonly known as Unicode, unified or 16 bits to represent a character with 2 bytes, a maximum of 2 ** 16 characters.

  3, utf-8: As the character ascii code can represent too little, Unicode will waste storage and transportation, so utf-8 was brought up. utf-8 is a variable-length encoding. How many how many bytes byte characters need to use. Usually ascii code characters are English, Chinese characters uses three bytes to encode.

  4, gbk: gbk is the encoding, Chinese generally by 2 bytes, one byte in English.

  eg: for example, "Li Jie" utf-8 at 6 bytes needed to represent, but GBK requires 4 bytes to represent.

  5, len function: python3 which calculates the length of the string, python27 bytes which is calculated.

  eg: a = "Jie" utf-8 encoded is assumed, in the python2 len (a) the output 6, pyhton3 was 2.

python Notes:

   1, single-line comments: #

   2, multi-line comments: '' '' '' or "" "" ""

python variable rules:

   1, number / letter / underscore

   2, the numbers do not begin with

   3, built-in functions, and can not influence the same name features built-in functions

There follows a variable n = 15, showing what is the minimum number of binary variables need:

n=5
print(n.bit_length())

What are Boolean values:

   1, there are: False, True

capitalize function:

   1, the first letter capitalized

The following variables a = "aleX", please implement the following functions:

   1, removing the variable value corresponding to both sides of the space, and outputs the space after the removal of

 

a=" aleX"
print(a)
print(a.replace(' ',''))

 

   2, it is determined whether the variable al beginning or ending X, and outputs the judgment result

a=" aleX"
print(a)
print(a.startswith('al'))
print(a.endswith('X'))

  3, the variable l is replaced by p, and outputs the result:

 

a=" aleX"
print(a)
print(a.replace('l','p'))

 

  4, is divided according to the variable L, and outputs the result: the result output list file type

a=" aleX"
print(a)
print(a.split('l'))

   5, becomes the value of the variable upper / lower case output:

a=" aleX"
print(a)
print(a.upper())
print(a.lower())

   6, the output of the second variable and the first three characters and the last two characters of characters:

a=" aleX"
print(a)
print(a[1])
print(a[0:3])
print(a[-3:-1])

  7, output a variable index position e of:

a=" aleX"
i=0
while i<len(a):
    if a[i]=='e':
        print(i)
    i=i+1
print(a.index('e'))
print(a.find('e'))

  8, whether the string iterable (as long as you can use a for loop circulation of objects)? As you can use the for loop for each element?

a=" aleX"
i=0
while i<len(a):
    print(a[i])
    i=i+1

     9, use code implemented:
      . A use of each element of the list underlined spliced into a string, Li = "alexericrain"
      B. Using underlined each element of the list spliced into a string, Li = [ 'Alex', ' eric ',' rain ']

 

li="alexericrain"
print('_'.join(li))
li=['alex','eric','rain']
print('_'.join(li))

 

  10, Python 2 in the range and Python3 the range difference?

        python2 which is created immediately stored in memory, if the data is too large, insufficient memory or stuck, python3 created if there are eleven cycle.

        python2 inside xrange and python3 inside the range is the same, all eleven created if inside a for loop cycle.

    11, to achieve a integer addition calculator:

        Such as:

          content = input ( ' Enter content: ') # as: 5 + 9 or 5+ 9 or 5 + 9

content = input ( "Enter a two expression Vega Act") 
V1, V2 = content.split ( '+') 
V1 = int (V1) 
V2 = int (V2) 
Print (V1 + V2)

   12、计算用户输入的内容中有几个十进制数?几个字母?
     如:
        content = input('请输入内容: ') # 如: asduiaf878123jkjsfd-213928

a = input("请输入一个字符串\n")
t1 = 0
t2 = 0
for i in a:
    if i.isnumeric():
        t1 += 1
    if i.isalpha():
        t2 += 1
print(t1, t2)

  13、简述 int 9 等数字 以及 str "xxoo" 等字符串的关系?
       int/str是类,9/“xxoo”是对应类下面的对象。

   14、制作趣味模板程序
       需求:等待用户输入名字、地点、爱好,根据用户的名字和爱好进行任意现实
       如:敬爱可亲的 xxx,最喜欢在 xxx 地方干 xxx

a = input("请依次输入姓名,地点,爱好(以逗号分割)\n")
v1, v2, v3 = a.split(',')
print('敬爱可亲的' + v1 + ',最喜欢在' + v2 + '地方干' + v3)
#或者如下
v = "敬爱可亲的{0},最喜欢在{1}地方干{2}"
print(v.format(v1, v2, v3))

  15、制作随机验证码,不区分大小写。
       流程:
        - 用户执行程序
        - 给用户显示需要输入的验证码
        - 用户输入的值
        用户输入的值和显示的值相同时显示正确信息; 否则继续生成随机验证码继续等待用户输入

 

#生成随机码自定义函数
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)
    a = input("请输入验证码:\n")
    if a.upper() == code.upper():
        print("输入正确")
        break

 

  

 

      16、开发敏感词语过滤程序, 提示用户输入内容,如果用户输入的内容中包含特殊的字符:

        如 "苍老师" "东京热",则将内容替换为 *** 

 

a = input("请输入内容:\n")
a = a.replace("苍老师","***")
a = a.replace("东京热","***")
print(a)

 

  

 

 17、制作表格

       循环提示用户输入:用户名、密码、邮箱 (要求用户输入的长度不超过 20 个字符,如果超过则只有前 20 个字符有效)
       如果用户输入 q Q 表示不再继续输入,将用户输入的内容以表格形式大隐

s=''
while True:
    a = input("请输入用户名,密码,邮箱(按照','分割)\n")
    if a.upper() == 'Q':
        break
    v1,v2,v3 = a.split(',')
    v1 = str(v1[0:20])
    v2 = str(v2[0:20])
    v3 = str(v3[0:20])
    #print(v1,v2,v3)
    temp = "{0}\t{1}\t{2}\n"
    s=s+temp.format(v1,v2,v3)
print(s.expandtabs(20))

  



 



 

 





 

 

 

 

 

  

 

Guess you like

Origin www.cnblogs.com/liuzhijia/p/11102763.html