Python's path One, Python basic syntax

Source: Internet
Author: User

Python basic syntax: 1.python BASIC statement structure:

First, in other languages, such as java,c++,c#, after writing a line, you need to add a semicolon at the end of the statement to indicate the end of the statement, but in Python we don't need to add semicolons at the end of each line, and Python defaults to one statement for each behavior. Of course, we add not to error, this is a bit like JS. For example, the following code will execute smoothly

1 Print("Hello World")2 Print("Hello World")3 Print("Hello World")4 Print("Hello World")5 Print("Hello World")

2. Declaration of variables:

In Python, when we want to declare a variable, we don't need to add the variable type before the variable, and Python automatically matches the type of data we want to declare, such as:

str1 = 1.523print= 1print"helloWorld"Print (Type (STR3))

Note: The type method is to print out the data type of the current object.

The three printed data types are the Float,int and STR types, respectively.

<class'float'><class'int'> <class'str'>

Let's say the formatted output of the string, usually our output string can only be printed directly, but in Python there is a function to format the output, as follows:

" " name:liqtpassword:111age:90sex: Male ""
Print (str)

If you use three single or double quotation marks to include it, you can follow our idea of direct printing output, the output is as follows

Name:liqtpassword:111 Age:allsex: Male

Finally about the variables let's talk about how it's assigned. There are three ways to assign variables, and we explain in turn

1. Assign values by percent plus data type, as follows

Name="liqt"age =18Sex=" male "str ="" "--------info--------name:%sage:%dsex:%s"""%(name,age,sex)  print(str)

Where%s represents a string,%d represents an integer, and the output is as follows

--------Info--------name:liqtage:Sex: Male

2. Assign a value by placeholder, as follows

Name="liqt"age =18Sex=" male "str ="" "--------info--------name:{0}age:{1}sex:{2}" ". Format (name,age,sex)  print(str)

Print results same as above

3. Assign a value to the parameter name in the placeholder, such as

Name="liqt"age =18Sex=" male "str= """ --------info--------name:{_name}age:{_age}sex:{_sex} "" ". Format (_name=name,_age=age,_sex=sex)print(str)

Printing results are the same, but I personally recommend a third, because it will look very clear, and do not need to follow the order of the variable assignment.

3.if-else statements

First example

1A =12 ifA==1:3     Print("true")4 elifa==2:5     Print("false")6 Else:7     Print(" Other")

The output of the above results is undoubtedly true, but there are a few things that need to be explained

First, the conditional statement after the if is not enclosed in parentheses, and after the conditional statement is written, it is necessary to use a colon (:) in the latter half to represent the end of the conditional statement, and a statement that needs to execute the condition, that is, the print output

Second: Note that the third line of code and the beginning of the sentence is a distance (a tab), this point to pay particular attention, because in Python, in order to distinguish whether a statement is a condition after the execution of the statement, from the structure to distinguish the hierarchy, as we use the compiler tool, Each level folder and the upper folder have some back move is a reason

Third: In the IF statement, if you want to indicate that there are other possible conditions, not using else if, but elif, which is different from many languages

4.for Cycle
1  for  in range:2     print(i)3else: 4     Print (11)

The basic statement structure is simple, as shown above. But there are a few more notes.

First: The loop condition behind for is not required to add a small curly brace

Second: The condition is followed by a colon (:), which is identical to the IF statement

Third: After the For loop execution, you can also write an else, which indicates that code that is not part of the for loop or that must be executed after the loop executes, that is, else is sure to be executed after the for execution, and else will not execute unless the FOR loop is used to jump out of the loop. , increase the break as follows

1 For I in Range (Ten): 2      Print (i)3     if i==5:4         break5  Else:  6     print (one)

IV: Rang (10) can be changed to any array or list

5.while Cycle
1 i=02 while i<10:3     print(i)4      i=i+15Else:6     print(11)

Principle with a For loop, do not want to do else still need to use break to jump out or not write else statement.

Python's path One, Python 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.