Python Learning bit record-day01

Source: Internet
Author: User

Reference link https://triaquae.gitbooks.io/python/content/chapter1.html

First, the installation of Python

1, the Windows platform to the Python official website to download the Windows version of the installation media, follow the prompts to install the installation is ready to use.

2. The Linux platform can be installed by downloading the tar package from Yum or the Python website.

For ease of use, either Windows or Linux, it is recommended to add to the environment variable.

Windows: Computer Properties--Advanced system settings--system--Add Python's installation directory to the environment variable--path

Add the installation location of export Path=path:python at the end of Linux:/etc/profile

Ii. How Python is executed

1. Python interpreter runs Python code in the form of interaction

Print (' Hello World ')

2. Save Python code to a file and name it end of. Py, execute with python command + file name

Python helloworld.py

Third, variable

As the name implies, the amount of change, the format: variable name = value, the value of the right side of the variable name will change, by using the same variable name without regard to the change in value, but can be implemented in the form of different variable values to simplify the logic.

Declaring variables: name= ' Tianle '

To define a rule:

1. Variable names can only be any combination of letters, numbers, or underscores
2. The first character of a variable name cannot be a number
3. The following keywords cannot be declared as variable names

[' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']

How to define:

1, Hump body: username= ' Tianle '

2, Underline: User_name= ' Tianle '

Changes to variables:

A=1

B=a

The ID, type, and value of a are the same as B because both A and B two variables point to the same memory space address, so that variables of type str and int cannot be modified. list, dictionary can.

Four, simple program interaction:

variable = standard input

Prompts the user to enter a name and then outputs the user input

User_name=input (' plz input your name ')

Print (' Hello%s '% user_name)

Comments in Python:

Single-line Comment #

Bulk comment ""

V. Basic data types for Python

We humans can easily distinguish between numbers and characters, but the computer does not, although the computer is very powerful, but from a certain point of view and very silly, unless you explicitly tell it, 1 is the number, "Han" is the text, otherwise it is not clear 1 and ' Han ' difference, therefore, in each programming language will have a called data type of East, in fact, is the common data types are clearly divided, you want the computer to do numerical operations, you will pass the numbers to it, you want him to deal with the text, send him a string type.

1, int digital integral type

2. str String Type:

All the elements in the quotation marks in Python will be treated as strings;

Single and double quotation marks are used alone without any difference, while the use is guaranteed to be paired;

string concatenation, strings also support the addition and multiplication of operations

3. BOOL Boolean type

true| False

True: All non-0, non-null values are true

False: The above definition takes the inverse

4, List

List1=[' A ', ' B ', C]

Can be evaluated by the subscript of the list, which can hold various types of elements such as int, str, list, DIC, etc.

5. DiC Dictionary

{' A ': 1, ' B ': 2}

{Key:value} form, dictionary can be nested in dictionary, list

Vi. formatted output

------------ info of Alex Li -----------Name  : Alex LiAge   : 22job   : TeacherHobbie: girl------------- end -----------------
The value to be changed is defined as a variable, and the next time it is used, the content of the measure is changed directly.

Just need to print the format first ready, because some of the information inside the user input, you can not preset to know, so it is possible to place a placeholder, and then the string of placeholders and external variables to map the relationship between the good

name = input("Name:")age = input("Age:")job = input("Job:")hobbie = input("Hobbie:")info = ‘‘‘------------ info of %s ----------- #这里的每个%s就是一个占位符,本行的代表 后面拓号里的 name Name : %s #代表 name Age : %s #代表 age job : %s #代表 job Hobbie: %s #代表 hobbie ------------- end -----------------‘‘‘ %(name,name,age,job,hobbie) # 这行的 % 号就是 把前面的字符串 与拓号 后面的 变量 关联起来 print(info)

In addition to%s, there is%d, which is used for the placeholder number

Note that input is received by default, so the value is the str type, and if it is to be received as an element of another type, it needs to be escaped, such as int (input (' plz input you id '))
Otherwise, the type error may be prompted
7. Operators

Computer can be carried out many kinds of operations, can not only subtraction so simple, operations by type can be divided into arithmetic operations, comparison operations, logical operations, assignment operations, member operations, identity operations, bit operations, today we only learn arithmetic operations, comparison operations, logical operations, assignment operations

Arithmetic operations

The following hypothetical variables: a=10,b=20

Comparison operation

The following hypothetical variables: a=10,b=20

Assignment operations

The following hypothetical variables: a=10,b=20

Logical operations

8. Process Control--if Else

Single branch:

if 条件:    满足条件执行代码else:    if条件不满足就走这段

Multi-branch:

if 条件:    满足条件执行代码elif 条件:    上面的条件不满足就走这个elif 条件:    上面的条件不满足就走这个elif 条件:    上面的条件不满足就走这个    else: 上面所有的条件不满足就走这段

Note indentation: You will find that the next line of each condition in the above if code is indented 4 spaces, which is why? This is a feature of Python, forced indentation, to let the program know which conditions each piece of code depends on.

Process Control-Cycle

While loop

While condition:

Set up to walk here

Else

Do not set up to walk here (when the conditions of the establishment of the completion of the implementation will also go here)

Example:

count=0# defines a counter count initial value 0

While True: #while判断True条件成立, start the execution of the word code

If Count <: #if判断count小于10时, start executing the If Word code

Print (' True ') #条件成立打印

count=count+1# each condition is set to the counter +1, cycle again, until the condition is not established

else: #当上面条件不成立时, execute the following code

Break#break jump out of all loops, end

Python Learning bit record-day01

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.