14-day training camp for Python development-Chapter 1: python training camp
Chapter 1 python Variables
Functions of Variables
Stored data is called and operated by the program
Mark data
Declare Variables
Name = "Ydh" variable name = variable value
Variable definition specification:
Variable names can only be any combination of letters, numbers, or underscores
The first character of the variable name cannot be a number.
The following keywords cannot be declared as variable names [python syntax keywords, python built-in variables]
Variable naming conventions
1. Camper (uppercase for each letter)
AgeOfOldboy = 56
NumberOfStudents = 80
2. Underline (officially recommended)
Age_of_oldboy = 56
Number_of_students = 80
Define the Low Mode of Variables
Variable name: Chinese and Pinyin
Variable Name Too Long
Variable name is not satisfactory
For example:
Your age = 20 age_of_oldboy = 20
Ni_denianling = 20
The_ni_de_mingzi = 20 your_name = 20
Name1 = 1
Name2 = "Beijing"
Call variable
Print (age_of_oldboy)
Modify variable value
Age_of_oldboy = 30
Constant
For example, π = 3. 14 .....
There is no special syntax in python to represent changliang. The programmer agreed that the constant variable names should all be capitalized.
For example, AGE_OF_OLDBOY = 20
Read user input
Input ()
Name = input ("input name :")
Note
Purpose:
1. Comment out unnecessary code
2. Describe the meaning of the code segment
Code annotation principles:
1. Do not add comments to all
2. You only need to add comments to what you think is important or hard to understand.
3. Annotations can be in Chinese or English, but do not use Pinyin.
Python Data Type
Basic Type
Number
Integer int
Long Integer
Float
Negative
Character Set
Text str
Bytes
Boolean
True/Flase
Dataset
List
Tuple
Dictionary dict
Ordered dictionary
Unordered dictionary
Set
Ordered Set
Unordered set
Int (integer)
On a 32-bit machine, the integer has 32 digits and the value range is-2.3 ~ 231-1
In a 64-bit system, the number of digits of an integer is 64-bit and the value range is-2.63 ~ 263-1
Long (long integer)
The Length Integer of python does not specify the bit width. That is to say, python does not limit the size of long integer values, but in fact, due to the limited memory of the machine, the long integer values we use cannot be infinitely large.
Float
A simple understanding is decimal
String
In python, the characters enclosed in quotation marks are considered as strings (including single quotation marks, double quotation marks, and three quotation marks)
There is no difference between single double quotation marks. The combination of single double quotation marks must be considered only in the following cases.
Msg = "I'm 20 years old! "
Multiple quotes are used to enclose strings with multiple quotes.
Msg = ''' I love the rise of the Sun on Tiananmen Square in Beijing ''' print (msg)
View Code
String concatenation
Strings can be added and multiplied. Strings can only be concatenated with strings.
Name + age
Name * 10 print the name value 10 times
Boolean Type
True/False
It is used for logical judgment, whether it is True or False if it is incorrect!
Why do computers describe such a condition?
This is because it can be implemented based on the conditions, such:
if a > b : print ("this is a bigger number than b")else: print("this is a smaller number than b")View Code
Format output
% S represents a string
% D indicates a number
% F indicates float.
Operator
Arithmetic Operation
"+-*/, Take the remainder: %, power: ** x's y power, and take an integer //"
Comparison
"Equal to: =, not equal :! =, Not equal to: <>,>, <,> =, <="
Logical operation
"And, or, not"
Value assignment
"Equal to =, plus Equals + =, Minus equals-=, multiplication equals * =, Division equals/=, modulo equals % =, power equals ** =, and integer equals/ /="
Member operation
Identity Calculation
Bitwise operation
Process Control
Single Branch
If condition: the code to be executed after the condition is met
Dual Branch
If condition: the code to be executed after the condition is met. else: if the condition is not met, use the code here.
Multi-branch
If condition: the code elif to be executed after the condition is met: if the preceding condition is not met, the Code elif: if the preceding condition is not met, the Code elif: if the preceding conditions are not met, use this method.
While Loop
Syntax:
While condition: the condition is true and the code is executed...
Pass# Do nothing
Dead loop
count = 0while True: print ("count:", count) count += 1
Loop termination statement
Break is used to completely end a loop and jump out of the loop body to execute the following statement.
Continue jumps out of this loop and proceeds to the next loop
Exercise: Game of age Prediction
Exercise 1. Enter the name and gender to determine if a girl is a girl and print out my favorite girl. Otherwise, print them together to get the foundation!
Name = input ("input name:") sex = input ("input sex:") if sex = "female": print ("I like girls! ") Else: print (" get together! ") ''' Test result 1: input name: joininput sex! ----------- Test result 2: input name: Wei input sex: female I like girls! '''View Code
Exercise 2. Enter the name, gender, and age. If the girl is younger than 28 years old, print my favorite girl. Otherwise, print out the relationship between siblings!
Name = input ("input name:") sex = input ("input sex:") age = int (input ("input age :")) if sex = "female": if age <28: print ("I like girls") else: print ("it's good for siblings! ") ''' Test result 1: input name: Wei input sex: Female input age: 25 I like girls -------------- test result 2: input name: Wei input sex: Female input age: 29 siblings are also in love! Test result 3: input name: johninput sex: Male input age: 25: The output is empty.View Code
Exercise 3. enter your name, gender, and age. If you are a girl and younger than 28 years old, print my favorite girl. Otherwise, print out my siblings! If you are a boy, print them together to get the foundation!
Name = input ("input name:") sex = input ("input sex:") age = int (input ("input age :")) if sex = "female": if age <28: print ("I like girls") else: print ("it's good for siblings! ") Elif sex =" male ": print (" get together! ") Else: print (" Incorrect sex input! ")
Exercise questions in this section
1. Process Control;
A small program that matches the score. The score has five ABCDE levels. The relationship with the score is as follows:
A 90-100
B 80-89
C 60-79
D 40-59
E 0-39
While True: score = float (input ("input your score:") if score> 100 or score <0: print ("No such score") elif score> = 90: print ("A") elif score> = 80 and score <= 89: print ("B") elif score> = 60 and score <= 79: print ("C ") elif score> = 40 and score <= 59: print ("D") elif score> = 0 and score <= 39: print ("E ")View Code
2. Exercise by guessing age
Exercise 1
Optimized the age-based game, allowing users to guess up to three times, right in the middle, and jump out of the loop directly
Name = 25 count = 1 while True: user_input = int (input ("Guess age, enter age:") if count> 3: break elif user_input = name: print ("Congratulations! ") Break count + = 1View Code
Exercise 2
Optimized the age-based game, allowing users to guess three times at most. After three times, they asked if they would continue to play,
If the user selects y, it is allowed three more times and cyclically. If the user inputs n, it exits the program.
Name = 25 count = 1 while True: if count> 3: while True: judge = input ("whether to continue playing the game, y/n :") if judge = "y": count = 0 break elif judge = "n": print ("Thank you! ") Exit () else: print (" incorrect input! ") Else: user_input = int (input (" Guess age, enter age: ") if user_input = name: print (" Congratulations! ") Break elif user_input <25: print (" think bigger !! ") Elif user_input> 25: print (" think smaller !! ") Count + = 1View Code
3. while loop exercises
Exercise 1. 1-100 cycles
count = 1while count <= 100: print("loop:",count) count += 1View Code
Exercise 2. an even number in the loop 1-100:
Count = 1 while count <= 100: if count % 2 = 0: # the even number can be divisible by 2. The opposite is the base print ("loop:", count) count + = 1.View Code
Exercise 3. Print the value from 1 to 100 cyclically, and do not print the value for 50th times. Print the square of the corresponding value for 60 to 80 times.
Count = 1 while count <= 100: if count = 50: pass if count> = 60 and count <= 80: print ("the square of loop count is :", count * count) else: print ("loop:", count) count + = 1View Code
Exercise 4. loop termination statement
count = 1while count <= 100: print("loop:",count) if count == 5: break count += 1View Codewhile else
While condition match: The condition matches successfully. Run the code else here: the condition does not match. Run the code here.
Example:
Count = 1 while count <= 5: # When count <= 5, the condition matches and runs print ("loop:", count) count + = 1 else: print ("loop terminated! ") # If count = 6 does not match the while condition, run the code here.
Output:
Loop: 1
Loop: 2
Loop: 3
Loop: 4
Loop: 5
The loop is terminated!
Assignments in this Chapter:
Basic requirements:
Ask the user to enter the user name and password
Welcome information displayed after successful authentication
Enter an error three times and exit the program.
Upgrade requirements:
You can log on to multiple accounts (Note: store multiple accounts in the list)
After three Authentication failures, the user exits the program,
When you attempt to log on again, the program is still locked (Prompt: You need to save the user's locked status to the file)