Python Basic Grammar Learning

Source: Internet
Author: User

1. Variables

in Python, we don't need to specify a data type for a variable. For example a = 1, so that the variable ABC is the integer type; a = 1.0, then the variable ABC is the floating-point type; a= ' Hello ', then the variable ABC is a string.

Python defines variables when the strings are enclosed in quotation marks, single and double quotation marks no difference, with anything, if there is a single quotation mark inside the string, then the outside with double quotation marks, there are double quotation marks, the outside with single quotation marks, if there are both single and double, then use three quotation marks, Three quotation marks can also be multi-line comment code, single-line comment, use # (Code comment shortcut key ctrl+/)

Attention:

the variable name can only be any combination of letters, numbers, or underscores

the first character of a variable name cannot be a number

A keyword in Python cannot be declared as a variable, such as and,for, while These keywords are not declared as variables.

2. input, Output

Typically, Python 's code does not need to read input from the keyboard. But we can still use function input () in Python to do this, andinput () has an optional string parameter for printing on the screen. Returns the string entered by the user. the output uses print. The code is as follows:

name=input (' Please enter your name : ')

Print ('name')

Input at the time of receiving inputs, you can see the value you entered, if you enter the password, do not want to let others see your password, you need to use a standard library getpass, what is the standard library, it is not necessary to install, After loading the Python Library, is the standard library,getpass is a standard library, import in, directly use getpass.getpass The method is not displayed when you enter it .

Using the standard library getpass, you can not let input display What you entered when you accept input.

Import the standard library getpass code as follows:

Import Getpass

Use the following code:

Passwd=input . Getpass (' Please enter your password:')

3. Conditional Judgment

The conditional judgment in Python uses if else to judge, multi-branch words using if ... elif ... Else.

= = equals,>= is greater thanor equal,<= is less than or equals,!= is not equal to, > greater Than, < less than

Note that there must be a comparison operation of the same type, with different types of errors

For example , the int type is compared with the character type, the int type must be cast, or an error will be made.

a ge=int (Input (' Please enter your age '))

4. Loops

While Loop:
Count=int (Input (' Enter number of loops : ')) #count as Counter
While count<10:
Print (' A ')
Count=count+1
Else
Print (' B ')

Break: Exit loop , break can only be used in the loop, if you encounter a break in the loop , then immediately exit the Loop

Continue: exit this cycle

For Loop:

For I in range (10):
Print (' joke-telling,%s '%i)
If i==6:
Break

5. string Formatting

Name=input ('Please enter your name:')
Sex=input ('Please enter your gender:')
Print ('you're welcome .' +name) #The first kind uses+Connection
Print ('you're welcome .', name) #The second type uses,Connection
Print ('you're welcome .%s '%name) #The third use of placeholders
Print ('you're welcome .%s, your sex is%s '% (name,sex)) #multiple parameters, using multiple placeholders

Python Basic Grammar Learning

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.