Python basic syntax

Source: Internet
Author: User

Python basic syntax

The Python language has many similarities with languages such as Perl,c and Java. However, there are some differences.

In this chapter we will learn the basic syntax of Python in the future


Interactive programming

Interactive programming does not require creating scripts, but rather writing code through the Python interpreter interaction pattern.

Pythonpython 2.7.6 (default, Sep 9, 15:04:36) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on Darwintype " Help "," copyright "," credits "or" license "for more information.>>>


Installing the interactive client on Windows

650) this.width=650; "src=" https://s3.51cto.com/wyfs02/M00/8F/D2/wKioL1js3vbjwKT7AACQAjnIusY146.jpg "title=" 1.JPG "alt=" Wkiol1js3vbjwkt7aacqajniusy146.jpg "/>



Enter the information in the Python prompt

>>> print "Hello, python!";


In the Python 2.7.1 the output is

Hello, python!.


Print: Multiple characters can be printed for channeling, separated by a string of outputs

Print (a), (b), (? )


Enter information in the Python prompt (version issue high version requires plus () otherwise error)

>>> print ' A ', ' B ', '? ';


Displayed as

A B?


Python identifiers

In Python, identifiers are composed of letters, numbers, and underscores.

In Python, all identifiers can include English, numeric, and underscore (_), but cannot begin with a number.

Identifiers in Python are case-sensitive.

Identifiers that begin with an underscore are of special significance. A class attribute that begins with a single underscore (_foo) cannot be accessed directly, and is accessed through the interface provided by the class and cannot be imported with "from XXX import *";

A double underscore (__foo) represents a private member of a class; a double underscore (__foo__) represents a special method-specific identifier for Python, such as __init__ (), which represents the constructor of a class.


Variable

What is a variable:

One can be variable value

The variables in the program are purely in memory to open up a place where the data can be stored, and then seven names, the name is the variable

Variables can store any data, variable declarations must be assigned values

Variable name can be people have to go, must be case in English, numeric underline, can not start with numbers


Define variables A and B to assign values separately

>>> a=123

>>> B=sam


Output variable View results

>>> Print a

123

>>> Print B

Sam

>>>

Variable characteristics, variable itself type is not fixed, Python dynamic language, the advantages of more flexible


Defining strings in Python

‘’“”

If the middle character is channeling ' "', you can use the translator \\\\\\\\\


>>> a= ' 123 '

>>> b= ' 1 '


Run an error after defining the string

>>> Print A*b


Traceback (most recent):

File "<pyshell#22>", line 1, in <module>

Print A*b

Typeerror:can ' t multiply sequence by non-int of type ' str '


Undefined string to display normally

>>> a=1

>>> b=2

>>> Print A*b

2

-----------------------------------------------------------------------------------------------the delimiter of one ear



Look at the list.

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/8F/D3/wKioL1js4_HiqPZwAABXIhbaToc107.jpg "title=" Timg.jpg "alt=" Wkiol1js4_hiqpzwaabxihbatoc107.jpg "/>

This article is from the "three-pole Walker" blog, please be sure to keep this source http://namesam.blog.51cto.com/12364721/1915041

Python basic syntax

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.