A. Python Development basics

Source: Internet
Author: User
Tags bitwise function definition garbage collection in python

Python development environment

Slightly

Variable Nature

1. Variables in Python do not need to be defined, reused, used directly, and reused to store different types of values. 2. Variable naming follows C naming style.
3. Sensitive to case sensitivity.
4. Variable reference count.

5. Del statement can directly release resources, variable name deletion, reference count minus 1.
6. Variable memory automatic management collection, garbage collection.
7. Specify the encoding to add # Coding:utf8 or #coding=utf8 at the beginning of the file.

>>> a =
>>> print a
>>> ID (a)
136776784
>>> type (a)
<class ' int ' >
>>> b = 12.34
>>> print b
12.34
>>> ID (b)
3071447616
>>> type (b)
<class ' float ' >
>>> a = ' Itcast "
>>> print a
itcast
>>> ID (a)
3071127936
>>> type (a)
<class ' str ' >
>>> c = b
>>> print C
12.34
>>> ID (c)
3071447616
>>> type (c)
<class ' Float ' >
>>> B =
>>> ID (b)
136776784
>>> type (b)
<class ' int ' >
> >> Print B
12

#无需定义, direct use, Python interpreter based on right value determines the left type # variable A in-memory number

#a的类型为int类型

The type of the #变量b在内存中所占内存编号 #b is float

#变量a从新指向一个字符串 # Variable A in-memory number is to save the "itcast" place, the original A is pointing to the memory number of the content is not immediately released

#变量a现在指向一个字符串

#变量c保存的内存中的编号和b一致

#解释器在内存中发现有保存12的这个单元, the variable b points to this unit, reducing the repeated application and release of the storage space

>>> print a
itcast
>>> del (a)
>>> print a
traceback (most recent call last) :
  File "<stdin>", line 1, in <module>
nameerror:name ' a ' isn't defined


Simple functions

function definition Format

Defadd (x, y): z=x+y

Returnz

Res=add (3,5) printres
8

def defines the keyword of a function

X and y for formal parameters, no type decoration required

The function definition line needs to be followed by ': '

Integral indentation of function body

function can have return value, if no return value, return none, equivalent to NULL in C

Input and output functions

Input and raw_input () built-in functions
Raw_input () Getting data from standard input is the string type that needs to be converted using int ()

Output
Print function
>>> print '%s is%dsthello World '% ("Tody", 1) Tody is 1sthello world
>> redirection operator
LogFile = open ('/tmp/mylog.txt ', ' a ')
Print >>logfile, ' ERROR '
Logfile.close ()

Local variables and global variables

Code 1. Local variable scopes override global variables

Defp_num (): num=5

Printnum

Num=10p_num () printnum# results: 5 10

Code 2. There is a local variable definition in the function, the interpreter does not use global variables, the definition of local variables is later than referenced, the error

Defp_num ():p Rintnum

Num=5printnum

Num=10p_num () printnum# result error

Code 3. Direct access to global variables within functions

Defp_num ():p Rintnum


Num=10p_num ()
Printnum
#结果: 10 10

Code 4, modifying global variables within the function, using the Global keyword

Defp_num (): Globalnum

Printnumnum=20printnum

Num=10p_num () Printnum

Special variables

_xxx from module Import * Cannot import __xxx__ system-defined variables
Local variables for the __xxx class

An expression

An arithmetic expression

+a
A
A + ba Ba**ba * ba/ba//ba%b

Logical expression

          Not a
          A and b A or b A are not
          b

Relationship expressions

The result of the operation is a Boolean type

= = equals
!= is not equal to
<> not equal to (obsolete) > Greater than
< less than
>= is greater than or equal to
<= less than or equal

Bit operations

The result symbol is invariant to the result sign minus a plus B
A minus B

B Power of a
A Times BA divided by B, the real exception, floating point number, keep decimal a divided by B, rounding down
A pairs of B remainder

The logical Boola and B logic of A and Boola and B logic or Boola and B are the same objects Boola and B are not the same object bool

~a by Bitwise counter


A<<n a left n bit a>>n a right shift n bit a&b A and B bitwise with A|B A and B bitwise or a^b a

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.