My life is short. I use Python first and python first

Source: Internet
Author: User

My life is short. I use Python first and python first

 

 

I. variable name

Custom variable names can only be usedNumbers, letters, and underscores.

Assign a value to a variable before using it.

Note: 1. The variable name cannot start with a number;

2. it cannot be a keyword. For example, 'and', 'as', 'assert ', 'Break', 'class', 'contine', 'def', 'del ', 'elif', 'else', 'cancel', 'exec ',

'Finally ', 'for', 'from', 'global', 'if', 'import', 'in', 'is, 'lambda, 'not ', 'or', 'pass', 'print ', 'raise ',

'Return ', 'try', 'while', 'with', 'yield'

3. It is best not to repeat the built-in things in Python. In addition, when writing code, it is best to connect the two words with underscores, such as use_name;

Comments: single line comments # ...... content ..................

Multi-line comment "... content .............."""

 

Ii. Condition Statement 1. Basic if statements

    If condition:
Internal code block #The internal Block Code cannot be aligned with the if top. Generally, four spaces are left, that is, a Tab key.
Internal code block
Else:
...

Print ('....')
For example:
IF 1 = 1:
Print ("Welcome to Python ")
Print ("Welcome to blog ")
     
Else:
Print ("Welcome to Southwest Petroleum University ")

2. if supports nesting
If 1 = 1:
If 2 = 2:
Print ("Welcome to Python")
Print ("Welcome to blog")
Else:
Print ("Welcome to Southwest Petroleum University")
Else:
Print ("Welcome")

3. if elif

Indium = input ('Enter the score :')

If indium = "90 ":
Print ('excellent ')
Elif indium = "80 ":
Print ('good ')
Elif indium = "70 ":
Print (moderate ')
Else:
Print ('pass ')

Print ('... end .......')

Pass: if the condition is followed by nothing, you can use pass. Pass indicates the Code with no meaning and is only used to represent the code block.

For example
If 1 = 1:
Pass
Else:
Print ('SB ')

III. Basic Data Type 1. String

The string we have recognized so far is everything in quotation marks, and we also call it text. Text and numbers are completely different.

For example, add two numbers and add two characters.

Adding quotation marks to a number becomes a String concatenation.

When creating a string, quotation marks must be placed on both sides of the character, which can be single quotation marks, double quotation marks, or triple quotation marks, but cannot be mixed.

For example, name = "I am Jiao Shuai Feng"
Name = 'I am Jiao Shuai Feng'
Name = "I am Jiao Shuai Feng """
Name = ''' I'm Jiao Shuai Feng '''

For example, if the character string Let's go is entered, an error is returned if it is enclosed in single quotes.

There are two solutions at this time: one is to use double quotation marks

Second, use the escape symbol (\) to escape the quotation marks in the string.

 

Note: The string cannot end with "\". In the string, "\" indicates that the string has not ended. The meaning of continued line feed

2. Arithmetic Operators

+-*/% **//

The first four are addition, subtraction, multiplication, and division.

% Indicates the remainder.

// Indicates the operator

** Indicates the power.

 

3. while Loop

While condition:

The operation that is executed if the condition is true.

Endless loop

While 1 = 1:

Perform operations

Exercise questions

1. Use a while loop to input 1 2 3 4 5 6 8 9 10

a=0while a<10:    a+=1    if a==7 :        pass    else:        print(a)

2. Calculate the sum of all numbers from 1

a=0s=0while a<100:    a+=1    s=s+aprint(s)

3. Output all odd numbers in 1-100

a=0while a<100:    a=a+1    if a%2==0:        pass    else:        print(a)

 

4. Output all the even numbers in 1-100

a=0while a<100:    a=a+1    if a%2==1:        pass    else:        print(a)

 

5. Calculate the sum of all numbers of 1-2 + 3-4 + 5... 99.

N = 1 s = 0 # s is the sum of all previous numbers while n <100: temp = n % 2 if temp = 0: s = s-n else: s = s + n = n + 1 print (s)

 

6. User Logon (three retries)

 

I = 0 while I <3: a = input ("Enter the User name:") if a = "1234": print ("Login successful") else: print ("Logon Failed") I = I + 1 print ("Please try again later ")

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

--- Restore content end ---

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.