1. The birth and application of Python
The founder of Python is Guido van Rossum (Guido van Rossum). 1989 Christmas period, Guido van Rossum (Chinese name: Uncle Turtle) in order to pass time in Amsterdam, determined to develop a new script interpreter, as an inheritance of the ABC language.
(Uncle Tortoise: 2005 joined Google to 2012, 2013 joined Dropbox until now, still grasp the core direction of Python development, known as benevolent dictator).
The July 2017 Tiobe leaderboard, Python has taken the fourth place, Python's elegance, clarity, and simplicity is an excellent and widely used language.
2. The difference between python2.x and Python 3.x
The 1th place is different:
python2.x: Source duplication, non-standard
Python3x: Integrate the source code, more clear and simple beautiful.
The 2nd place is different:
PYTHON2X: The default encoding is ASCII, resolution: #-*-Encoding:utf-8-*-
PYTHON3X: The default encoding is Utf-8
Specify interpreter: #!/usr/bin/env python
#-*-Encoding:utf-8-*-
The 3rd place is different:
PYTHON2X: A long, int type (print ' ABC ' Print (' abc ') is supported in both ways)
Python3x: no long, int type (print (' abc '))
The 4th place is different:
Python2x:raw_input () input () can only enter a numeric type
Python3x:input
3. The Division of Python language
The division of Python language:
Compile: Compile your code all at once into binary, then run.
Disadvantage: The development efficiency is low, cannot cross the platform.
Advantages: High execution efficiency.
Representative: C language
Explanatory: When the program starts running, it interprets the line of code as binary and executes it.
Disadvantage: Low execution efficiency.
Advantages: The development efficiency is high, can cross the quality table.
Representative: Python language
4. Variables & Constants
4.1. What are the variables?
Variables: The intermediate results of running the program are temporarily stored in memory for subsequent code calls.
4.2. Rules for variable definitions
Variable names can only be any combination of letters, numbers, or underscores, the first character of a variable name cannot be a number, and the keyword cannot be declared as a variable name
Keywords in python:
[' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']
4.3. Constants
Constants are constant quantities, such as Pai 3.141592653 ..., or quantities that do not change during a program's operation
举例:常量一般是大写,放到内容的顶部:AGE_OF_OLDBOY = 56
5. Comments
Single-line Comment: #
Multi-line Comment: ' Annotated content '
6. Basic data type
What is a data type?
We humans can easily distinguish between numbers and characters, but the computer does not, although the computer is very powerful, but from a certain point of view and very silly, unless you explicitly tell it, 1 is the number, "Han" is the text, otherwise it is not clear 1 and ' Han ' difference, therefore, in each programming language will have a called data type of East, in fact, is the common data types are clearly divided, you want the computer to do numerical operations, you will pass the numbers to it, you want him to deal with the text, send him a string type. Python has a number of commonly used data types, today we only talk about 3 kinds, numbers, strings, Boolean type
6.1, integer type (int).
int (integral type)
On a 32-bit machine, the number of integers is 32 bits and the value range is -2**31~2**31-1, which is -2147483648~2147483647
On a 64-bit system, the number of integers is 64 bits and the value range is -2**63~2**63-1, which is -9223372036854775808~9223372036854775807
Long (integer)
Unlike the C language, Python's long integers do not refer to the positioning width, that is, Python does not limit the size of long integer values, but in fact, because of limited machine memory, we use a long integer value can not be infinite.
Note that, since Python2.2, Python automatically converts integer data to long integers if an integer overflows, so it does not cause any serious consequences if you do not add the letter L after long integer data.
Note: There is no longer a long type in Python3, all int
6.2, String type (str).
In Python, the quoted characters are considered to be strings!
What's the difference between single quotes, double quotes, and multiple quotes? Let me tell you aloud that there is no difference between single and double quotes, only the following situation you need to consider a single pair of mates
"My name is Alex , I‘m 22 years old!"
What does a multi-quote function do? The function is that multiple lines of string must be in multiple quotes
msg = "" Today I want to write a poem, praise my deskmate, you see his black short hair, like a fried chicken. "Print (msg)
string concatenation: Strings can be added, multiplied, not reduced and removed
6.3, Boolean value (True,false)
Boolean type is simple, two values, a true (true), a false (false), mainly in mind logic
7. Formatted output
Existing practice needs, ask the user's name, age, work, hobbies, and then print into the following format
------------info of Alex Li-----------Name : Alex liage : 22job : teacherhobbie:girl-------------End----- ------------
How do you make it? You'll find that it's hard to make the output of this format with the character stitching, so learn the new poses together.
Just need to print the format first ready, because some of the information inside the user input, you can not preset to know, so it is possible to place a placeholder, and then the string of placeholders and external variables to map the relationship between the good
Name = input ("Name:") Age = Input ("Age:") job = input ("job:") Hobbie = input ("Hobbie:") info = ""------------info of%s---- -------#这里的每个%s is a placeholder for the name name :%s #代表 name Age :%s #代表 age job :%s #代表 Jo B Hobbie:%s #代表 hobbie-------------End-----------------"% (Name,name,age,job,hobbie) # This line is the number of the previous string with the extension The following variables are associated with print (info).
%s is the placeholder for the string, in addition,%d, is the number placeholder, if the above age after the change of%d, it means you must only enter the number
age : %d
We ran it, but we found something wrong ...
Say%d requires a number, not str, what? What we have entered is a number, 22,22.
Don't worry, don't trust your eyes. Let's debug and see if the input is a number. What do you think? What is the method for viewing data types? Type ()
name = input("Name:")age = input("Age:")print(type(age))
The execution output is
Name:AlexAge:22<class ‘str‘> #怎么会是strJob:IT
Let me tell you aloud that all input received by input is in string format by default!
What do you do if you want the program to be error-prone? Simple, you can turn str into int
age = int( input("Age:") )print(type(age))
I'm sure it's okay. Instead, can you turn the string into a number? Must be,str( yourStr )
Question: Now there's this line of code
msg = "I am%s, age%d, currently learning progress is 80%"% (' jinxin ', ') print (msg)
This will be an error, because in the formatted output, you appear% default is the placeholder%, but I think the last 80% in the above statement is to represent 80% instead of placeholders, what to do?
msg = "I am%s, age%d, currently learning progress is 80%"% (' jinxin ', ') print (msg)
That's it, the first percent is the translation of the second%, telling the Python interpreter that this is just a simple%, not a placeholder.
8. Basic operators
Operator
Computer can be carried out many kinds of operations, can not only subtraction so simple, operations by type can be divided into arithmetic operations, comparison operations, logical operations, assignment operations, member operations, identity operations, bit operations, today we only learn arithmetic operations, comparison operations, logical operations, assignment operations
Arithmetic operations
The following hypothetical variables: a=10,b=20
Comparison operation
The following hypothetical variables: a=10,b=20
Assignment operations
The following hypothetical variables: a=10,b=20
Logical operations
Further research for logical operations:
1, in the absence of () the not priority is higher than And,and priority above or, that is, the priority relationship is () >not>and>or, the same priority is computed from left to right.
Examples:
Determine the true,false of the following logical statements.
1,3>4 or 4<3 and 1==12,1 < 2 and 3 < 4 or 1>2 3,2 > 1 and 3 < 4 or 4 > 5 and 2 < 14,1 > 2 an D 3 < 4 or 4 > 5 and 2 > 1 or 9 < 85,1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
6,7 <6
2, x or Y, X is true, the value is X,x is False, the value is y;
X and Y, X is true, the value is y,x false, and the value is x.
Example: Find the values of the following logical statements.
8 or 4 and 3 or 7 or 9 and 6
In,not in:
Determines whether the child element is in the original string (dictionary, List, collection):
For example:
#print (' Like ' in ' dkfljadklf like Hfjdkas ') #print (' A ' in ' Bcvd ') #print (' Y ' isn't in ' Ofkjdslaf ')
python2.x is different from Python 3.x