Python basics 1 and python Basics

Source: Internet
Author: User

Python basics 1 and python Basics
1. variable naming rules: Start with a letter or underline, only letters, numbers, underscores (_) are allowed, and keywords cannot be declared as variable names. variable = "values"

name = "Ethan Du"print("My name is ", name)
2. Chinese characters and non-ASCII characters are not supported in character encoding python2. The character encoding must be declared at the beginning.-*-coding: UTF-8-*-python3 supports unicode character encoding by default. 3. Single-line comment # multi-line comment '''code''' 4. user input: the default input is character input () username = input ("Username:") password = input ("Password:") print (username, password) ciphertext access import getpass passwd = getpass. getpass ("password:") (This function cannot be used in pychar) 2. raw_input and 3. input in x is equivalent. (Do not use 2. input in x) 5. formatted input. 1. string concatenation: occupies multiple memory blocks. Low efficiency. 2.% s: string % d: integer % f: float
 info0 = '''  ------info of %s------  name:%s  age:%s  job:%s  ''' % (,,age,job)
3. Variable Substitution
  info1 = '''  ------info of {_name}------  name:{_name}  age:{_age}  job:{_job}  '''.format(_name=name, _age=age, _job=job)  = '------info of {_name}------\n' \'name:{_name}\n' \'passwd:{_passwd}\n' \'-----------------------------'.format(_name=name,_passwd=passwd)
4. Separator sep end
  name = "Ethan Du"  name1 = "HAHA"  print(name,name1,sep=' ',end='.')  STDOUT:Ethan Du HAHA.
5. Output redirection
sys.stdout = (open('log.txt'),'a') import systemp = sys.stdoutsys.stdout = open('log.txt','a')print('test')sys.stdout.close()sys.stdout = tempprint('test')
6. if else process judgment PS: 1. Pay attention to the if judgment condition or the colon after else: 2. Strict indent, difference sub-Statement 3. By default, all input values in python are of optimized type.
age = int(input('guess age: '))if age == 50:    print('yes')elif age > 50:    print('younger')else:    print('older')
7. while Loop
count = 0while count != 10:    if count == 9:        print('count:',count)    else:        print(count)    count +=1else:    print('count:',count)
8. dictionary: dict = {'ethan': 'hahaha', 'dyp ': 'aaa'} call dict ['ethan'] to update dict ['ethan'] = 'hhhaa' to delete del dict ['ethan'] # Delete the element dict whose key is ethan. clear () # Delete All dict elements del dict # Delete dict dictionary 9. file I/O open file = open ('filename', 'R') # r: read, w: write, a: if not, create the Write file a +: if not, create the Read/Write file temp = file. read () temp = file. readline () # Read a row of temp = file. readlines () # read each line for str in file: # print (str) traversal file Write file. write (str) file. writelines (str) # write multiple lines to close the file. close (): with automatically closes with open ('file') 10. split () function str. split (str = '', num = string. count (str) [n] str: delimiter, default space num: Number of splits, divided into num + 1 string [n]: Indicates selecting the first segment
Print u. split ('. ',-1) # The maximum number of splits (the actual number is the same as that without the num parameter) ['www', 'doiid', 'com ', 'cn'] u1, u2, u3 = u. split ('. ', 2) # split twice, and save the separated three parts to the print u1wwwprint u2doiidoprint u3com.cn str = "hello boy <[www.doiido.com]> byebye" print str. split ("[") [1]. split ("]") [0] www. doiido. comprint str. split ("[") [1]. split ("]") [0]. split (". ") ['www ', 'doiid', 'com']
11. Exercise. logon to the applet requires you to enter the user name and password. If the authentication succeeds, the welcome information is displayed. If you enter an error three times, the lock process is as follows:

 

Code:

#Author:Ethan Ducount = 0while True:    flag = 0    username = input('Username: ')    password = input('Password: ')    if username == '' or password == '':        print('Username or Password is empty, please input again')        continue    with open('user_file','r') as file:        for key in file:            if key.split()[0] == username and count != 3:                with open('lock_file','r') as lock:                    for dlock in lock:                        if dlock.split()[0] == username:                            print('too much wrong login, exit')                            exit()                if key.split()[1] == password:                    print('Welcome to system')                    flag = 1                    exit()                else:                    print('Wrong password, please input again')                    count += 1                    if count == 3:                        with open('lock_file','w') as lock:                            lock.write(username)                        count = 0                        print('too much wrong login, exit')                        exit()                    flag = 2                    break        if flag == 0:            print('username is not exsit, please input correct username')

  

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.