Python Basics (i) Python data types, conditions, loops

Source: Internet
Author: User

This article is intended to discuss some of the basic syntax in Python through several examples, including basic data types, Python syntax rules, defining variables, loops, conditional judgments, and so on. And make a simple comparison between these features and C (this code is based on Python3)

Example 1: Find out the and of 1-100.

First give the implementation code

i=0sum=0 while i<=100:    sum+ =I    i+=1print(sum)

This code is parsed on a line-by-row basis:

The first line defines a variable i and assigns I to the initial value 0, which requires attention to two points, the first is that we do not declare the type of the variable i, in C, we want to define a variable, we must declare the variable type. In combination with the code below, we can see that I is here equivalent to the int type in the C language. 2nd, after the code in the I=0 line, there is no common line delimiter in the C language ";" in the C language, a line ends and a ";" must be followed to indicate the end of a line, but not in Python. So the first line is described in C, and it needs to be written as: int i=0; This is the first big difference between Python and C.

The second line of code is the same as the first line, so it is moved to the third row. The third line to line fifth is a while code block. The code block means that these lines are equivalent to a single statement. In C, to represent a while code block, you need to use the while (expression) {...} Format, while in Python, it is the while expression: ... To express. In Python, there is no {} symbol, substituting: and indentation. After while (FOR,IF): The number is not missing, ":" means that the following is a block of code.

Line fifth is the contents of the while loop, which is a whole with the third row, in Python, indented to indicate whether a few lines of code belong to a block of code. It is important to note that indentation is strictly required in python, and that if a few lines are part of the same block of code, the rows must be indented the same spaces, such as lines fourth and fifth, because they all belong to the while code block, so they must have the same number of indentation spaces, and if the fourth line indents 4 spaces, A five-line indentation of 3 spaces will cause an error. Therefore, it is important to note the indentation of each line in Python. To avoid this problem, you typically use the TAB key to control indentation. In the C language, there is no requirement for indentation because there is a {} Flag code block, and more to make the code look more beautiful.

One more thing to note in lines fourth and fifth: Sum+=i is equivalent to Sum=sum+i, and this is consistent with the C language, but the fifth line of I+=1 is not as i++ as the C language. This is because in Python, the auto-decrement operation is not supported.

The last line is the output statement print statement for Python. With this statement, you can output the content to the device. It acts like a printf statement in C, but is much simpler than a printf statement, as long as you enclose the statement you want to output in parentheses.

The first example summarizes the following points:

  Variables defined in 1.python are not required to declare variable types, and in Python there are no data types such as Int,double,char, and Python automatically determines the variable type based on the value of the variable and the operation.

The end of a line of code in 2.python does not require a ";" separation, whereas in c it is required.

The loop body of the 3.whil loop (for,if, function) does not have {},python is the ":" Identity while loop (for,if, function)

4.python strict indentation, because Python does not have {} to control the code logic, so with strict indentation control code logic, it is recommended to use tab indentation control.

There is no self-increment operator (+ +) and a decrement operator (--) in 5.pyhon.

Example 2: Using while loop output 1 2 3 4 5 6 8 9 10

First, attach the code:

1I=12  whileI<=3:3     Print("Please enter user name:")4Inuser=input (">>>")5     Print("Please enter your password:")6Inpasswd=input ()7     ifinuser=="Hahh"  andinpasswd=='123aaa':8         Print("Login Successful")9          Break;Ten     elifinuser=="Hahh"  andINPASSWD! ='123aaa': One         Print("bad password, please try again") A     Else: -         Print("user name or password is wrong, please try again") -I=i+1

This code is parsed on a line-by-row basis:

1-3 lines skip, starting from line fourth. The input (">>>") function appears in line four, which indicates that the user entered data from the keyboard. Where the parameter in input is a prompt, the parameter can be empty. Therefore, the input parameter of line 6th is empty. Line 7th is an if statement, where the code style of the IF statement is similar to while, and is marked with ":". Be aware that there is no && operator in Python and | | operator, replaced by the and and OR keywords. In addition to the code style, the use and syntax rules of the IF statement are consistent with the if usage in the C language

The 9th line break indicates the exit loop, the keyword and the Continue keyword are common keywords for the control loop, its role and the C language break,continue keyword has been, break represents the terminating loop, continue means skipping the loop.

Summarize:

no && in 1.python | | Operators, substituting and and or

2.if usage rules consistent with C language

3.break and Continue Keywords

Python Basics (i) Python data types, conditions, loops

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.