Day01: The Path to Python: The Path to day01python

Source: Internet
Author: User

Day01: The Path to Python: The Path to day01python
1. What language is Python?

Python is a dynamically interpreted language with a strong type definition.

(1) Dynamic and Static languages

① Static language: a language that performs data type check during compilation, such as C and C ++.

② Dynamic Language: a language that performs data type check during running, such as Python.

When programming in a dynamic language, you do not need to specify the data type for any variable. This language records the data type when you assign a value to the variable for the first time.

(2) compiled and interpreted languages

① Compiled language: converts the source file to the target file at one time.

If the program code we write is included in a source file, an executable file will be generated after compilation, and we can run it directly.

If the project is complex and the program code is scattered among multiple source files, after compilation, a source file will usually correspond to a target file. At this time, we need to link the program (Linker) convert all target files into executable files.

② Interpreted language: At the moment before the program runs, only the source program does not have an executable program. Each time the program executes a certain instruction of the source program, A shell called an interpreter converts the source code into binary code for execution. All in all, it is to constantly explain, execute, explain, and execute... So, interpreted programs cannot be separated from interpreted programs.

Because interpreted language puts the compilation process into the execution process, it determines that interpreted language is destined to be slower than compiled language, it is not surprising that the speed gap is several hundred times faster.

(3) strong type definition and weak Type Definition Language

① Strong Type Definition Language: Once a variable is specified with a data type, it will always be the data type if it is not forcibly converted. Therefore, a strongly typed language is a type-safe language.

② Weak type definition language: language in which data types can be ignored. It is opposite to a strongly typed definition language. A variable can assign values of different data types.

The speed of a strong-type definition language may be slightly inferior to that of a weak-type definition language, but the rigor of a strong-type definition language can effectively avoid many errors.

 

2. Python Interpreter

Python has many interpreters, but CPython is the most widely used. If a program needs to interact with a Java or. Net platform, the best way is not to use Jython or IronPython, but to interact through network calls to ensure the independence of each program. There is also a Python Interpreter: PyPy, which aims to improve the execution speed.

 

3. rules defined by variables in Python

(1) The variable name can only be any combination of letters, numbers or underscores;

(2) The first character of the variable name cannot be a number;

(3) KEYWORDS cannot be declared as variable names.

Python has the following keywords:

And as assert break class continue def del

Elif else should t exec finally for from global

If import in is lambda not or pass

Print raise return try while with yield

 

4. the first program: Hello World!
 1 # Author:GCL 2  3 s="Hello World" 4 print(s) 5  6 name="gcl" 7 name1=name 8 print(name,name1) 9 name="lmy"10 print(name,name1)
Hello World!

Output:

Note: The default encoding method of Python3 is UTF-8.

 

5. user input and formatted output

Input Function: input ()

1 # Author: GCL 2 3 # user input function: input () 4 name = input ("name:") 5 age = int (input ("age :")) 6 7 "8 Mode 1: Data splicing, but it will open up multiple memory blocks, low efficiency, we do not recommend 9 "10 info1 =" 11 = Personal Information 1 "+ name +" = 12 name: "+ name +" 13 age: "+ str (age) 14 print (info1) 15 16" 17 Method 2: format the output: % 18 "19 info2 =" 20 = Personal Information 2% s = 21 name: % s22 age: % d23 "" % (name, name, age) 24 print (info2) 25 26 "27 method 3: format the output: format28 "29 info3 =" 30 ==== Personal Information 3 {_ name }==== 31 name: {_ name} 32 age: {_ age} 33 """. format (_ name = name, _ age = age) 34 print (info3) 35 36 "37 Method 4: format the output: format38 "39 info4 =" 40 ==== personal information IV {0 }==== 41 name: {0} 42 age: {1} 43 """. format (name, age) 44 print (info4)
Input and Output
1 Name: gcl 2 age: 23 3 4 ==== Personal Information 1 gcl ==== 5 Name: gcl 6 age: 23 7 8 ===== Personal Information 2 gcl ==== 9 name: gcl10 age: 2311 12 13 ===== Personal Information 3 gcl ==== 14 name: gcl15 age: 2316 17 18 ===== personal information 4 gcl ==== 19 Name: gcl20 age: 23
Output

 

6. Operator priority

 

7. Expression if... else
1 # Author: GCL 2 3 _ userName = "gcl" 4 _ passWord = "123" 5 6 userName = input ("userName:") 7 passWord = input ("passWord: ") 8 9 if userName = _ userName and passWord = _ passWord: 10 print (" Welcome {name} user ". format (name = userName) 11 else: 12 print ("incorrect user name or password ")
If... else

 

8. Game of age Prediction
1 # Author: GCL 2 3 oldNum = 56 4 5 # Method 1: while LOOP 6 count = 0 7 while count <3: 8 newNum = int (input ("Please input: ") 9 count + = 110 if newNum = oldNum: 11 print (" You guessed it! ") 12 break13 elif newNum> oldNum: 14 print (" I guess it's big! ") 15 else: 16 print (" I guess it's okay! ") 17 else: 18 print (" You guessed too many times ") 19 20 # Method 2: for Loop 21 for I in range (, 1 ): 22 newNum = int (input ("Enter:") 23 if newNum = oldNum: 24 print ("You guessed it! ") 25 break26 elif newNum> oldNum: 27 print (" I guess it's big! ") 28 else: 29 print (" I guess it's okay! ") 30 else: 31 print (" You guessed too many times ")
For and while guess age

 

Postscript:

This is my first blog. I certainly have many shortcomings, and many others need improvement and improvement. I welcome your criticism.

Time: 2018.03.30

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.