Official website: https://www.python.org/free installation version:portable pythonhttp://portablepython.com/wiki/portablepython2.7.3.1/ python details Benefits 1, provides a large number of inside, for you to use; 2, Linux original python& nbsp Type Pypy cpython jpython rubypython python Environment installation windows: Download installation package https://www.python.org/ & nbsp Default path:c:\python27 Update: View version p ython -v update python windows: unload load linux: & nbsp Linux Yum relies on its own python, in order to prevent errors, the update here is to install a python 1, install GCC, for compiling python source code yum install gcc 2, download source package, https://www.python.org/ftp/ python/ 3, unzip and enter the source file &NBSp 4, compile and install ./configure MAKE&NB sp;all make install 5, view version &N Bsp /usr/local/bin/python2.7 -v 6, modify the default Python version & nbsp mv /usr/bin/python /usr/bin/python2.6 ln -s /usr/ local/bin/python2.7 /usr/bin/python 7, to prevent Yum from performing exceptions, modify the Python version used by Yum vi /usr/bin/yum to change head #!/usr/bin/python to & nbsp;#!/usr/bin/python2.6
Getting started with PythonThe first program Linux writes Python code to add the following words #!/usr/bin/env Python interpreter declaration (tells the system that this script executes in Python) #如果写中文要用utf-8 yards; The following two lines are all used Utf-8 code; Uniform requires the use of the first; #-*-Coding:utf-8-*-# coding:utf-8 All Python scripts use the following sentence#!/usr/bin/env python
#-*-Coding:utf-8-*-python3.0 after the default encoding is utf-8 three encoding rules ASCII Unicode UTF-8
NotesSingle-line comment; Gag; if # coding:utf-8 multiple lines of comment; Surround with "" "and" "(for illustrative purposes)
Capture ParametersIMPOTR Sys #impotr导入模块 (the name of the foot can be followed, such as: m.py so write Impotr m) printSYS.ARGV#使用argv模块; captures all the parameters behind the Python interpreter, separated by spaces and saved to the collection, such as Vim hello.py writes print sys.argv in this file#!/usr/bin/env python#-*-Coding:utf-8-*-Import SYSPrint SYS.ARGVExecute the script outside; capture all parameters behind the Python interpreter, separated by spaces; [[email protected] python]# python hello.py [' hello.py '][[email protected] python ]# python hello.pyZhangyu[' hello.py ', 'Zhangyu‘]
byte codesuch as: [[email protected] python]# vim m.py#!/usr/bin/env python#-*-Coding:utf-8-*-print "Wonil, hello"Import m.py files in hello.py [[email protected] python]# vim hello.py#!/usr/bin/env python#-*-Coding:utf-8-*-#impotr导入模块 (the name that can follow footsteps, such as: m.py so write Impotr m)Import Mprint "Yaoshen"Execute Python hello.py to see the result; m.py is the first reference to execute hello.py content [[email protected] python]# python hello.pyHello, Wonil.YaoshenUse the view command; Find more M.PYC files;when the module you call (m) finishes executing; PYc bytecode files are automatically produced[[email protected] python]# ll total dosage 12-rwxrwxrwx. 1 root root 368 October 22:27 hello.py-rwxrwxrwx. 1 root root 69 October 22:22 m.py-rw-r--r--. 1 root root 124 October 22:27 M.pycM.pyc End With PYc is a byte-code file, when the content is the same; the. pyc file has a high priority; When the contents of the. py file change; the. PYc bytecode file is newly generated.
variable-declaration1, digital letters and underscores; 2, the first character of the variable name cannot be a number, 3, when declaring a variable, can not use the system built-in keywords;
Variable-Assignmentname1 = "Sishen" name2 = name1 name1 = "Sile"results: name1 is equal to sile;name2 equals Sishen;in the case of a string, when the name1 changes, the name2 does not change .string attribute; once modified; re-created (reallocate memory space);The number type is 5 to 257 in a memory space
Input/Outputraw_input ("Please enter user name:")Python built-in functions, receiving input from the user, or interacting with the user by reading the console, as in the following examplename = raw_input ("Please enter user name:")Import GetpassGetpass built-in modules make raw_input input invisiblepwd = getpass.getpass ("Please enter password:")
Process Control#if else if otherwisename = raw_input ("Please enter user name:")if name = = "Zhangyu":Two equals is the value and value comparison; is memory address comparison;print "Login Successful"Else: print "Login Failed"--------------------------------------Eric General Tony Super Alex Super God # if it's Eric; output normal # if it's Tony; output Super # if it's Alex; The code below is a layer down judgment. On the basis of if 1; output; otherwise 2; output; otherwise if 3; output; otherwise; output#!/usr/bin/env python#-*-Coding:utf-8-*-name = raw_input ("Please enter user name:")if Name = = "Eric":print "Normal"elif name = = "Tony":print "Super"elif name = = "Alex":print "Super God"Else:print "illegal"-----------------------------------------------------------practice requires Eric normal 123tony Super 123alex Super God 123name = raw_input ("Please enter user name:")pwd = getpass.getpass ("Please enter password:")
if Name = = "Eric" and pwd = = "123":print "Login successful; normal"elif name = = "Tony" and pwd = = "123":print "Login successful; Super"elif name = = "Alex" and pwd = = "123":print "Login successful, super God"Else:print "Login Failed"There's another way.name = raw_input ("Please enter user name:")pwd = getpass.getpass ("Please enter password:")if pwd = = "123":if Name = = "Eric":print "Login successful; normal"elif name = = "Tony":print "Login successful; Super"elif name = = "Alex":print "Login successful, super God"Else:print "Login Failed"Else:print "Login Failed" ################################################## #数据类型两部分1, Single value 1) digital int 3714 Long integer Large integer floating-point 3.14 with decimal point & nbsp Complex 2) string string formatting &NBS P 1] "string %s %d"% ("s represents the string", d represents the number) &N Bsp name = "i am %s" % "Alex" & nbsp name = "i am %s ,age %d" % ("Alex") %s The string is a placeholder; that's the alex &NB Sp The%d number is a placeholder; it represents the 73 behind it. name = "i am %s ,age %d" & nbsp name % ("Alex", 123) &N Bsp such as: >>> name = "I am%s"% "Alex">>> name' I am Alex '2]name = "I am {0},age {1}"Name.format ("Alex", 999)new_name = Name.format ("Alex", 999) Print new_name ' ' single-line string " " single-line string "" " " "" Multiline String | can also be used when the string print "" "ABCDEFG" "" Index such as:name = "Alex" #name [0]= "a" The first character is represented by 0; the first character is aprint name[0] Prints the first character print name[0:2] two characters before printing (prints characters less than 2, such as: AL) print name[0:] Print to end print name[-1] Print last character Print len (name) How many characters to get The subscript is starting from 0 Print name[-1] == name[len (name) -1] print name.strip () Remove the space at both ends Print name.lstrip () Remove left space Print name.rstrip () Remove right space split Name.split (",") 3) Boolean False (True,false) 1 true ; 0 is False---------------------------------------------------------2, set 1) list string array modifiable Create list name_list = ["Alex", "Seven", "Eric"] or Name _list = list (["Alex", "Seven", "Eric"]) Append:append Name_list.append ("abc") Append to Name_list list abc Delete:del del name_list[0] Delete first character Length:len len (name_list) "_". Join (Name_list) new compilation delimiter view in with not:in "Alex" in (name_list) split Name.split (",") , Division name = "A b c"      2) tuples meta-fathers not modifiable ("Alex", "seven", "Eric") 3 dictionary dictionary:dict: Dictionary unordered storage & nbsp Special for loop with {} person ={ &NBS P "Name": "Alex", "age": 19, "gender": "not known", }person ["name"] for k,v in petrson.items (): every element of a dictionary print k print v print "===========" Person.keys () so key; list person.values () so valu; list Person.items () all elements; only for loop use Assign an element to a k,v 4) hash table important; all slices; index ( -1); Len () contains, loop str string re-open space List table modified memory address invariant tuple tuple does not allow modification Cyclename_list = ["Alex", "Seven", "Eric"]For ele in name_list:Print Ele#1, Alex.#2, seven#3, Eric.name_list = ["Alex", "Seven", "Eric"]
For ele in name_list:if ele = = "Alex":print "KKK"ContinueStart the Next loop againif ele = = "Eric":print "KKK" BreakDo not want the For loop to continue; Use this while loop while: if the condition is true; loop forever; print "11111" while true: Dead loop print "true" while False: Print "false"continue "to end this cycle immediately;Break "immediately jumps out of the loop structure"Pass "empty operation; to save unnecessary code"-----------------------------------------------------------file, read 1, locate the file e:/log2, open the file Openfile_obj = files ("Path to File", " Mode ") Mode: R Read W write a append content r+ read/write w+ write read 3, file operation, read and write File_obj.read () read all in memory File_obj.readlines () [line, row] read into for the File_obj.xreadlines (): A row of read print linefor line in file_obj: Each loop, read one line, avoid all read into memory File_obj.write () write to File _obj.writeline () write in line mode 4, file close File_obj.close ()
Python first day