Python path: Day01, python path day01
Content of this section
1. Python Introduction
2. Development History
3. Variables
4. user input
5. Expression if... else statement
6. Expression for Loop
7. expression while loop
8. Initial Knowledge of the module
9. Data Type first recognized
10. Data Operations
I. Introduction to Pyhon I. Compilation and interpretation languages
A compiled language translates a source program into executable target code. The translation and execution are separated. An interpreted language is a one-time translation and execution of the source program, do not generate a storage target code. This is just a representation. The biggest difference between the two is that for interpreted language execution, control is in the interpreter rather than in the user program while for compiled language execution, the runtime control is in the user program.
Literally, both "Compilation" and "interpretation" have the meaning of "Translation". The difference between them is that the timing of translation is not the same. For example, if you want to read an external document and you do not know this foreign language, you can find a translator, give him enough time to let him translate the whole book from start to end, and then give you the mother-tongue version of the book for reading. Or, you can immediately ask the translator to help you read the book, let him translate a sentence for you. If you want to look back at a chapter, he has to translate it for you again.
The former is equivalent to the compiled type we just mentioned: Converting all the code into machine languages at a time and writing it into executable files; the latter is equivalent to the interpreted type we want to talk about.
2. Static and Dynamic languages
Dynamic type language: a dynamic type language is a language that performs data type checks during runtime. That is to say, when programming in a dynamic type language, you never need to specify a data type for any variable. This language records the data type in the internal part when you assign a value to the variable for the first time.
Static type language: the static type language is the opposite to the dynamic type language. Its data type is checked during compilation, that is, the Data Types of all variables must be declared during program writing.
Based on the abovePython is a dynamic interpreted language.
Ii. Pyhon Development History
In 1991, the first Python compiler was born. It is implemented in C language and can call library files in C language. From birth, Python already has core data types including classes, functions, exception handling, tables and dictionaries, and module-based extended systems.
Python 1.0-January 1994 adds lambda, map, filter and reduce.
Python 2.0-October 16,200 0, added the memory reclaim mechanism, forming the foundation of the current Python language framework
Python 2.4-November 30,200 4, the most popular WEB framework Django was born in the same year.
Python 2.5-September 19,200 6
Python 2.6-October 1, 2008
Python 2.7-July 3, 2010 officially announced that python only supports
Python 3.0-December 3, 2008
Python 3.1-June 27,200 9
Python 3.2-February 20,201 1
Python 3.3-September 29,201 2
Python 3.4-March 16,201 4
Python 3.5-September 13,201 5
So now I chose to use Python3.5, and all subsequent code runs in this environment.
Iii. Variable 1. Declare Variables
#!/usr/env python# -*- coding:utf-8 -*-name = "xiaoming"
The code above declares a variable named: name. The value of the variable name is: "xiaoming"
2. Variable assignment
name = "xiao ming" name2 = nameprint(name,name2) name = "xiao hua" print("What is the value of name2 now?"name,name2)
III. Rule for defining variable names:
1. Variable names can only be any combination of letters, numbers, or underscores
2. the first character of the variable name cannot be a number.
3. The following keywords cannot be declared as variable names
['And', 'as', 'assert ', 'Break', 'class', 'contine', 'def', 'del ', 'elif ', 'else', 'Got t', 'exec ', 'Finally', 'for ', 'from', 'global', 'if', 'import', 'in ', 'Is ', 'lambda', 'not ',' or ', 'pass', 'print', 'raise ', 'Return', 'try', 'while ', 'with', 'yield ']
Note
When the line comment: # the content to be commented out
Multi-line comment: '''comment content '''
Iv. user input
#! /Usr/env python #-*-coding: UTF-8-*-name = input ("enter your name:") print ("Hello", name)
When entering the password, if you want to be invisible, you need to use the getpass method in the getpass module, that is:
#! /Usr/bin/env python #-*-coding: UTF-8-*-import getpasspwd = getpass. getpass ("enter the password:") # print the input content print (pwd)
However, PyCharm in windows is not easy to use.
5. Expression if... else 1. user login verification
# Prompt for entering the user name and password # verify the user name and password # If the user name or password is incorrect, output the user name or password is incorrect # If the user name is successful, output welcome, XXX! #! /Usr/bin/env python #-*-coding: encoding-*-name = "xiao gou" password = "123" username = input ('enter your username :') pwd = input ('enter password: ') if name = username and password = pwd: print ("welcome", name) else: print ("Incorrect username and password ")
2. Guess the age
Set your age in the program, and start the program to let the user guess. After the user inputs, the system prompts whether the user inputs correctly. If the input is incorrect, indicates whether to guess whether it is big or small.
1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 4 my_age = 28 5 6 user_input = int (input ("enter a number :")) 7 8 if user_input = my_age: 9 print ("Congratulations! Sorry! ") 10 elif user_input <my_age: 11 print (" you have guessed it! ") 12 else: 13 print (" You guessed it! ")
Guess age
6. expression for the simplest loop for 10 times
#!/usr/bin/env python# -*- coding: encoding -*-for i in range(10): print("loop:", i )
Output:
loop: 0loop: 1loop: 2loop: 3loop: 4loop: 5loop: 6loop: 7loop: 8loop: 9
Requirement 1: The above program is used, but the number of cycles smaller than 5 does not go, directly jump into the next loop
For I in range (10): if I <5: continue # Do not go down, go directly to the next loop print ("loop:", I)
Continue jumps out of this loop
Requirement 2: The above program is used, but the process does not go if the number of cycles is greater than 5. Exit directly.
For I in range (10): if I> 5: break # Don't go down, directly jump out of the entire loop print ("loop:", I)
Break directly jumps out of the entire loop and exits the program.
7. expression while
There is a kind of loop called an endless loop.
Count = 0 while True: print ("You are wind, I am sand, entangled in the world...", count) count + = 1
It is better to reduce the number of endless loops. The code above will exit after one hundred Loops
Count = 0 while True: print ("You are wind, I am sand, entangled in the end of the world... ", count) count + = 1 if count = 100: print (" so far! ") Break
8. Module first recognized sys module
#! /Usr/env python #-*-coding: UTF-8-*-# Version: Python3.4import sysprint (sys. path) # print the environment variable print (sys. argv) # output $ python test. py helo world ['test. py', 'HELO', 'World'] # obtain the parameters passed during script execution.
OS Module
#! /Usr/bin/env python #-*-coding: UTF-8-*-import OS. system ("dir") # Call system commands
Fully integrated
Import OS, sys OS. system (''. join (sys. argv [1:]) # Use your input parameters as a command and submit it to OS. system for execution.
9. Data Types first recognized 1. Numbers
1. int (integer)
On a 32-bit machine, the number of digits of an integer is 32 bits and the value range is-2 ** 31 ~ 2 ** 31-1, I .e.-2147483648 ~ 2147483647
In a 64-bit system, the number of digits of an integer is 64-bit, and the value range is-2 ** 63 ~ 2 ** 63-1, that is,-9223372036854775808 ~ 9223372036854775807
2. long (long integer)
3. float (float type)
A floating point number is used to process a real number, that is, a number with a decimal number.
Ii. Boolean Value
True or false
1 or 0
True or False
Iii. String
"hello world"
String concatenation: a string in python is a character array in C. Each time you create a string, you need to open a continuous empty string in the memory. Once you need to modify the string, it is necessary to open up space again, and every time the "+" icon appears, it will open up a space from it.
String formatting output
Name = "xiao ming" print "I am % s" % name # output: I am xiao ming
PS: the string is % s; the integer is % d; the floating point is % f
String common operations:
Remove Blank
Split
Length
Index
Slice
Iv. List
Creation list:
Name_list = ['Alex ', 'seven', 'Eric'] Or name_list = list (['Alex ', 'seven', 'Eric'])
Basic operations:
Index
Slice
Append
Delete
Length
Slice
Loop
Include
5. tuples (immutable List)
Create tuples
Ages = (11, 22, 33, 44, 55) or ages = tuple (11, 22, 33, 44, 55 ))
6. Dictionary (unordered)
Create a dictionary
Person = {"name": "mr. wu ", 'age': 18} or person = dict ({" name ":" mr. wu ", 'age': 18 })
Common Operations:
Index
New
Delete
Key, value, and key-Value Pair
Loop
Length
10. Data Operations
Arithmetic Operation:
Comparison:
Assignment operation:
Logical operation:
Identity calculation:
Bitwise operation:
Ternary Computation
Result = value 1 if condition else value 2
If the condition is true: result = value 1
If the condition is false: result = value 2
a,b,c = 1,3,5d = a if a > b else cprint(d)# d = 5