Python's Road "first: Python Basics"

Source: Internet
Author: User

One: Use of Python

1, two versions of Python: python2.0 and python3.0. The difference between the two versions is that Python3 is not backwards compatible with Python2 components and extensions, but in two versions of python2.6 and 2.7 will continue to be compatible with python2.0 and 3.0 two versions. The simple point is that python2.6 and 2.7 are the transition versions of version 2.0 to 3.0, while the 2.7 version of Python will be the last 2.0 version, and then all will use the Python version of 3.0.

Installation of python3.x in Windows:

1 1 , download the installation package 2     HTTPS://www.python.org/downloads/32, installation 4     Default installation path: C:\python2753, configuring environment variable 6    "Right-click Computer"-"Properties"-"Advanced system Settings"- -"Advanced"-"Environment variable"-"in the Second content box, select a row of the variable named path, click Edit"- "append the Python installation directory to the variable value, use; split"7     such as: the original value; C:\python27, remember that there's a semicolon in front

2. The first Python program:

On-screen output: Hello word!

First create the hello.py script in Linux (all Python programs are named with a. Py end, indicating that this is a Python program)

1 Vim hello.py
1 #!/usr/bin/env python3 #声明python解释器; Because Linux may be installed in different locations, so use env to the path path automatically find 2 #-*-coding:utf-8-*-#支持中文字符集3 Print ("Hello word!")
1 chmod +x hello.py #加入执行权限2./hello.py #执行hello. py Script

The above operation will then be output in the window:

2. Python syntax specification:

It should be more prescriptive when Python writes code: All levels of code indentation must be the same. It should be noted that the use of tabs in different operating systems, in our common Windows and Linux Systems, tab Self-indentation of the number of spaces is different (Windows System tab By default is 4 spaces, which is in line with the development of the basic specification). It is recommended that you write Python programs in your Windows system. The indentation of Python is forced indentation, and code indentation at the same logical level must be consistent.

3, Python variables;

Use rules for variables: variable names can only be made up of numbers, letters, underscores, and variables cannot be defined with the beginning of a number. Note: The variable name cannot be the same as the built-in keyword! Python distinguishes uppercase and lowercase letters!

The role of a variable: pointing to the contents of an address in memory

4, the assignment of variables:

A = 1 #变量a赋值 1;b = 2 #变量b赋值 2;
A = 1 B = A #变量a, B is assigned a value of 1, which means that A/b points to 1 at the same time;
1=2 #变量a重新赋值 2, and points back to 2;

5, User Interactive assignment:

#! /usr/bin/env    -*-coding:utf-8 -*-= input (" Enter your name:" ) #将用户输入的内容赋值给name变量print (name)   #打印用户输入的内容

Two: data type;

1. Number:

int type (integral type):

The number of integers on a 32-bit machine is 32 bits, and the range of values is -2**31~2**31-1;

The number of integers on a 64-bit machine is 64 bits, and the range of values is -2**63~2**63-1;

Long (length integer): End with L;

Float (floating point type):

Used to process numbers with decimals, i.e. real numbers

2. Boolean value (BOOL):

' True ' or ' false ', respectively, with 1 and 0 representing true and false.

3, String:

The common function of strings is to remove whitespace, split, length, index, slice

4. List:

Basic operations for lists: index, slice, append, delete, length, slice, loop, include

= ['Alex','xiaoming','xiaohei' ]
Actions on a list:
Name_list.append (' Alex ') #在列表的末端增加名为alex的新元素;
Name_list.index (' Alex ') #去列表中查找alex的索引值, but only returns the index value of the first Alex found;
Name_list.coount (' Alex ') #统计列表中alex的数量;
Name_list.insert (2, ' Peilan ') #在列表的第二个位置插入 ' Peilan ';
Name_list.pop () #默认删除列表的最后一个元素;
Name_list.remove (' Alex ') #指定删除alex这个元素;
Name_list.reverse () #对列表进行反转排序;
Name_list.sort () #按照ascii码进行排序;
Name_list[0:2] #对列表进行切片: The principle is Guxio disregard the tail;
Name_list[0:4:2] #隔一个切一个;
A.extend (b) #列表b加入列表a中;

5, tuple (tuple):

Index, slice, length, include, loop

= (1,2,3,4)
Conversions between tuples and lists:
A is a list, B is a tuple;
List (b): Tuple B is converted to List B;
Tuple (a): List A is converted to tuple A;

6. Dictionary:

Common operations: indexes, additions, deletions, key-value-key-value pairs, loops, lengths

7. The difference between break and continue:

Break: A loop that jumps out of this layer;

Continue: Jump out of this cycle

8, write a simple guessing number game:

#!/usr/bin/Env python3#-*-coding;utf-8-*-Luck_number=6 #幸运数字定义为: 6; forIinchRange3): #定义游戏只有3次输入机会;=int(Input ("Please input your number:") ) #用户输入自己猜测的数字; ifNumber >Luck_number:print ("your number is large") #如果用户输入的数字大于6, the output: your number is large; Elif Number<Luck_number:print ("your number is small") #如果用户输入的数字小于6, the output: your number is small; Else: Print ("Bingo!!!") #如果用户输入的数字是6, the output: Bingo!!!; At the same time the cyclic termination; BreakElse: Print ("too many times") #如果用户输错的次数大于3次, the output: too many times; and the game terminates.

Python's Road "first: Python Basics"

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.