Python basics-01 and python-01

Source: Internet
Author: User

Python basics-01 and python-01
Knowledge points:

1. Initial correlation

2. Variables

3. User Interaction, data type, placeholder

4. if judgment

5. while Loop

6. for Loop

 

1. Initial correlation:

1.1 declare Interpreter

#! /Usr/bin/env python

Declare the interpreter and call the interpreter in linux and mac.

 

1.2 indent

In python, note the use of indentation. indentation errors occur when IndentationError is reported.

 

1.3 annotations and escaping

Note:

Single line comment can be used :#

Multi-line comment: ''' commented content ''' (three single or three double quotation marks are used)

Escape:

The following code can be used to escape a single character :\

Escape by line: '''... ''' (three single quotes can also be used for escape)

 

2. Variables

2.1 variable assignment

Assignment format of variables in python:

A = 10

A is the variable name, and 10 is the value assigned to variable.

 

2.2 variable naming rules

1. Variable names can only be any combination of letters, numbers, and underscores

2. the first character of the variable name cannot be a number.

3. python variable names are case sensitive

4. You cannot use the python keyword as the variable name to obtain the keyword of the current python version. Method:

    import keyword
    print(keyword.kwlist)

2.3 point of the Variable

A = 10

B =

A = 11

Print (a, B)

The execution result shows a = 11, B = 10.

  

A points to 10 in the memory. When a = B, B is not directed to a, but B is directed to the memory location that a points.

So when a's direction changes, B's direction does not change because a's direction changes.

 

3. User Interaction, data type, placeholder

User interaction: input

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

Job = input ('Please input your job ')

Age = input ('Please input your job ')

Info = '''

Name: {_ username}

Job: {_ job}

Age: {_ age}

'''. Format (_ username = name,

_ Job = job,

_ Age = age)

Print (info)

Use. format to call a variable. Replace the variable value entered by the user into the following info.

 

Data Type:

String: any text enclosed by ''or ""

Integer: python can process integers and negative numbers of any size.

Floating Point Number: decimal number. The floating point number operation is sometimes rounded down.

Division in python:

/: Normal division. The result is a decimal number.

// The calculated floor division is always an integer (not rounded down, only the integer part is retained)

Boolean value:

A boolean value has only two results: true or false.

Boolean operation: and or not

Variable:

Constant: the amount that cannot be changed. It is usually expressed by a variable name in python.

  

Placeholder:

Common placeholders:

% D: integer

% F: Floating Point Number

% X: hexadecimal integer

% S: String

Note: In python, % is used to indicate the percent sign.

 

4. if statement:

Username = input ('name :')

Password = input ('password :')

 

If username = 'zq' and password = '000000 ':

Print ('wecome, user {_ name} login'. format (_ name = username ))

Else:

Print ('input error ')

Note that if must be followed by: (colon)

 

5. while loop:

Requirement: the user can guess the age three times.

Age_of_zq = 25

Count = 0

While count <3:

Guess_age = input ('Please input guess age :')

If guess_age = age_of_zq:

Print ('It \'s right ')

Break

Elif guess_age> age_of_zq:

Print ('too old ')

Else age_of_zq <25:

Print ('too small ')

Count + = 1

Else:

Print ('your chance is over ')

 

Break & continue:

Break: ends the current loop

Continue: Skip this loop, end this loop in advance, and enter the next loop directly.

 

6. for Loop

Requirement: Print and output the 10 numbers 0-9 each with 3 numbers.

For I in range (0, 10, 3 ):

Print (I)

 

Use the for loop to implement section 5th

Age_of_zq = 25

For I in range (3 ):

Guess_age = int (input ('Please input guess age :'))

If guess_age = age_of_zq:

Print ('OK ')

Break

Elif guess_age> age_of_zq:

Print ('bigger ')

Else:

Print ('small ')

Else:

Print ('your chance I over ')

 

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.