Variables are used to store some intermediate results in the computation of programs. In order to facilitate future calls, variables must be descriptive.
Variable can be regarded as a container for information, marked and stored in memory, which can be used in the whole program.
student_number=30 studentNumber=30 #Hump Microsoft programmers often use
Naming rules for variables
To be descriptive
Variable names cannot be named in Chinese. They can only be numbers, letters, spaces or special characters
Cannot start with a number
Cannot use reserved characters
Cannot start with an uppercase letter
Constant = = constant quantity
The constant in python is variable, so the variable name in all uppercase means that the variable is constant.
Assignment of variables
name="OurNYeras" name2=name print(name,name2)
When is the memory freed for recycling?
Empty automatically when not in use, i.e. when not pointing.
age=21 print(age) del age #Hard dismantling
When reassigning, the original assignment is invalid. Python memory timing recovery mechanism.
Encoding (2.x does not support Chinese)
#!-*- coding:utf-8 -*- #coding:utf-8
Notes and input / output
Add "×" before the code to represent a single line comment
Add a comment after the code to comment on the code
Multiline comments are preceded by three single quotes or three double quotes
A program that interacts with a user.
name=input("your name:") age=input("your age:") print(name,age)
Enter an age and print how many years you can live.
#Convert the string to int, and use int (the transferred data) #To convert data into strings, use str death_age=200 age=input("your age:") #All data received by input is "string", even if the input is a number, it will be treated as a string. print("you can still live for",death_age-int(age),"years.")
age_of_men = 56 guess_age=int(input(">>:")) if guess_age==age_of_men: print("yes") elif guess_age>age_of_men: print("try samller.") else: print("try bigger.")
score = int(input("score:")) if score > 90: print("A") elif score > 80: print("B") elif score > 70: print("C") elif score > 60: print("D") elif score < 60: print("rolling")
# This program says hello and asks for my name. print("hello world!") print("what is your name?") #Ask for the name my_name = input() print("It is good o meet you,"+my_name) print("The length of your name is:") print(len(my_name)) print("What is your age?") #Ask age my_age = input() print("you will be"+str(int(my_age)+1)+"in a year.")