Python basic syntax

Source: Internet
Author: User

First, line and indent

The biggest difference between Python and other languages is that Python's code block does not apply braces {} to control classes, numbers, and other logical judgments. Python's most distinctive feature is the use of indentation to write modules .

The amount of white space to indent is variable, but all code block languages must contain the same amount of indentation whitespace, which is strictly enforced.

As shown below:

If True:print "True" Else:print "false"

The following code will execute the error:

#!/usr/bin/python#-*-coding:utf-8-*-# if True:print "Answer" print "True" Else:print "Answer" # no strict indentation, Error print "False" on execution

after executing the code, the following error occurs:

$ python test.py File "test.py", line 5 if True: ^indentationerror:unexpected indent

Second, print output

#! /usr/bin/python#-*-coding:utf-8-*-x= "A" y= "B" Print (x) print (y)

Third, if statement

Four, the dictionary
A dictionary is another mutable container model and can store any type of object.

Each key value of the dictionary (Key=>value) is split with a colon (:), each pair is separated by a comma (,), and the entire dictionary is included in parentheses ({}).

The format is as follows:

D = {key1:value1, key2:value2}

The key must be unique, but the value does not have to be.

The value can take any data type, but the key must be non-becoming, such as a string, a number, or a tuple.

A simple Dictionary instance:

Dict = {' Luwenjuan ': ' 123 ', ' Ningji ': ' 18 '}

You can also read the following dictionary:

Dict1 = {' abc ': 4456};

Dict2 = {' abc ': 123,456,45:12};

#!/usr/bin/python dict = {' Name ': ' Zara ', ' age ': 7, ' Class ': ' First '}; Print "dict[' Alice ']:", dict[' Alice ');

The result of the above example output:

dict[' Alice ']: traceback  (most recent call last):  file  "test.py",  line 5, in <module>    print  "dict[' Alice ']: ",  dict[' Alice ']; keyerror:  ' Alice ' 
Five, modify the dictionary to add new content to the dictionary method is to add new key/value pairs, modify or delete the existing key/value pairs of the following instance: #!/usr/bin/python dict = {' name ': ' Luwenjuan ', ' age ': ' Class ': ' Firs   T '};   dict [' age '] = 19;   doct [' School '] = "DPS School";   print "dict [' Age ']:", dict [' age '];  print "dict [' School ']:", dict [' School ']; Above example output: dict[' age ']: dict[' School ': DPS School
   


This article is from the "HCWJ" blog, make sure to keep this source http://luwenjuan.blog.51cto.com/10967115/1922431

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.