Python Learning (i)

Source: Internet
Author: User

A variable definition in Python

Similar to Java

1, composed of alphanumeric underline

2, the first letter can not be a number

3. No keywords

Second, constant

All consist of uppercase letters, do not change it, can actually change

Third, comments

Single-line Comment: #

Multiline Comment: Three single or three double quotation marks

Iv. Types of data

Bool:true,false

int:+ 、-、 *,/,%, * *

STR: Single or double quote wrapping, the two are actually no special difference, if used in the statement using single quotation marks, the outer layer uses single quotation marks, or vice versa, for example:

Print ("I ' m A student")

The + and * in the string

+ in the string for stitching

For example:

Print (" stitching "+" success ")

* Number of times to stitch the string

For example:

Print (" stitching "*8)
V. User interaction

Using input, the input type obtained by input is str, usage:

Input ("information prompted")

For example:

Input (" Enter information ")
Vi.. Judgment statement If

1, single-layer judgment if

If condition:

Results

if True    print("True")

2. Double-layered judgment if-else

if condition;

Results

Else

Results

if False:     Print ("True") Else :     Print ("False")

3, Multilayer judgment If-elif-else,ps: The last else can be added without

If condition:

Results

Elif Conditions:

Results

Else

Results

For example:

 age = Int (input ( "  Enter number  "   if  age>5< Span style= "COLOR: #000000" >:  print  ( a>5   " )  elif  age==5 :  print  ( " a=5  "  )  elif  age<5:  print  ("  a<5  " ) 

4, if can be multi-layered nesting

For example:

if True:     if False:         Print (" this is nested ")
Seven, loop statement 1, while Loop statement

While condition:

Results

For example

 while True:     Print (" Perpetual loop ")

While condition:

Results

Else

Results

When a complete execution of the condition in the while is performed, the Else statement is executed, incomplete execution is complete, or jumps out, and the Else statement is not executed

For example:

Count = 0
While count<=3:
i = Int (input ("Enter Number"))
If i==5:
Print ("Jump out")
Break
Print (count)
Count + = 1
Else
Print ("Complete Execution")

2. For Loop statement

A, for custom parameter in "string":

A custom parameter takes one character at a time

For example:

 for inch " Andraw " :     Print ("letterfor:", letter)

Results:

b, for custom parameter in string set

Custom parameters take one string at a time

For example:

  

msg = ['apple','banana','mango' ] for in msg:    print("fruit:  ", Fruit)

Results:

c, for custom parameter in range (numeric n)

Custom parameters are taken starting from 0 until n-1

For example:

 for  in range (5):    print(i)

Results:

D, for-else statements, similar to While-else

3. Break and Cotinue use

Break: Jump out of the current loop

Continue: Give up this cycle and jump to the loop to judge the position

4. Use of Pass

Pass: Nothing is performed and is generally used for placeholder

Python Learning (i)

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.