Python simple getting started learning notes, python getting started learning notes
I. Input and Output
Print DISPLAY variable content
For example, print "hello, world", print ("hello, world") 3.0 only supports the second
Raw_input () reads the standard input and assigns the read data to the specified variable.
Example: name = raw_input ("Please input your name :")
Input () the value of the read variable is equivalent to eval ()
Example: name = input ("Please input your name :")
Please input your name: "test"
2.7 version 3.0
Raw_input () input ()
Input () eval ()
Ii. Notes
# The line comment symbol is similar to C ++ //
Print 'Hello world' # print hello world
You can also implement multiline comments by defining the format of multiline text.
1 "2 # Here is multi-line comment 3... 4... 5... 6 """
3. Condition judgment and indentation
If statement if-elif-else
1 sex = raw_input ("Input your sex:") 2 3 if sex = 'man': 4 print ("man") 5 elif sex = 'Woman ': 6 print ("") 7 else: 8 print ("sorry, incorrect input! ")
The code block uses indentation to represent the code logic, which is different from the braces in C.
Advantages: Concise and readable
Iv. Loop
1. The while loop is similar to the while loop in C ++.
1 while True: 2 sex = raw_input ("Input your sex: \ n") 3 if sex = 'man': 4 print ("man ") 5 elif sex = 'Woman ': 6 print ("") 7 else: 8 print ("sorry, incorrect input! ")
2. for Loop (count loop)
1 for I in range (3): 2 sex = raw_input ("Input your sex: \ n") 3 if sex = 'man': 4 print ("man ") 5 elif sex = 'Woman ': 6 print ("") 7 else: 8 print ("sorry, incorrect input! ")
The range built-in function indicates a list.
range(Stop)
range(Start,Stop[,Step])
If the step parameter is omitted, the default value is 1. If the start parameter is omitted, the default value is 0.
>>> range(10)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> range(1, 11)[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>>> range(0, 30, 5)[0, 5, 10, 15, 20, 25]>>> range(0, 10, 3)[0, 3, 6, 9]>>> range(0, -10, -1)[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]>>> range(0)[]>>> range(1, 0)[]
V. Data Types
1. Number
Int (integer)
In a 32-bit system, the integer number of digits is 32 bits. value range:-2 ** 31 ~ 2 ** 31-1, I .e.-2147483648 ~ 2147483647
In a 64-bit system, the integer number of digits is 64 bits. value range:-2 ** 63 ~ 2 ** 63-1, that is,-9223372036854775808 ~ 9223372036854775807
Long (long integer)
Different from the C language, The Length Integer of python does not specify the bit width, but because the machine memory is limited, the Length Integer cannot be infinitely large, so the Length Integer of python is limited by the total number of computer virtual memory.
From python2.3, if an integer overflows, python will automatically convert the integer data to an integer type. Therefore, if 'L' is not added after the integer type, it will not cause serious consequences, the two will be seamlessly integrated in future versions.
Float (float type)
It is used to process real numbers. Similar to the double type in C language, it occupies 8 bytes (64 bits), where 52 bits indicate the bottom, 11 represents the index, and the remaining one represents the symbol.
Let's talk about the binary of a floating point number.
Bool (Boolean)
Boolean values are special integer values: True and False. How to place a Boolean value in a numerical context, such as adding True and 1, True as 1, False as 0.
Complex (plural)
6.23 + 1.5j-1.23-1.23j
2. String
Strings in Python are defined as character set combinations between quotation marks. Python supports pair single or double quotation marks. Three or more consecutive single or double quotation marks can be used to contain special characters. (% S % d % f)
Common Operations on strings: Remove blank split Length Index slices
3. List and metadata
Different types of objects can be stored.
List:
Alist = [1, 2, 3, 4]
Common Operations: Index, slice, append, delete, length, loop, including
Tuples (read-only list ):
ATuple = ('robots', 'try ')
4. Dictionary
A dictionary is a ing data type in Python. It consists of key-value pairs. Almost all types of Python objects can be used as keys, but it is generally most commonly used as numbers or strings. The value can be any type of Python object. The dictionary elements are enclosed in braces.
ADict = {'host': 'global'} # create a dictionary
ADict ['Port'] = 80 # Add to Dictionary
Vi. Operators
1. Arithmetic Operators
+-* // % **
// Indicates the division of Floating Point Numbers (rounding)
/Traditional division
** Represents the multiplication party
Priority:
**> Plus and minus signs (+-)> (*,/, //, %)> + ,-
2. Comparison Operators
<<=>>==! = <>
3. logical operators
And or not (similar to C in &, |, ^)
4. bitwise operators
& Bitwise AND | bitwise OR ^ bitwise OR ~ Bitwise inversion <left shift> right shift
VII. File Operations
Open a file
File_obj = file ("file path", "Mode")
File_obj = open ("file path", "Mode")
Open File Mode
R. open the file in read-only mode.
W. Open a file for writing only. If the file already exists, overwrite it. If it does not exist, create a new file.
A. Open a file for append. If the file already exists, the file pointer will be placed at the end of the file, does not exist, create a new file to write.
W +, open a file for reading and writing. If the file exists, it will be overwritten. If it does not exist, a new file will be created and written.
In addition, there are a + rb wb rb + wb + AB + and so on.
Read File Content
# Load all content to the memory at a time
Obj. read ()
# Load all content to the memory at a time and split it into strings Based on rows
Obj. readlines ()
# Read a row at a time
For line in obj:
Print line
Write File Content
Obj. write ('content ')
Close file handle
Obj. close ()