To understand the python The download You don't have to watch online classes to learn python The article ( The first day )_Why_does_it_work The blog of -CSDN Blog
Today officially entered python Learning from :
python Its grammar is relatively simple , First
Catalog
We said Coding for so long , How much do you know about running ?_Why_does_it_work The blog of -CSDN Blog
Programs are used to process data , Variables are used to hold data , So in python Code for ( Program ) in , How variables are used :
1. Definition of variables
python The definition of language variables is relatively simple , But in python Must assign a value to a defined variable in , Only by assigning this variable will the format be created : Variable name = value
vx="123456"
Because it's followed by a string , So the string should be quoted
The above code only needs to enter vx You can output it directly 123456, If you need to output directly 12356, Then we need to use printf function ( take print The functions inside are output to the console
vx="123456"
printf(vx)
2. Using variables
After the variable definition is complete , Variables can be used directly , But how to define the variables to be calculated ?
price=8.5
weigh=7.5
money=price*weigh
print(money)
As shown in the above code , It can be defined by other variables
3. Defining a variable in memory requires
Variable name , Data saved by variables , The type of data the variable holds , And the address of the variable
4. Variable type
stay python In language , There is no need to add data types , Because in python The interpreter will accurately deduce the correct data type according to the right side of the assignment
The string needs to be added when assigning a value to a variable ”“ The string type is str
Integer types don't need to add anything , Just follow the numbers The integer type is int
Boolean types don't need to add anything ( Boolean types are true and false types ) The boolean type is bool( No 0 It's all true 1)
Floating point types don't need to add anything Floating point type is float
To sum up
stay python There are two types of statements , One is the number type , One is a non numeric type
Numeric type Including integer type (int) Floating point type (float) Boolean type (bool) There is also a plural type (complex)
Non numeric type Including strings , list , Tuples , Dictionaries
int(x) The function can convert a string to an integer
float(x) The function converts a string to a decimal
Strings can only be added and spliced , You can't multiply. You must convert the string to decimal and integer
Analyze the data type of the variable , It has to be said that type function , Main forms
type( Variable )
The purpose of this function is to determine the function type , After defining the variables , Input type Function can output the data type of this variable
1. Input
Input Is to use the code to obtain the information entered by the keyboard , stay python Need to use input function , The function is to obtain keyboard input information
input(" Please enter your password ")
Please enter your password :123546
obtain :123456
input The function is right there c Language equals printf+scanf,printf In parentheses
If we want to use input The keyboard input is saved by assignment
vx=input(" Prompt information ")
Because in python You can only use numeric type for multiplication, not string type for multiplication , So we need to convert
price=float(input(" price is "))
2. Output
Format output
I want to output data when I output text , You need to use formatted output
%s Output string
%d Output integer %06d( If you want to output a six bit integer, there is 0)
%f Output floating point number %.2f Indicates two digits after the decimal point of the output
%% Output %
This and c The language situation is similar
name=Why_does_it_work
print(" My name is %s, Please take good care of me ”%name)
keyword Is in the python Identifiers already used in the language
1.import tool kit
import You can use the toolkit , You can use the keywords in the toolkit
Arithmetic operator
+ Add , Add two objects ; a+b The output is 8
- reduce , Subtract two objects or get a negative number ; a-b The output is 4
* ride , Multiply two objects or return a string that is repeated several times ; a*b The output is 12
/ except , Two objects divide ; a/b The output is 3.0
% modulus , Find the remainder of division ; a%b The output is 0
** power - return x Of y The next power a**b The output is 36
// Divide and conquer - Take down the whole number close to the quotient a//b The output is 3
Comparison operator
== be equal to , Compare objects for equality ; (a == b) return False;
!= It's not equal to , Compare two objects for inequality ; (a != b) return True;
> Greater than , return x Is it greater than y; (a > b) return False;
< Less than , return x Is less than y; (a < b) return True;
>= Greater than or equal to , return x Greater than or equal to y; (a >= b) return False;
<= Less than or equal to , return x Less than or equal to y; (a <= b) return True;
Assignment operator
Operator effect example
= Simple assignment operators c = a + b take a + b The operation result of is assigned as c ;
+= Addition assignment operator c += a Equivalent to c = c + a ;
-= Subtraction assignment operator c -= a Equivalent to c = c - a ;
*= Multiplication assignment operator c *= a Equivalent to c = c * a ;
/= Division assignment operator c /= a Equivalent to c = c / a ;
%= Modulo assignment operator c %= a Equivalent to c = c % a ;
**= Power assignment operator c * *= a Equivalent to c = c ** a ;
//= Integer division assignment operator c //= a Equivalent to c = c // a ;
:= Walrus operators , You can assign a value to a variable inside an expression .
An operator
& Bitwise operators , Two values involved in the operation , Both corresponding bits are 1 when , The result of this bit is 1, Otherwise 0;
| bitwise or operator , One of the corresponding two binary bits is 1 when , The result bit is 1;
^ bitwise exclusive or operator , When two corresponding bits are different , The result is 1;
~ Bitwise negation operator , Invert every bit of data , Namely the 1 Turn into 0, hold 0 Turn into 1;
<< Left move operator , All the binary bits of the operand are shifted to the left by several bits , from "<<" The number on the right specifies the number of digits to move , High level discard , Low complement 0;
>> Move right operator , hold ">>“ Each binary of the left operand is shifted several bits to the right ,”>>" The number on the right specifies the number of digits to move
Logical operators
Operator expression effect
and x and y And , x by False,x and y return x Value , Otherwise return to y Calculated value ;
or x or y or ,x by True,x or y return x Value , Otherwise return to y Calculated value ;
not not x Not ,x by True, return False , x by False, return True
I'm going to class , That's it , By the end c After the grammar of the language python It will be easy to understand , Write the next one after class , The next one is python The statement module of !!