Python basic notes and python notes

Source: Internet
Author: User

Python basic notes and python notes

1. Environment variable settings:

 

Edit the system variable Path and add two new paths.

C: \ Python26 can be used to call python.exe.

C: \ python26 \ Scripts calls third-party Scripts added to Python through extension.

 

2. If you use Chinese characters, the character set must be specified in the first line of the py file:

 

#-*-Coding: UTF-8 -*-

Or

# Encoding: UTF-8

 

3. Variable Length Parameter

 

 

4. Use global variables

 

You can use the global keyword before a variable name.

A = 5

Def fun (x ):

Global

Return x +

 

5. lambda expressions

 

Anonymous function, single-row small function

 

Format: labmda parameter: Expression

Returns the value of the expression.

 

It can contain a single parameter expression and cannot contain statements, but can call other functions.

Fun = lambda x: x * x-x

Fun (3)

 

Def show ():

Print 'lambda'

F = lambda: show ()

F ()

 

Def shown (n ):

Print 'lambda '* n

Fn = lambda x: shown (x)

Fn (2)

 

6. run scripts

 

Python code
  1. # Encoding = UTF-8
  2. Def show ():
  3. Print u' I am a module .'
  4. If _ name _ = '_ main __':
  5. Show ()

 

7. Random Number

 

Import random

Random. random () # generate a random floating point number [)

Random. randint (a, B) # generate an integer between [a, B] (including a and B)

 

 

8. input parameters in the command line

 

 

Def prompt (prompt ):

Return raw_input (prompt). strip ()

Name = prompt ("Your Name :")

 

9. Python characters

In python, the character is a string of 1.

Obtain all characters of a string:

 

Python code
  1. >>> String = 'abcxyz'
  2. >>> Char_list = list (string)
  3. >>> Print char_list
  4. ['A', 'B', 'C', 'x', 'y', 'z']
  5. >>> Char_list = [c for c in string]
  6. >>> Print char_list
  7. ['A', 'B', 'C', 'x', 'y', 'z']
  8. >>># Obtain a set of all characters of a string
  9. >>> Import sets
  10. >>> Char_set = sets. Set ('abbcc ')
  11. >>> Print char_set
  12. Set (['A', 'C', 'B'])
  13. >>> Print ','. join (char_set)
  14. A, c, B
  15. >>> Type (char_set)
  16. <Class 'sets. set'>
  17. >>> For c in char_set:
  18. Print c
  19. A
  20. C
  21. B

 

10. Conversion of characters and character values

Converts a character to an ascii code. The built-in function ord ():

 

>>> Ord ('A ')

97

 

Convert ascii code to character. built-in function chr ():

 

>>> Chr (97)

'A'

 

Convert unicode characters to unicode codes. The built-in function ord ():

 

>>> Ord (U' \ u2020 ')

8224

 

Converts a unicode code to a unicode character. The built-in function unichr ():

 

>>> Unichr (8224)

U' \ u2020'

 

11. test whether the object is a class string

Isinstance (anobj, basestring)

 

12. sys. argv

List of command line parameters passed to the Python script. Argv [0] is the script name.

 

Python code
  1. # _ * _ Coding: UTF-8 _*_
  2. Import sys
  3. If _ name _ = "_ main __":
  4. If len (sys. argv) <2:
  5. Print "Need a argument! "
  6. Sys. exit (1)
  7. Arg = sys. argv [1]
  8. Print 'you input a argument: ', arg

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.