Concise python tutorial: http://woodpecker.org.cn/abyteofpython_cn/chinese/
Enter Python:
[[email protected] ~]# python
Python 2.6 5 ( r265 : 79063 jul 14 2010 11 : 36 : 05 )
[ Span class= "PLN" >GCC 4.4 4 20100630 ( red hat 4.4 4 - 10 on linux2
Type"help","copyright","credits" or "license"for more information.
>>>
quit Python[CTRL + d]View version:[Email protected] ~]# Python-vpython 2.6.5executes the first Hello program
[[email protected] ~]# cat hello.py
#!/usr/bin/python
print‘hello!‘
[[email protected] ~]#
[[email protected] ~]# python hello.py
hello!
Print plus color:>>> print ' \033[31;1mhello world!\033[0m '
Print repeat output characters:>>> print (' a ') aaaaaRead user input:>>> A = raw_input (' Pleas input: ') pleas input:hello!>>> print ahello!guaranteed Input as number:>>> A = Int (raw_input (' pleas input number: ')) pleas input number:sdftraceback (most recent call last): File "< Stdin> ", line 1, in <module>valueerror:invalid literal for int. () with base: ' SDF 'convert numbers to characters:>>> Str (12) ' 12 'Simple calculation:>>> 2 * 36>>> 32/48>>> b=2+3>>> print B5variables:
>>> name=‘Likun‘ --定义,并给变量赋值,字符类型要加引号
>>> print name
Likun
>>> name1=name --变量之间传递赋值
>>> print name1
Likun
>>> print ‘%s is good‘%name1 --%s 为变量占位符
Likun is good
>>> name1=addr --addr未加引号,会当做变量而不是字符,找不到变量因此报错
Traceback(most recent call last):
File"<stdin>", line 1, in <module>
NameError: name ‘addr‘ is not defined
the difference between single-cited, double-cited, and three -lead variablesA string with a single citation in a normal string using a double-cited three-lead to draw multiple lines of characters
[[email protected] python_scripts] # #!/usr/bin/pythonprint'hello! ' Print " What ' s your name? " n="'good morningeveryone,come on! " " Print N
"" can be used to annotate multiple lines of codeSmall exercise: Exchange RMB
[[email protected] python_scripts]#Cat 3rate.py#!/usr/bin/pythonPrint 'The rate between HK $ and US $ to rmb\n'RMB=int (Raw_input ('input how much $ want to change:')) --int to do floating-point arithmetic HK=rmb/0.84US=rmb/6.4Print 'hk=', HK,', us=', US--stitching with commas
[[email protected] python_scripts]# python 3rate.py
The rate between HK$ and US$ to RMB
input how much RMB you want to change:10
HK=11.9047619048,US=1.5625
Indentation Requirements:Logically the same level, to align, misalignment will be error different levels must have indentation, otherwise it will also error indentation can choose tab, single space, double space, the full text must ensure that the indentation is consistent
[[email protected] python_scripts] # #!/usr/bin/pythonprint'hello! ' Print ' World ' for in range (1,5): Print i
python Command Auto-complete script:Vim ~/.pythonstartup
ImportSYSImportReadLineImportRlcompleterImportatexitImportOsreadline.parse_and_bind ('Tab:complete') Histfile= Os.path.join (os.environ['HOME'],'. Pythonhistory')Try: Readline.read_history_file (histfile)exceptIOError:PassAtexit.register (Readline.write_history_file, Histfile)delOS, Histfile, ReadLine, Rlcompleter
echo "Export Pythonstartup=~/.pythonstartup" >> ~/.BASHRC
1. Python Basics