1. Python Basics

Source: Internet
Author: User

Concise python tutorial: http://woodpecker.org.cn/abyteofpython_cn/chinese/

Enter Python:

  1. [[email protected] ~]# python 
  2. Python 2.6 5 ( r265 : 79063 jul 14 2010 11 : 36 : 05 )  
  3. [ Span class= "PLN" >GCC 4.4 4 20100630 ( red hat 4.4 4 - 10 on linux2
  4. Type"help","copyright","credits" or "license"for more information.
  5. >>> 
quit Python[CTRL + d]View version:[Email protected] ~]# Python-vpython 2.6.5executes the first Hello program
  1. [[email protected] ~]# cat hello.py 
  2. #!/usr/bin/python
  3. print‘hello!‘
  4. [[email protected] ~]# 
  5. [[email protected] ~]# python hello.py 
  6. 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:
  1. >>> name=‘Likun‘        --定义,并给变量赋值,字符类型要加引号
  2. >>> print name
  3. Likun
  4. >>> name1=name        --变量之间传递赋值
  5. >>> print name1
  6. Likun
  7. >>> print ‘%s is good‘%name1       --%s 为变量占位符
  8. Likun is good
  9. >>> name1=addr            --addr未加引号,会当做变量而不是字符,找不到变量因此报错
  10. Traceback(most recent call last):
  11.   File"<stdin>", line 1, in <module>
  12. 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
    1. [[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
  1. [[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

  1. [[email protected] python_scripts]# python 3rate.py 
  2. The rate between HK$ and US$ to RMB
  3. input how much RMB you want to change:10
  4. 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
    1. [[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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.