01
Common commands of Python in Linux:
Go to: python
# You can install ipython with the code prompt function.
Exit: exit ()
Go to the Python command line
Clear screen command: ctrl + L
View help () # ex. help (list)
Python provides built-in functions for hexadecimal conversion:
Bin ()
Hex ()
02
Python file type:
1. Source Code. py
Run the. py file in the following format:
Add the following to the first line of the demo1.py file:
#! /Usr/bin/python
Print 'hello, world'
Add executable permissions to the file:
Chmod + x demo1.py
You can execute the py file as follows:
./Demo1.py
2. byte code
Python source file generated after compilation with the extension of. pyc
Compilation Method:
Import py_compile
Py_compile.compile ("demo1.py ")
---> Generate demo1.pyc
3. Optimize code
Optimized source file with the extension ". pyo"
>>> Python-O-m py_compile demo1.py
Generate demo1.pyo
Python program example:
#!/usr/bin/python# import modules used here -- sys is a very standard oneimport sys# Gather our code in a main() functiondef main():print 'Hello there', sys.argv[1]# Command line args are in sys.argv[1], sys.argv[2] ...# sys.argv[0] is the script name itself and can be ignored# Standard boilerplate to call the main() function to begin# the program.if __name__ == '__main__':main()
Run this program:
$./Hello. py Alice
Hello there Alice
Function:
# Defines a "repeat" function that takes 2 arguments.def repeat(s, exclaim):result = s + s + s # can also use "s * 3" which is faster (Why?)#the reason being that * calculates the size of the resulting object once whereas with +, that calculation is made each time + is calledif exclaim:result = result + '!!!'return result
Function call:
def main():print repeat('Yay', False) ## YayYayYayprint repeat('Woo Hoo', True) ## Woo HooWoo HooWoo Hoo!!!Indent:
Python uses four spaces as indentation. For details, refer to the following documents:
See here: http://www.python.org/dev/peps/pep-0008/#indentation
03
Python variable
View the variable memory address: id (var)
Online help tips:
Help (len) -- docs for the built in len function (note here you type "len" not "len ()" which wocould be a call to the function)
Help (sys) -- overview docs for the sys module (must do an "import sys" first)
Dir (sys) -- dir () is like help () but just gives a quick list of the defined symbols
Help (sys. exit) -- docs for the exit () function inside of sys
Help ('xyz '. split) -- it turns out that the module "str" contains the built-in string code, but if you did not know that, you can call help () just using an example of the sort of call you mean: here 'xyz '. foo meaning the foo () method that runs on strings
Help (list) -- docs for the built in "list" module
Help (list. append) -- docs for the append () function in the list modul
04
Operator
Raw_input () // obtain the value from the keyboard
Ex.
A = raw_input ("please input :")
B = raw_input ("please input B :")
05
Data Type
Type () can view the data type of a character
Double quotation marks: Commonly Used for comments. functions are often used for doc data areas.
""
Str
"
Python string:
Python strings are "immutable" which means they cannot be changed after they are created (Java strings also use this immutable style ).
S = 'Hi' print s [1] ## iprint len (s) #2 print s + 'there' ## hi there # print raw = r'this \ t \ n and that '# r'str' in the new line indicates the Declaration is a raw string, print raw # this \ t \ n and thatmulti = "It was the best of times. it was the worst of times. "pi = 3.14 # text = 'the value of pi is '+ pi # NO, does not worktext = 'the value of pi is' + str (pi) # yes
Common string methods:
s.lower(), s.upper() #-- returns the lowercase or uppercase version of the strings.strip() #-- returns a string with whitespace removed from the start and ends.isalpha()/s.isdigit()/s.isspace()... #-- tests if all the string chars are in the various character classess.startswith('other'), s.endswith('other') #-- tests if the string starts or ends with the given other strings.find('other') #-- searches for the given other string (not a regular expression) within s, and returns the first index where it begins or -1 if not founds.replace('old', 'new') #-- returns a string where all occurrences of 'old' have been replaced by 'new's.split('delim') #-- returns a list of substrings separated by the given delimiter. The delimiter is not a regular expression, it's just text. 'aaa,bbb,ccc'.split(',') -> ['aaa', 'bbb', 'ccc']. As a convenient special case s.split() (with no arguments) splits on all whitespace chars.s.join(list) #-- opposite of split(), joins the elements in the given list together using the string as the delimiter. e.g. '---'.join(['aaa', 'bbb', 'ccc']) -> aaa---bbb---cccFor more information, see the following documents:
Http://docs.python.org/2/library/stdtypes.html#string-methods
Formatted output of string: % Operator
# % Operatortext = "% d little pig come out or I'll % s and % s" % (3, 'huff', 'puff ', 'blowlow') # because this line is too long, you can use the following method: brackets # add parens to make the long-line work: text = ("% d little pig come out or I'll % s and % s" % (3, 'huff', 'puff ', 'blowlow '))