Python Learning notes-basic syntax

Source: Internet
Author: User


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/8B/14/wKiom1hD7wCwA0Q-AAE4A3J2jJc744.png "title=" 1.png " alt= "Wkiom1hd7wcwa0q-aae4a3j2jjc744.png"/>


1. Constants and variables

constant : Once assigned, it can no longer be changed, in other words, it cannot be re-assigned to a value. python does not exist constant

literal constants : A single occurrence of a quantity that is not assigned to any variable or constant

variable : is a name, on the left side of the assignment symbol, that name can refer to the right side of the assignment symbol

In [PNS]: i = 3In [+]: print (i) 3


2. System type

    • Python is a strongly typed language--

    • Python is a dynamic type language-- variables can be re-assigned to other types


n [MAX]: 3 + 3out[39]: 6In [+]: 3 + ' 3 '--------------------------------------------------------------------------- TypeError Traceback (most recent) <ipython-input-40-1142db5d42c6> in <mo Dule> ()----> 1 3 + ' 3 ' typeerror:unsupported operand type (s) for +: ' int ' and ' str ' in [all]: in [+]: i = 3In []: t Ype (i) out[42]: Intin [+]: i = ' 3 ' in [St]: Type (i) out[44]: str


3. Operators

3, 1 arithmetic operators-- arithmetic operators can only operate on int and float

In [the]: 1 + 1out[45]: 2In [1]: + 1.2out[46]: 2.2In []: 3/2out[47]: 1.5In []: 3//2out[48]: 1

3, 2 comparison operator-- The return value of the comparison operation is of type bool

In [$]: 3 > 5out[49]: Falsein [3]: 3 < 5OUT[50]: Truein [Wuyi]:! = 5out[51]: True

3/3 logical operators-- The operands of the logical operators are either bool types or types that can be implicitly converted to type bool, and the return value is of type bool

In [all]: True and falseout[52]: Falsein [+]: TRUE or falseout[53]: Truein [si]: not falseout[54]: True


4. Program Structure

4, 1 sequential structure

4/2 Branch Structure

Single Branch

In [all]: If a > 3: ...: Print (' a > 3 ') ...: print (a) ...: a > 35

Dual-branch structure

In [page]: a = 5In [+]: If a <: ...: print (' A < ') ...: else: ...: print (' a > Ten ') ...: P Rint (a) ...: a < 105

Multi-branch structure

in [+]: a = 30In []: If a <: ...: print (' A < ') ...: Elif a <: ...: print (' <= a < ') ...: else: ...: print (' a >= ') ...: print (a) ...: a >= 2030

4/3 Cycle Structure

While loop

In [a]: i = 0In [+]: While I < 5: ...: print (i) ...: i + = 1 ...: 01234

There must be some mechanism to modify the modulation so that it exits the loop, usually in the loop body to modify the condition


For loop

in [+]: For I in Range (0,5): ...: print (i) ...: 01234

Never modify an iterative object in a for in loop


5.Break and Continue


Break: Early termination

in [+]: For I in Range (0,10): ...: print (i) ...: If I% 2! = 0: ...: print (' OK ') ...: Break ...: 01ok


Continue: used to skip the remainder of the loop body

In [total]: For I in Range (0,10): ...: = if I% 2! = 0: ...: print (i) ...: Continue ...: 13 579


Else-- The ELSE clause is executed when the loop does not exit prematurely

In [66]: for i in range (0,5):    ...:      pass    ...: else:    ...:      Print (' OK ')     ...:     okin [67]: for i in  range (0,5):     ...:     break    ...:  else:    ...:     print (' OK ')     ..:          ...:     in [68]: for  i in range (0,5):    ...:     continue     ...: else:    ...:     print (' OK ')      ...:         ...:          ...:&nbSp;    ok 



This article from "Thick tak" blog, declined reprint!

Python Learning notes-basic syntax

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.