Python Quick Start

Source: Internet
Author: User

One, the string

Characters in Python are defined as the character set between quotes, using the index operator ([]) and the Slice operator ([:]) to wait for a substring. A string has its own index rule: The index of the first character is 0, and the last character's index is-1.

The plus sign (+) is used for string join operations, and the asterisk (*) is used for string repetition.

The following are examples:

>>> pystr= ' python '
>>> isool= ' is cool! '
>>> Pystr[0]
' P '
>>> Pystr[2:5]
' Tho '
>>> Isool[:2]
' Is '
>>> Isool[:1]
I
>>> Isool[-1]
‘!‘
>>> Isool[-2]
L
>>> Pystr + Isool
' Pythonis cool! '
>>> pystr + ' + isool
' Python is cool! '
>>> PYSTR * 2
' Pythonpython '
>>> '-' * 2
‘--‘

>>> pystr = "' Python
... is cool ""
>>> Pystr
' Python\nis cool '
>>> Print Pystr
Python
Is cool

Second, list and tuple

Lists and tuples can be used as normal "arrays", which can hold any number of types of Python objects, as well as arrays, by accessing elements from a numeric index starting at 0, but lists and tuples can store different types of objects.

There are several important differences between a list and a tuple. The list element is wrapped in brackets ([]), and the number of elements and the value of the element can be changed.

Tuple elements are wrapped in parentheses (()) and cannot be changed (although their content can). Tuples can be viewed as read-only lists. A subset can be obtained by slicing operations ([] and [:]), which is the same as the way strings are used, and of course lists and tuples can be converted to each other.

Example:

>>> alist=[1,2,3,4]
>>> alist
[1, 2, 3, 4]
>>> Alist[0]
1
>>> Alist[1]
2

>>> Alist[1] = 5
>>> alist
[1, 5, 3, 4]
>>> Alist[1:3]
[5, 3]

The = = tuple can also perform slicing operations, resulting in tuples (cannot be changed)

>>> atuple = (' A ', ' B ', ' C ', ' d ')
>>> Atuple
(' A ', ' B ', ' C ', ' d ')
>>> Atuple[:3]
(' A ', ' B ', ' C ')
>>> Atuple[0:3]
(' A ', ' B ', ' C ')
>>> Atuple[3]
' d '
>>> Atuple[6]
Traceback (most recent):
File "<stdin>", line 1, in <module>
Indexerror:tuple index out of range

= = tuples and lists cross-transfer

>>> alist
[1, 5, 3, 4]
>>> Atuple
(' A ', ' B ', ' C ', ' d ')
>>> Tuple (alist)
(1, 5, 3, 4)
>>> List (atuple)
[' A ', ' B ', ' C ', ' d ']

Third, the dictionary

A dictionary is a mapping data type in Python that works like an associative array or hash table in Perl, consisting of a key-value (Key-value) pair. Almost all types of Python diagonal can be used as keys, but generally it is most commonly used as numbers or as strings.

The value can be any type of Python object, and the dictionary element is wrapped in curly braces ({}).

Example:

>>> adict = {' host ': ' localhost '}
>>> adict
{' host ': ' localhost '}
>>> adict[' host ']
' localhost '
>>> adict[' localhost ']
Traceback (most recent):
File "<stdin>", line 1, in <module>
Keyerror: ' localhost '
>>> adict[' IP ']=192.168.1.11
File "<stdin>", line 1
adict[' IP ']=192.168.1.11
^
Syntaxerror:invalid syntax
>>> adict[' ip ']= ' 192.168.1.11 '
>>> adict[' IP ']
' 192.168.1.11 '
>>> adict
{' IP ': ' 192.168.1.11 ', ' host ': ' localhost '}
>>> adict[' Port ']=80
>>> adict
{' IP ': ' 192.168.1.11 ', ' host ': ' localhost ', ' Port ': 80}
>>> adict[' ipaddress ']= ' 192.168.1.11 '
>>> adict
{' IP ': ' 192.168.1.11 ', ' host ': ' localhost ', ' ipaddress ': ' 192.168.1.11 ', ' Port ': 80}

Iv. If statement

The syntax for a standard if condition statement is as follows:

If expression:

If_suite

If the value of the expression is not 0 or the Boolean value True, the code group If_suite is executed, or the next statement is swept. A code group (suite) is a Python term that consists of one or more statements that represent a block of child code. Python differs from other languages in that conditional expressions do not need to be enclosed in parentheses.

If x < 0:

print ' "X" must be atleast 0! '

The Else statement is also supported, with the following syntax.

If expression:

If_suite

Else

Else_suite

The ELIF statement is also supported, as follows.

If expression1:

If_suite

Elif expression2:

Elif_suite

Else

Else_suite

Python Quick Start

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.