python Foundation Lesson 1

Keywords: Python encoding ascii Lambda

I. The first python program

print('hello world!')   # python3.x
 
print 'hello world!'   # python2.x

 

II. Variables

2.1 Variable Name Rules

  • Variable names can only be any combination of letters, numbers, or underscores
  • The first character of a variable name cannot be a number
  • The following keywords cannot be declared variable names

['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

 

2.2 Cases

_name = 'sam'   # Correct
1name = 'yang'  #  error
name_it_1 = 'yuyu'  # Correct

 

2.3 Code Case

#!/usr/bin/env python3
# encoding: utf-8
# Author: Sam Gao


# print("Hello world")

name = "Sam Gao"   #  name -----> "Sam Gao" 
name2 = name   #  name -----> "Sam Gao"   and   name2 -----> "Sam Gao" 

print('My name is', name, name2)   # Output: My name is Sam Gao Sam Gao

name = 'pao che ge'  #   name -----> "pao che ge" and name2 -----> "Sam Gao" 

print(name, name2)   # Output: pao che ge Sam Gao

 

Character encoding

3.1  ascii

ASCII (American Standard Code for Information Interchange) is a computer coding system based on Latin letters. It is mainly used to display modern English and other Western European languages. It can only be expressed in eight bits (one byte), that is, 2** 8 = 256-1. Therefore, ASCII code can only represent 255 symbols at most. Note: Python 2.x uses ASCII coding by default

 

3.2 Chinese Character Set

GB2312

A total of 7445 characters are included, including 6763 Chinese characters and 682 other symbols. The internal code range of Chinese character area is high byte from B0-F7 and low byte from A1-FE. The occupied bits are 72*94=6768. Five of the vacancies are D7FA-D7FE.

GBK

GB2312 supports too few Chinese characters. The Chinese Character Extension Specification GBK 1.0 in 1995 contains 2186 symbols, which are divided into Chinese Character Area and Graphic Symbol Area. The Chinese character area includes 21003 characters.

GB18030

GB18030 in 2000 is the official national standard to replace GBK1.0. The standard contains 27484 Chinese characters, as well as Tibetan, Mongolian, Uygur and other major minority languages. Now PC platform must support GB18030, so there is no requirement for embedded products. So mobile phones and MP3 s generally only support GB2312.

From ASCII, GB2312, GBK to GB18030, these encoding methods are downward compatible, that is, the same character always has the same encoding in these schemes, the latter standard supports more characters. In these codes, English and Chinese can be processed in a unified way. The method of distinguishing Chinese encoding is that the highest bit of the high byte is not 0.

 

As programmers call it, GB2312, GBK, and GB18030 all belong to the double byte character set (DBCS). Traditional Chinese, big5.

 

GB2312 GBK GB18030 takes up two bytes and is downward compatible with GB18030 - > GBK - > GB2312 - > ASCILL

 

3.3  unicode

Unicode (Unicode, Universal Code, Single Code) is a kind of character coding used in computer. Unicode is designed to address the limitations of traditional character encoding schemes. It sets a unified and unique binary encoding for each character in each language. It stipulates that although some characters and symbols are represented by at least 16 bits (2 bytes).

Note: Python 3.x defaults to unicode encoding

 

3.4  utf-8

UTF-8 is the compression and optimization of Unicode encoding. Instead of using at least 2 bytes, he classifies all characters and symbols: ascii code content is saved in 1 byte, European characters are saved in 2 bytes, and East Asian characters are saved in 3 bytes.

 

3.5 Cases

#!/usr/bin/env python
# encoding: utf-8    # Tell the interpreter to use utf-8 Code
# Author: Sam Gao

name = 'Gao Shaoyang'      # python2.x

 print name
#!/usr/bin/env python3

# Author: Sam Gao

name = 'Gao Shaoyang'  # Default unicode Code

print(name)

 

IV. Input input

4.1 Simple Input

#!/usr/bin/env python3
# encoding: utf-8
# Author: Sam Gao

import getpass

_username = 'sam'
_password = '123456'

username = input('username:')
passwd = getpass.getpass('password:')   # stay pycharm It's not used in it. It can only be used in interactive mode or in interactive mode. linux Li Yi python3 passwd.py Execution, Function: Input will not be displayed

if _username == username and _password == passwd:
    print('welcome user {name} login'.format(name=username))
else:
    print('Invaild username or password')

4.2 Case Guess Age Game

#!/usr/bin/env python3
# encoding: utf-8
# Author: Sam Gao

age_of_sam = 28

while True:
    guess_age = int(input('guess age:'))

    if age_of_sam == guess_age:
        print('yes, you got it')

    elif age_of_sam > guess_age:
        print('think big')
    else:
        print('think small')

 

V while for Cycle and if Judgment

5.1 while True:

# Refer to case 4.2 to guess the age game. Use it when you don't know where the cycle is going.

 

5.2 Cases

Knowledge Points: 1. In the form of while... else...; 2. Use of break and continus; if and if... elif... else and if...else

#!/usr/bin/env python3
# encoding: utf-8
# Author: Sam Gao

age_of_sam = 28
count = 0


'''
//Guess three times. If you guess correctly, quit.
'''

# while True:
#     if count == 3:
#         break
#
#     guess_age = int(input('guess age:'))
#
#     if age_of_sam == guess_age:
#         print('yes, you got it')
#         break
#
#     elif age_of_sam > guess_age:
#         print('think big')
#     else:
#         print('think small')
#
#     count += 1
#####################################################
# while count < 3:
#     guess_age = int(input('guess age:'))
#
#     if age_of_sam == guess_age:
#         print('yes, you got it')
#         break
#
#     elif age_of_sam > guess_age:
#         print('think big')
#     else:
#         print('think small')
#
#     count += 1
#     if count == 3:
#         print('you guess too many times, fuck you !')

######################################################
#  perhaps

while count < 3:
    guess_age = int(input('guess age:'))

    if age_of_sam == guess_age:
        print('yes, you got it')
        break

    elif age_of_sam > guess_age:
        print('think big')
    else:
        print('think small')

    count += 1
else:
    print('you guess too many times, fuck you !')

5.3 Play for only three times

#!/usr/bin/env python3
# encoding: utf-8
# Author: Sam Gao

age_of_sam = 28

for i in range(3):
    guess_age = int(input('guess age:'))

    if age_of_sam == guess_age:
        print('yes, you got it')
        break

    elif age_of_sam > guess_age:
        print('think big')
    else:
        print('think small')

else:
    print('you guess too many times, fuck you !')


for i in range(1, 12, 3):   #   3 Representational step length
    print('loop:', i)

 

5.4 Wilful Play 5.2 Game

#!/usr/bin/env python3
# encoding: utf-8
# Author: Sam Gao

age_of_sam = 28
count = 0

while count < 3:  #   while True:
    guess_age = int(input('guess age:'))

    if age_of_sam == guess_age:
        print('yes, you got it')
        break                            # break  End the entire loop, not the parent loop
    elif age_of_sam > guess_age:
        print('think big')
    else:
        print('think small')

    count += 1
    if count == 3:
        while True:
            continue_play = input('Do you want to play:[yes/no]:')
            if continue_play == 'yes':
                count = 0
                break
            elif continue_play == 'no':
                break
            else:
                print('pls input valid string')
                continue             #   continus  Jump out of the current loop
else:
    print('you guess too many times, fuck you !')

 

6. String formatting

case

#!/usr/bin/env python3
# encoding: utf-8
# Author: Sam Gao

# username = input('usrname:')
# password = input('password:')
#
# print('username:', username)
# print('password:', password)

name = input('name:')   #  =====  python2 raw_input      python3   eval(input) =====  python2  input
age = int(input('age:'))   #  The default output is a string
job = input('job:')
salary = int(input('salary:'))

print('name:', type(name), 'age', type(age))

info = '''
----------------info of %s---------------
Name: %s
Age: %d
Job: %s
Salary: %d
''' % (name, name, age, job, salary)

# %s Character string    %d  integer      %f   Floating Point

info2 = '''
----------------info of {_name}---------------
Name: {_name}
Age: {_age}
Job: {_job}
Salary: {_salary}
'''.format(_name=name, _age=age, _job=job, _salary=salary)

info3 = '''
----------------info of {0}---------------
Name: {0}
Age: {1}
Job: {2}
Salary: {3}
'''.format(name, age, job, salary)

print(info)

print(info2)

print(info3)

Posted by hunainrasheed on Wed, 05 Jun 2019 12:25:06 -0700