python-Basic Learning-day1

Source: Internet
Author: User

1 Introduction to Python What language is 1.1 python?

Python is a dynamically explanatory, strongly typed definition language.

The characteristics of the compiler type: The consistency is poor, running fast.

Explanatory features: Side-performing side interpretation, slow speed

1.2 Python Advantages and disadvantages
  • Python's positioning is "elegant", "clear", "simple", so the Python program looks always easy to understand, beginners learn python, not only easy to get started, but also in the future, you can write those very very complex programs.
  • Development efficiency is very high, Python has a very powerful third-party library, basically you want to achieve any function through the computer, the Python official library has the corresponding modules to support, directly download the call, on the basis of the base library to develop, greatly reduce the development cycle, to avoid repeating the wheel.
  • High-level language ———— when you write programs in the Python language, you don't have to consider the underlying details such as how to manage the memory used by your program
  • Portability ———— because of its open source nature, Python has been ported on many platforms (modified to make it work on different platforms). If you are careful to avoid using system-dependent features, all your Python programs can run on almost any system platform on the market without modification
  • Scalability ———— If you need a piece of your critical code to run faster or you want some algorithms to be private, you can write some of your programs in C or C + + and then use them in your Python program.
  • Embeddable ———— You can embed python into your C + + program to provide scripting functionality to your program users.
Disadvantages
  • Slow speed
  • Cannot encrypt
  • Threads cannot take advantage of multi-CPU issues
2 Installation of Python 2.1 windows

1. Download the installation package
https://www.python.org/downloads/

2. Installation
Default installation path: C:\python27

3. Configure Environment variables
"Right-click Computer"-"Properties"-"Advanced system Settings"-"Advanced"-"Environment variable"-"in the second content box to find the variable named path of a row, double-click"-"Python installation directory appended to the variable value, with;

如:原来的值;C:\python27,切记前面有分号
Note: Windows installs both python3.5 and python2.7, configuring environment variables after you enter PYTHON2 hint: not an internal or external command, or a program that can be run
  1. environment variable already configured
  1. Modify the Python.exe name to Python2.exe according to the installation path of the python2.7
  1. cmd command Direct input Python2 normal
2.2 Linux Installation python2.7

Download the source code and recompile the installation:

1. cd/usr/local/src/2. wget http:Python.org/ftp/python/2.7.8/python-2.7.8.tgz3. Tar XF Python-2.7.8.tgz4. CD Python-2.7.85:/configure--prefix=/usr/local/python276. Make && make install 7. MV /usr/bin/python / Usr/bin/python26  #将系统安装的重命名转移 8. ln-s /usr/local/python27/bin/python /usr/bin/ #创建软连接直接调用2.7 version Python9. Vim /usr/bin/yum ##!/usr/bin/python2.6 Modify the default Python version of the original system to ensure that the Yum command is applied  
2.3 Linux Installation python3.5

Download the source code and recompile the installation
Download the Python3.5 source package and compile it

--prefix=/usr/local --enable-sharedmakemake install温馨提醒:如果编译遇到如下问题:Ignoring ensurepip failure: pip 7.1.2 requires SSL/TLS解决方法:yum install -y openssl openssl-devel继续:ln -s /usr/local/bin/python3 /usr/bin/python3在运行Python之前需要配置库:echo /usr/local/lib >> /etc/ld.so.conf.d/local.confldconfig可以设置别名:alias py=python3方便使用
3 Hello World Program code writing

Program Execution Information:

After executing python on Windows, enter the python interaction:

>>> print ("Hello world!")Hello world!

Note: Python2 's raw_input () = = Python3 input () but execution without parentheses in Python3 will cause an error:

>>> print "Hello world!"File "<stdin>", line 1print "Hello world!" ^SyntaxError: Missing parentheses in call to ‘print‘
3.1 Small Knowledge points
  • Exit Python Interactive: Exit () Windows CTRL + Z enter Linux down CTRL + Z exit
  • = The left is always the variable name = right string, you need to add a "string" number can not be added, operation: +-*/
  • Print ("string", variable) #数字不需要加引号 referenced variable without quotation marks
  • Output Chinese in python2 need to declare character encoding: #-*-coding:utf-8-*-,python3 default is UTF-8 character encoding
  • A binary digit =1bite bit = The minimum storage unit for the computer. 8 bit = 1bytes byte 1024bytes = 1kbytes KB
  • Double quotes are the same as single quotes: Single and double quotes
  • Single-line and multiline comments: Ctrl +/
  • Multiline comment: ' ' three double quotes represent a single paragraph string.
  • Ctrl +D copy when moving forward
  • ID () To view variable memory address
3.2 Variable naming rules: character encoding and variable declaration

Python2 you must declare character encoding #_ *_coding:utf-8_*_

The default is utf-8 in Python3

Name = "Stone"

The code above declares a variable named: Name, and the value of the variable name is: "Stone"

Rules for variable definitions:
  • 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 ', ' Finall Y ', ' for ', ' from ', ' global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' Try ', ' While ', ' with ', ' yield ']
Variable Program instance:
name = input("name:")   定义变量age = input("age:")job = input("job:")hobby = input("hobby:")info = ‘‘‘ 定义输出变量-----info of %s ----- Name : %sAge : %sJob : %sHobby: %s----- end ----- ‘‘‘ %(name,name,age,job,hobby)print(info) 输出 
3.3 User interaction
>>> name = input("pls input your name:")pls input your name:stone>>> name‘stone‘

Assign the value of input to the name variable and enter what name is

#!/usr/bin/env python#_*_coding:utf-8_*_#name = raw_input("What is your name?") #only on python 2.xname = input("What is your name?")print("Hello " + name )

When entering a password, if you want to be invisible, you need to take advantage of the Getpass method in the Getpass module, namely:

#!/usr/bin/env python# -*- coding: utf-8 -*-import getpass# 将用户输入的内容赋值给 name 变量pwd = getpass.getpass("请输入密码:")# 打印输入的内容print(pwd)
3.4 article judgment and first line indent guessing age program
#!/usr/bin/env python       #指定解释器为pythonright_age = 23              #首先定义正确的年龄guess_age = int(input("please input your guess num:"))#将input的值定义一个变量名if right_age == guess_age: #进行条件判断 正确(注意判断后加":") print("right") #注意缩进统一级别缩进要相同elif guess_age > right_age: #猜的年龄大于真实年龄 print("please guess smaller!")else: print("please guess bigger!") #猜的年龄小于真实年龄
Guess age-limit cycle times
#!/usr/bin/env pythonright_age =  #定义正确年龄 for N in range (3): #执行3次, not guessed to exit Guess_age = in T (input ("Please input your guess num:")) if right_age = = Guess_age:print ("right") Break  # Exit when layer loops elif guess_age > Right_age:print ("please guess smaller!") else:print ("please guess bigger!") else: #猜测次数过多, exit print ("Too many times!!!")          
Small Knowledge Points:

Indentation Error Prompt: Note that code indentation at the same level must be consistent.

Select Multiline +tab to indent multiple lines

Type + variable output variable types

python-Basic Learning-day1

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.