Python-based basic operations on variables and files,
Python Introduction Python Founder:Guido (), Python name from the TV series Monty Python's Flying Circus (create a function between C and shell, easy to learn, easy to use, and scalable ), the first Python editor was born in 1991.Python philosophy:Simple, elegant, clearPython applications:
- Web Programing: Django, Pyramid, Bottle, Tornado, Flask, web2py
- GUI Development: wxPython, tklnter, PyGtk, PyGObject, PyQt
- Scientific and Numeric: SciPy, Pandas, IPython
- Software Development: Buildbot, Trac, Roundup
- System Administration: Ansible, Salt, OpenStack
Python types:
- Cpython: the official version of Python, which is implemented in C language and the most widely used. Cpython converts the source file (py file) into a bytecode file (pyc file) and runs on a Python virtual machine.
- Jython: java Implementation of Python. jython dynamically compiles Python code into java bytecode and then runs it on JVM.
- IronPython: C # Implementation of Python. IronPython compiles Python into C # bytecode and then runs it on the CLR.
- PyPy: Python implemented by Python. It compiles Python bytecode into machine code (fast execution)
- ...
Python Installation
Linux:
1. Download python2.7.11
Wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgz
2. decompress the file
Tar zxvf Python-2.7.11.tgz
3. Create an installation directory
Mkdir/usr/local/python27
4. Install python
./Configure -- prefix =/usr/local/python27
Make
Make install
5. Modify the ln pointing of the old version
Mv/usr/bin/python/usr/bin/python2.6.bak
Ln-s/usr/local/python27/bin/python/usr/bin/python
After modification, yum cannot be used (yum execution depends on Python). You need to put the header in/usr/bin/yum #! /Usr/bin/python #! /Usr/bin/python2.6
Windows:1. Download Python3.5.1 2. Double-click to install
Python internal execution process:When Python code is run, the code is converted to bytecode, and then from bytecode to machine code.
Python interpreter description and Content Encoding:At the beginning of the script, you must declare what interpreter is used to execute code and declare code (Python3 does not need to declare Content Encoding). The Python interpreter is loading. when the code in the py file is used, the content is encoded (the default is ascii). The declaration is as follows:
#!/usr/bin/env python# -*- coding:utf-8 -*-
Common encoding types:
Ascii: American standard information interchange code (8 bits)
Unicode: World code (at least 16 bits), which can represent all languages
UTF-8: automatically identifies single-line notes that require ascii or unicode encoding (space saving) and are represented by #, as follows:
# This line is commented out
Multi-line comments are represented by three single quotes (''') or double quotes ("), as follows:
'''Multiline comment multiline comment ''' "" multiline comment """
Changes
Variable name requirements:
- It can only contain letters, underscores, and numbers.
- The first character cannot be a number.
- Variable names should be easy to understand
- You cannot use special keywords when declaring variable names.
(False, class, finally, is, return, None, continue, for, lambda, try, True, def, from, nonlocal, while, and, del, global, not, with, as, elif, if, or, yield, assert, else, import, pass, break, break t, in, raise)
Input:Input can use the input (Python3) or raw_input (Python2.x) method. The usage is as follows:
Name = input ('enter content: ') # usage name = raw_input in Python3 ('enter content:') # usage in Python2.x
Data Type:
1. Single valueNumber: INTEGER (int): the maximum value is 2147483647 (2 ** 31-1) long integer (long): the maximum value is 9223372036854775807 (2 ** 63-1) float: 3.14 plural: z = a + bj Boolean value: True 1 False 0
2. String (str)1. String features: to modify a string, you must open up a space in the memory to declare a string:
name='akon'
2. String formatting: % s: String % d: Number
Name = 'akon' age = 99 sentence = 'I am % s, age % d' % (name, age) # convert % s and % d to the value of the corresponding variable
3. String common method: Index: You can use an index to retrieve a single character in a string. The usage is as follows. Slice: Multiple elements can be taken from the slice. The usage is as follows. Length Calculation len (): strip: removes spaces or line breaks on both sides of the string. The usage is as follows. Split: splits the string to form a list. The usage is as follows. For Loop: while LOOP: Always executes the loop when the condition is met (it is easy to form an endless loop)
3. list)List format:
name_list = ['akon','ak','cloris',12]
The List also supports subscript indexes, so it can be sliced: Calculate the list length (number of elements): delete list elements (del): Add list elements (append): Convert the list to a string (join):
4. tuple)Format of tuples:
Name = ('akon', 'lhk ', 'ak') # The difference with the list is that it cannot be modified.
5. Dictionary (dict)It is enclosed by a pair of braces. The dictionary consists of keys and values. Each pair of keys and values forms a key-value pair. Dictionary representation:
akon={'name':'akon','age':'26','gender':'man'}
The dictionary is unordered. If you use a for loop to output all elements, the zombie unordered output: The dictionary method:
Basic File Operations
Find the file:File Path E:/log
Open the file:
Open (file path, "w + ")
File_obj = open ('file path', 'Mode ')
File opening modes include:
R. open the file in read-only mode
W. Open a file for writing only. If the file already exists, it will be overwritten. If the file does not exist, it is created.
A. Open a file for append. If the file exists, the file pointer is placed at the end of the file, that is, after the existing content. If the file does not exist, create a new file for writing.
W +. Open a file for reading and writing. If the file exists, it is overwritten. If it does not exist, it is created.
File Operations, read/write
File_obj = open ('file path', 'Mode ')
File_obj.read () # Read all content into the memory file_obj.readlines () # Read all content into the memory, and branch for line in file_obj. readlines (): print linefor line in file_obj: # Read-Only row for each loop. Avoid reading all data into the memory file_obj.write () # Write file_obj.writeline () # Write a row each time
Close file
File_obj.close ()