- variable
- Variable type
Variables can specify different data types, which can store integers, decimal or characters.
2.Variable assignment
- Each variable must be assigned before it is used, and the variable will be created after assignment.
- An equal sign (=) is used to assign variables.
- The equality sign (=) operator has a variable name on the left, and the equality sign (=) operator has a value stored in the variable on the right. For example:
-
1 # autor:guangqing xu 2 counter =100 #Assigned integer variables 3 miles = 1000.0 #float 4 name = "guangqing" #Character 5 print(counter) 6 print(miles) 7 print(name)
Expression if… Else
- 1、Scene 1
# Prompt for user name and password.
# Verify username and password
# If it is wrong, the user name or password error is exported.
# If successful, output is welcome, XXX!
-
1 # autor:guangqing xu 2 _username = "guangqing" 3 _password = "abc123" 4 username = input("username:") 5 password = input("password:") 6 if _username==username and _password==password: 7 print("Welcome user {name} login...".format(name=username)) 8 else: 9 print("Invalid username or password!")
1 import getpass 2 name= raw_input("Please enter user name:") 3 password = getpass.getpass("Please input a password:") 4 if name=="guangqing" and password=="abc123": 5 print("Welcome, Guangqing.") 6 else: 7 print("User name and password error")