0 Basic Learning Python programming not to be missed learning summary, small white welfare!

Source: Internet
Author: User

A software use

1 The first sentence of Python

In the C packing directory, create 1.tx text, open and enter the following content to save.

#! /usr/bin/env python#-*-Coding:utf8-*-print ("Life is short, I learn python!")

Open a DOS command window under the window system, such as execute:

Through the above can see we write a very cheap program to save a. txt end of the format, unexpectedly also executed, and did not follow the unified requirements of the. py format to design, that is not to say that the suffix is arbitrary? It can be arbitrary in theory!

Then why are we asking all the. py format to end? Because of the standard libraries we use, third-party libraries end with. py by default.

2 Python two modes of execution

For example, execute in two ways:

The first: Python interpreter python executes the path to the file

The second type: Enter the Python interpreter: Enter the Python statement in real time and get the execution result.

3 Python interpreter path

Under Windows system:

C:programdataanaconda3python is a Python installation path, but due to the mechanism of window, which has been added to the environment variable, we can execute it directly through Python 1.py.

Under Linux system: can be executed by/1.py, first set 1.py as executable file, and at the first line of the file The day sword #! /usr/bin/env python to specify the Python interpreter path.

4 encoding

Computer in different use environment has the same encoding format, English is commonly used as Ansic format, with 8 bits for a group, commonly used to have 128 kinds of characters. Chinese has Unicode (2 bytes), GBK (2 bytes) and other commonly used encoding, in order to be able to use in different environments, unified timing of the UTF-8 encoding, the composition of the variable, English is usually a byte, Chinese is usually accounted for 3 bytes.

5 variable naming rules

Named with a letter, number, underscore, and cannot begin with a number, the underscore is usually used to denote internal use. The name cannot be the same as the keyword, not the same name as the method, module, function, etc. inside Python!

Python naming conventions: Each letter represents a single function, a variable is represented by multiple strings, usually underlined separately for easy comprehension, e.g. USER_ID,USER_PASSWD, etc.

The essence of a variable: The variable refers to the content in memory, using the variable name to refer to the content represented in memory, in the most low-level conversion, memory block to store the actual content.

Name = "Ride Donkey chase Car"

Print (name)

Essentially on the bottom is print ("Ride Donkey Chase Car"), variable name name we use to refer to the string, create the variable must be assigned, do not assign the variable is not present, and also specify the type of the variable at the same time as the assigned value.

name = ' Xiaoming '

Name = 123

In the above two lines of code, the first line creates a variable name, assignment Xiaoming, in memory to open up an area to store the string ' xiaoming ', this area is used to denote name, the contents of which is the string ' xiaoming '

In the second line of code, the variable name is assigned a value of 123, this time the variable name becomes an integer number, and the Python interpreter re-opens an area in memory to store the integer 123, representing the variable 123, this time the opening string ' The Xiaoming ' memory area is freed by the Python interpreter.

6 Basic IO input and output

user_name = input ("Please enter user name")

USER_PASSWD = input ("Please enter password")

Print ("The user name you entered is%s, the password you entered is:%s"% (USER_NAME,USER_PASSWD))

Input is always waiting until the user enters a value, and the user input values are all stored in the context of the string. Ends with a carriage return character.

Print is used to format the output, print (*args, sep= ", end= ' n ', file=none), the input value is not fixed, the default is separated by a space, the end is automatically added ' n ' enter

7 conditional statements

(1) Basic format

If Judgment statement:

(tab) block of code

Else

(tab) block of code

If age >:    print ("You are already an adult") Else:    print ("minors, pay attention to protection!") ")

(2) Nested statements

If Judgment statement:

(TAB) code block

Elif Judgment statement:

(TAB) code block

......

Else

(TAB) code block

If value >= 1000000:    print ("You are Gaofu! ") elif value >=100000:    print (' You are middle class ') elif value >=1000:    print (' You can only eat and eat ') Else:    print (' poor cock silk ')

(3) Basic statement

If Judgment statement:

(tab) code block

if passwd = = ' 123456 '        print ("Welcome to System")

In a python statement, a colon: followed by a code block, the first line begins with a space-key indent, and usually the TAB key (4 spaces) is defaulted. The keyword pass indicates that nothing is done, meaningless, and is used only to represent blocks of code

If value < 0

Pass

8 Basic Data Forms

String str: Single quote ' Xiaoming '

Dual Bank "Xiaoming"

Three single quotes ' Xiaoming '

Three double quotes "" "Xiaoming" ""

Strings can be expressed in a variety of forms, such as above, can be single quotes, can be double bank, can also be 3 banks

String two basic operations:

string addition: + (connection)

name = ' Xiao ' + ' Ming '

Print (name)----> ' xiaoming '

String multiplication: * (repeat occurrences)

' # ' *10---> ' ########## '

Digital:

Integer Int:age = 10

Floating point float:value = 3.56

The most commonly used operations in Python are: add + subtract-multiply * divide/seek quotient//seek remainder% power * *

Listing list:

In brackets [], separated by commas, each element inside can be any value, including nested lists.

Tuple tuples:

with parentheses (), separated by commas, with at least one comma to indicate tuples, the biggest difference from the list is the inability to modify

Dictionary dict:

The curly braces denote {}, each element contains a key-value pair, the key is unique, and the dictionary is unordered.

9 Loop Statement while

While Judgment statement:

(tab) code block

The code shows the following columns:

i = 1while i <:    j = 1    while J <= I:        print ('%s *%s =%s   '% (j,i,j*i), end = ')        j + = 1    i + = 1    print (")

The while statement must have an exit condition in use, or it will be executed indefinitely. Typically used in conjunction with Break,continue, break terminates the loop and countinue ends the current loop.

The following break statement is used to jump out of the loop, combined with WHILE--ELSE statement to complete the input statement and judgment

"" "Enter the user ID correctly, then enter the secret key correctly to enter the system, otherwise enter Q directly exit" "" user_id = "user_passwd =" while user_id! = ' 123456 ':    user_id  = input ( "Please enter User id:")    if user_id = = ' Q ':        breakelse: While    user_passwd! = ' 666666 ':        user_passwd = input ("Please enter the secret key:")        if user_passwd = = ' Q ': Break    Else:        print ("Welcome to System") Print ("System task complete!") ")

Continue statement application, the use of continue jump out of the current loop, no longer execute the following statement, usually again do not make sense! The following statement is executed only if the preceding is correct.

While True:    print ("Who is?")    name = input ()    if name! = ' Xiaoming ':        continue    print ("Hello xiaoming!what is your password? It is first! ")    Password = input ()    if password = = ' 123456 ':        break;print ("Welcome to type System! ")

I have a public number, and I often share some of the stuff about Python technology. If you like my share, you can use the search "Python language learning" to follow

Welcome to join thousands of people to exchange questions and answers skirt: 699+749+852

0 Basic Learning Python programming not to be missed learning summary, small white welfare!

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.