Python Learning article: Python Introduction and Getting Started

Source: Internet
Author: User

Introduction and Features
    1. python language was developed by Guido van Rossum in 1989 and eventually published in early 1991.
    2. python is an object-oriented, interpreted computer language, the syntax is concise and clear, known as glue language, portability is better, and has a large standard library.
    3. python has many development environments, Idel (Python built-in IDE), Pythonwin (for Windows), Pycharm, and so on
    4. Advantages: Easy to learn, and open source, extensible, can be embedded in the C + + language, to provide script functionality to program users
    5. cons: Use indentation to differentiate the statement format, causing a lot of inconvenience, the speed of operation is slower than C + +, Python code cannot encrypt
Python Environment Installation

Windows installation  

  1、下载安装包

    https://www.python.org/downloads/

  2、安装

    默认安装路径:C:\python35, 一路next

  3、配置环境变量

    高级系统设置】--》【高级】--》【环境变量】--》【系统变量path】 --> 【Python安装目录追加到path变值中,用 ; 分割】

  4、测试python安装是否成功,cmd打开命令行输入python 命令

Linux Installation

without installation, Linux comes with Python, updates the Python version, and can be reinstalled with the Yum command

Python Basics

"First Python program"

Python source code file with "py" as the suffix, create a file named "hello.py" for the output string "Hello wolrd"

Description: The first line is equivalent to the main () function in C, which is the entry for the Python program

The second line of code uses the print statement to output the string "Hello world!"

Comments

    1. A line of code comments, using "#" plus a few spaces to start, followed by the contents of the comment
    2. Multiple lines of code comment, using three single quotation marks "', followed by annotated content, end Plus ' ' End

"Variables"

Variable naming rules

      • Variable names can only be any combination of letters, numbers, or underscores
      • The first character of a variable name cannot be a number
      • 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 ']

Variable scope:

What is stored in an address in memory

A local variable can only be used within a function or code snippet, and once the function or snippet has ended, the life cycle of the local variable ends

A global variable is a variable that can be shared by a different function, class, or file, and a variable defined outside of a function is called a global variable.

constant

Constants are variables that cannot be changed once initialized, such as the number ' 5 ', and the string ' abc ' is constant.

"Underlying data type"

Python has several built-in basic data types-numbers, strings, tuples, lists, and dictionaries.

1. Digital

Python3 supports int, float, bool, complex (plural).

In Python 3, there is only one integer type int, represented as a long integer, and no long in Python2.

    • Integer:

int=12;
print int;

    • Floating point number:

float=2.3;
print float;

    • Boolean type:

Bool=false;
rint bool;
Bool=true;
print bool;

Note: There is no Boolean in Python2, which represents False with the number 0 and 1 for True. To Python3, True and False are defined as keywords, but their values are still 1 and 0, and they can be added to the numbers.

    • Plural:

The complex number is made up of real and imaginary parts, which can be represented by a + BJ, or complex (A, a, b), and the real and imaginary parts of a complex number are floating-point types, such as 9.32e-38j,88j

2. String

There are 3 ways to represent strings in Python-single quotes, double quotes, triple quotes, single quotes, and double quotes.

Use note points:

    1. Enclosed in double quotes, enclosed in single quotes, enclosed in single quotes
    2. If there are double quotation marks inside, use three single quotes ""
    3. Three single quotes with bulk comment function

3. Conversion of numeric types

Operation

1. Arithmetic operators

2. Comparison operators

3. Assignment operators

4, bitwise operators

5. Logical operators

6. Member Operators

7. Identity operator

"Control Statement"

1, If....else .....

Condition judgment: Not 0 is true, non-empty is true

If condition_1:

Statement_block_1

Elif condition_2:

Statement_block_2

else:

Statement_block_3

Example: judging the corresponding level according to the input score

2. While....else ...

While ... else statement block that executes else when the conditional statement is false

3. For...else ...

For <variable> in <sequence;:

<statements>

else:

<statements>

4. Break and Continue

The break statement can jump out of a for and while loop body. If you terminate from a for or while loop, any corresponding loop else blocks will not execute.

The continue statement is used to tell Python to skip the remaining statements in the current loop block and proceed to the next round of loops.

"Input and Output"

1. Input

Python provides the input () function to read a line of text from the standard input, and the default standard input is the keyboard.

Input can receive a Python expression as input and return the result of the operation.

Note: For security purposes, the password entered does not need to be echoed, you need to introduce the standard library Getpass, directly using the Getpass.getpass method can not be echoed at the time of input, need to run in terminal.

2. Output

    • string and numeric types, can be directly lost, such as: print (' Hello World '), print (3)
    • Variable, no matter what type, value, Boolean, List, dictionary ... can be directly output

name = ' Mark '

Print (a)

    • Formatted output

(1).% character: The beginning of the token conversion specifier

(2). Convert flag:-Indicates left alignment, + indicates a positive sign before the value is converted, "" (white space character) to retain a white space before, and 0 for the conversion value if the number of digits is not enough. 0 padding

(3). Minimum field width: The converted string should have at least the width specified by the value. If it is *, the width is read from the value tuple.

(4). dot (.) Heel Precision Value: If the conversion is real, the precision value represents the number of digits after the decimal point. If the string is converted, the number represents the maximum field width. If it is *, then the precision will be read from the tuple

(5). string format conversion type

Conversion type meaning

D,i signed Decimal integer
o unsigned octal
U non-signed decimal
x hexadecimal without symbol (lowercase)
X hexadecimal without symbol (uppercase)
Floating point number represented by the e-scientific notation (lowercase)
The floating-point number represented by the E-scientific notation (uppercase)
F,f decimal Floating-point number
G If the exponent is greater than-4 or less than the precision value is the same as E, the other case is the same as F
G if the exponent is greater than-4 or less than the precision value is the same as E, the other case is the same as F
C Single-character (accepts integers or single character strings)
R string (convert any Python object using repr)
s string (convert any Python object using str)


Python Learning article: Python Introduction and Getting Started

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.