First, the variable name
Custom variable names can only be made up of numbers, letters, and underscores .
Before using a variable, you need to assign a value to the its first
Note: 1. Variable names cannot begin with a number;
2. cannot be a keyword; for example: ' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ',
' Finally ', ' for ', ' from ', ' global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ',
' Return ', ' try ', ' while ', ' with ', ' yield '
3. It is best not to duplicate what is built into Python, and two words are best used when writing code, such as Use_name;
About comments: single-line comment # ..... Content..................
Multi-line comment "" "..... Content ... "" "" "" ".
Second, the conditional statement 1.if Basic statement
If condition:
Internal code block # Internal block code cannot be aligned with if, typically leaving four spaces, i.e. a TAB key
Internal code block
Else
...
Print (' .... ')
For example:
If 1 = = 1:
Print ("Welcome to learn Python")
Print ("Welcome to the blog Park")
Else
Print ("Welcome to Southwest Petroleum University")
2. If support nesting
If 1 = = 1:
If 2 = = 2:
Print (" Welcome to learn python")
Print (" Welcome to the Blog Park ")
Else
Print (" Welcome to Southwest Petroleum University")
Else
Print (" welcome ")
3. If elif
INP = input (' Please enter score: ')
If InP = = "90":
Print (' excellent ')
Elif INP = = "80":
Print (' good ')
Elif INP = = "70":
Print (medium ')
Else
Print (' Pass ')
Print (' ... end ........)
Pass: If nothing is done after the condition, you can use the Pass,pass to empty the code, meaningless, just to represent the code block
For example
If 1==1:
Pass
Else
Print (' SB ')
III. basic data type 1. String
The string we've known so far is everything inside the quotation marks, and we've also called the string text, and the text and numbers are completely different.
To add two numbers and add two characters
Adding quotes to numbers adds up to string concatenation.
When creating a string, enclose the characters in quotation marks, either single quotes, double quotes, or Sanchong quotes, but not mash.
As name = "I am Gio"
name = ' I am Gio '
Name = "" I am Gio "" "
name = ' I am Gio '
For example: Let's go in the input string, and if you use single quotes, you'll get an error.
There are two ways to solve this problem: one is to use double quotes
The second is to escape the quotes in the string using the escape symbol (\)
Note: The string cannot end with \, in the string \ means that the string has not ended, and the newline continues to mean
2. Arithmetic operators
+ - * / % ** //
The first four are subtraction.
% means taking remainder
Represents a pickup
* * indicates power
3.while Cycle
While condition:
Action that the condition is true
Dead loop
While 1==1:
Perform actions
Exercises
1. Use while loop input 1 2 3 4 5 6 8 9 10
A=0 while a<10: a+=1 if a==7 : pass else: print(a)
2. For all numbers of 1-100
a=0s=0 while a<100: a+=1 s=s+a Print (s)
3. All odd numbers in output 1-100
A=0 while a<100: a=a+1 if a%2==0: pass Else : Print (a)
4. All even numbers in output 1-100
A=0 while a<100: a=a+1 if a%2==1: Pass else: print(a)
5, Beg 1-2+3-4+5 ... 99 of all numbers of the and
n = 1 # S is the sum of all previous numbers while n <: = n% 2 if temp = = 0 := s - nelse := s + n= n + 1 Print (s)
6. User login (three retry opportunities)
I=0 whileI<3: A= Input ("Please enter user name:") ifa=="1234": Print("Login Successful") Else: Print("Logon Failure") I=i+1Print("Please try again later .")
---restore content ends---
Life is short, I use Python's first article