python basis day1

Day1 notes

1. Programming Language Category

  • Compiled

    The code is compiled into binary all at once, and then execute

    Advantages: high efficiency

    Disadvantages: low efficiency of development can not be cross-platform

    Representatives Language: C

  • Interpreted

    Progressive interpreted as a binary, line by line run

    Advantages: development of high efficiency, cross-platform

    Disadvantages: low efficiency

    Representatives Language: python

2. Variable usage rules

  • All variables in any combination of numbers, letters and underlined
  • You can not begin with a number
  • You can not be a python keyword
    • ['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']
  • You can not use Chinese
  • Not too long
  • To descriptive
  • Recommended naming
    • Hump ​​body: AgeOfOldboy = 73
    • Underline: age_of_oldboy = 73

3. Constant

  • Life has been the same: ID number, name
  • python is not true constants, variables called constants in all uppercase
  • The variables in all caps, on the top of the file
NAME = '杨森'
print(NAME)

4. Comment

  • Explanation easy to understand the code
  • Single-line comments: #
  • Multi-line comment: "" "annotation content is" "" or '' 'is the annotation content' ''

  • Difficult to understand the code, functions, classes, files are required to comment, explanation

The basic data types

  • int (integer): 1,2,123 ......

  • str (string): python who quotes in the string data is referred to

    Note: string can be multiplied with the digital

  • bool: Only two forms True False

Analyzing the data type of a variable

s1 = 100
s2 = '100'
print(s1,type(s1))
print(s2,tpye(s2))

6. User interaction input

  • Page, enter the account password on APP
  • input out of all types are strings
username = input('请输入用户名:')
password = input('请输入密码:')
print(username,',您好!')
print(username, type(username))
print(password, type(password))

Small practice : allowing users to enter the name, sex, age, and then print the word 'My name: Gender: year: '

name = input('请输入您的姓名:')
sex = input('请输入您的性别:')
age = input('请输入您的年龄:')
msg = '我叫' + name + ',性别' + sex + ',今年' + age
print(msg)

7. The flow control statements if

  1. Alone if
if 1 < 2:
    print("aloha")
  1. if else
age = int(input("请输入您的年龄:"))  # 把字符串转换成数字
if age >= 18:
    print("你可以做羞羞的事情了[滑稽]")
else:
    print("回家喝奶去吧")
  1. if elife elife ……
number = 6
guess_num = int(input("猜数字(1-10):"))
if guess_num < number:
    print("猜小了")
elif guess_num > number:
    print("猜大了")
else:
    print("猜对了")
  1. Nested if
username = input("please enter your name:")
password = input("please enter your password:")
code = 'abcd'
enter_code = input("please enter code:")
if enter_code == code:
    if username == '杨森':
        if password == '1234':
            print("Welcome to the Python World!")
        else:
            print("password error")
    else:
        print("user does not exist!")
else:
    print("code error!")

Guess you like

Origin www.cnblogs.com/west-yang/p/12521736.html