Python Basic Statement __python

Source: Internet
Author: User
Python Basic Statements

First of all, this article for the author to learn "Python learning manual" notes, and to join the author's own understanding and summary.

1, Python statement features (1) The bracket () in the IF statement is optional.
(2) A colon (:) appears at the end, representing the end of a statement.
(3) Semicolon (;) do not have to end now.
(4) Curly braces are not needed, but are indented to represent the beginning and end of a block of code.

2, if statement (1) General format

If <state1>:               # If statements, with semicolons (:) end
	<statement1>           # indents replace curly braces
elif <state2>:             # elif statements, Can have more than
	<statement2>
else:                      # Else Statement
	<statement3>
(2) contains only the IF statement.
>>> if x > 1:
	print "True"
(3) If and else are used together.
>>> if x > 1:
	print "True"
else:
	print "False"
(4) Multi-route selection elif.
>>> if x <-1:
	print "x <-1"
Elif x < 0:
	print "x < 0"
Elif x < 1:
	print "X & Lt 1 "
Else:
	print" x >= 1 "

3, while statement (1) General format.

While <state1>:            # WHERE statement, semicolon (:) end
	<statement1>
else:                      # Else statement, loop normal end call
	< Statement2>
(2) while being used alone.
>>> x = "helloworld!"
>>> while x:               # x is empty list
	print x[0],
	x = x[1:]
H e l l o r L d!
(3) Else is called at the end of the loop.
>>> L = [1, 2, 3, 4]
>>> while L:
	print l[0],
	L = l[1:]
else:                      # Loop End, call Else statement 
  print 6
1 2 3 4 6

4, for statement (1) General format.

For <target> in <object>.:  # for statement, with semicolon (:) end
	<statement1>
else:                      # Else statement, loop normal end call
	<statement2>
(2) for use alone.
>>> for x in [1, 2, 3, 4]:
	print x,
1 2 3 4
>>> to X in ("Hello", "World"):
	print x
  
   hello World

  
For use tuple assignment.
>>> t = [(1, 2), (3, 4), (5, 6)]
>>> for (A, B) in T:
	print A, b
>>> for item in T:
	A, b = Item
	print A, b
1 2
3 4
5 6
For the dictionary operation, the dictionary operation is actually a list of keywords.
>>> d = {"A": 1, "B": 2, "C": 3}
>>> for item in D:
	print Item, D[item]
a 1
c 3
b 2
(3) Else is called at the end of the loop.
>>> L = [1, 2, 3, 4]
>>> for x in L:
	print x,
else:
	print 6
1 2 3 4 6

5, break and continue statements are used in circular statements. break is used to jump out of loops.

>>> x = "helloworld!"
>>> while True: if
	x:                  # x is an empty list
		print x[0],
		x = x[1:]
	Else:                  # x is an empty list, jump out of the loop
		break
H e l l o W o r l d!
Continue is used to jump to the top of the loop.
>>> L = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for x in L: if
	x% 2 = 0:         # If x is even, skip the following statement
		C Ontinue print
	x,               # This method only prints odd
1 3 5 7 9

6. Pass Statement

Pass is an empty placeholder statement.

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.