Python learning diary (second week), second week of python

Source: Internet
Author: User

Python learning diary (second week), second week of python

I have officially learned the Python language since this week. I will share with you an article on learning Python every week!

PythonInstallation

The first step of learning is to have a runtime environment. So next we will introduce the installation steps.

On the official Python website, we can see that Python is cross-platform and runs on Windows, Mac, and various Linux/Unix systems. Writing a Python program on Windows can also be run on Linux.

 

Note that Python has two versions: 2. x and 3. x, which are incompatible. In the future, py3 will be a trend, but there are still a large number of py2 programs.

Therefore, I have installed different versions of Python on two system platforms.

InWindowsInstall OnPython

1. Select a 32-bit or 64-bit installation package based on your windows system configuration. For example, I downloaded the 64-bit installation package for version 2.7.11.

2. Add the Python installation directory to PATH (in Python3.5, you can select Add to PATH during installation)

In win10, this is the case.


InLinuxInstall OnPython

Install in ubuntu 16.04

Python 16.04 and Python 2.7 are installed in ubuntu 3.5 by default.

You only need to select the execution environment. If not installed, you can run the following command in BASH:

 

sudo add-apt-repository ppa:fkrull/deadsnakessudo apt-get updatesudo apt-get install python3.5

 

Install in centos 6.7

Update centos 6.7 to version 2.7.11 under 2.6.6 of the default version installed in centos.

 

Yum install gccwget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tar.xzxz-d Python-2.7.11.tar.xztar-xvf Python-2.7.11.tar compilation installation. /configure make all makeinstall view version/usr/local/bin/python2.7-V modify the default Python version mv/usr/bin/python/usr/bin/python2.6 ln-s/usr/ local/bin/python2.7/usr/bin/python prevents yum execution exceptions, modify the Python version vi/usr/bin/yum used by yum to set the header #! /Usr/bin/python #! /Usr/bin/python2.6

 

PythonInterpreter version

When writing Python code, we get a text file containing the Python code with the. py extension. To run the code, you need the Python interpreter to execute the. py file.

CPython

After downloading and installing Python 3.5 from the official Python website, we directly obtained an official version of the interpreter CPython. This interpreter is developed in C language, so it is called CPython. Running python in the command line is to start the CPython interpreter.

CPython is the most widely used Python interpreter. All the code of the tutorial is also executed under CPython.

IPython

IPython is an interactive interpreter based on CPython. That is to say, IPython only enhances the interaction mode, but the function of executing Python code is exactly the same as that of CPython. For example, although many Chinese browsers have different appearances, the kernel actually calls IE.

CPython uses >>> as the prompt, while IPython uses In [serial number]: As the prompt.

PyPy

PyPy is another Python interpreter whose goal is the execution speed. PyPy uses JIT technology to dynamically compile Python code (note that it is not an explanation), so it can significantly improve the execution speed of Python code.

Most Python code can be run in PyPy, but PyPy and CPython are somewhat different, which leads to different results for the same Python code to be executed in two interpreters. If your code is to be executed in PyPy, you need to understand the differences between PyPy and CPython.

Jython

Jython is a Python interpreter running on the Java platform. It can directly compile Python code into Java bytecode for execution.

IronPython

IronPython is similar to Jython, except IronPython is a Python interpreter running on Microsoft. Net platform. It can directly compile Python code into. Net bytecode.

FirstPythonProgram

The first program to learn the language must be HelloWorld. Here we do not create a file named helloworld. py in the directory. The content of the file is as follows:

 

1#!/usr/bin/env python  2  3print"hello,world"

 

Ps :#! /Usr/bin/env python will tell the system what the Python interpreter is.

Ps2: print "hello, world" in Python3, an error is reported and must be changed to print ("hello, world"), but 2.7.11 is also compatible with print ()

Ps3: Execution permission required

PythonContent Encoding

In Python2.x, the default encoding format is ASCII, so Chinese encoding is not recognized. Therefore, to use Chinese in Python 2. x, you must add a line before the program starts. Python3 does not have this problem

 

#!/usr/bin/env python# -*- coding: utf-8 -*-

 

Comment format

In Python, you can add # comment before the line you want to comment. When you want to comment multiple lines, you can use "to annotate the range.

#! /Usr/bin/env python #-*-coding: UTF-8-*-# I am a single line comment # import timeprint ("hellow, world ") # The following multi-line comment "num = 0 while num <= 100: if num % 2! = 0: print (num) num + = 1 print ("end ")"""

Variable

1. data types are not required for variable declaration in Python.

2 variable name:

  • ['And', 'as', 'assert ', 'Break', 'class', 'contine', 'def', 'del ', 'elif ',

'Else', 'Got t', 'exec ', 'Finally', 'for ', 'from', 'global', 'if', 'import ',

'In', 'is, 'lambda ', 'not', 'or', 'pass', 'print ', 'raise', 'Return ', 'try ', 'while', 'with', 'yield ']

#! /Usr/bin/env python #-*-coding: UTF-8-*-# declare variable # import time name = "lzx" age = 22 grilorboy = Trueage2 = 22name2 = "lzx" print id (age) print id (age2) print id (name) print id (age2)

The execution result of the program is as follows:


Summary:

The id () method can obtain the memory address of a variable. The program running result shows that the value assignment of the variable is essentially

Get input content

To get user input in Python, you can get user input content in raw_input (py2) input (py3 ).

#! /Usr/bin/env python #-*-coding: UTF-8-*-# test the input content import getpassuserna = input ("UserName") passwd = getpass. getpass ("password") print usernaprint passwd

You can use the gatpass method in the getpass module to make the password invisible.

Condition judgment

Python uses if for conditional judgment. The format is

If condition:

Execution statement meeting condition 1

Else:

Execution statement that meets condition 2

Note that 1 must have: 2 when condition 1 is met, if will not execute the following judgment

age = 3if age >= 18:    print('your age is', age)    print('adult')else:    print('your age is', age)    print('teenager')

You can also use the if elif format.

If <condition judgment 1>: <execution 1> elif <condition Judgment 2>: <execution 2> elif <condition judgment 3 >:< execution 3> else: <execution 4>
While Loop

While loop is used

Whil condition:

Code executed cyclically

Do not lose the following:

2. When a break appears in a loop, immediately stop the following code in the loop.

3. continue is used to exit the current loop and continue the next loop.

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.