Python Learning Note (ix) Statement 1
Print
In Python2, print is a statement, and in Python3 it is a function.
Example 1:
>> print "hello,world!"
hello,world!
>> print "Hello", "world!"
Hello world!
Description: In the print statement, the string is followed by a \ n symbol, which is a newline! However, if you want to follow a comma after a string, the line break is canceled, as follows:
Example 2:
>> for i in [1,2,3,4]:
... print I
...
1
2
3
4
>> for i in [1,2,3,4]:
... print I,
...
1 2 3 4
Import
Example 3:
>> Import Math
>> Math.pow (3,2)
9.0
Or is:
>> from Math Import POW # #适合引入模块较少的情况. If the introduction of more than a module, the readability is reduced, you will not know that the function from that module, such as instance 4
or >>> from math import *
>> Pow (3,2)
9.0
Example 4:
>> from math Import pow as Pingfang # #对pow重命名, using Pingfang () is equivalent to using POW ()
>> Pingfang (3,2)
9.0
Introduction of multiple functions, standard practice:
Example 5:
>> from math import pow,e,pi
>> Pow (E,PI) # #e是欧拉数; pi is π (PAI)
23.140692632779263
Assignment statements
A = 3 is an assignment statement
Example 6:
>> x, y, z = "WTF", 1,[' love ', ' You ']
>> x
' WTF '
>> y
1
>> Z
[' Love ', ' You ']
Example 7:
>> A
(' Love ', ' Datagrand ')
>> type (a)
<type ' tuple ' >
if--conditional statements
Example 8:
>> A = 4
>> if a! = 5: # #冒号是必须的, only the return is true before the following statement is executed
.. print a # #要缩进四个空格
...
4
If...elif...else
Basic Style structure:
If condition 1:
Statement Block 1
Elif Condition 2:
Statement Block 2
Elif Condition 3:
Statement Block 3
。。。
Else
Statement Block 4
Example 9:
Cat shiyan.py
#!/usr/bin/env python
#coding: Utf-8
Print "Please enter any integer number:"
# #raw_input () The number entered is a string
# #int () Convert the string to an integer
Number = Int (raw_input ())
If number = = 10:
Print "The number you entered is:%d"%number
Print "You are great!"
Elif Number > 10:
Print "The number you entered is:{}". Format (number)
Print "The number is more than 10."
Elif Number < 10:
Print "The number you entered is:{}". Format (number)
Print "The number is less than 10."
Else
Print "Is you a human?"
Ternary operator
Syntax: a = b if c else D
(1) If C is true, then execute a = b
(2) If C is false, then execute a = d
Example 10:
>> name = "WTF" if "Didi" Else "Zhao" # #if "Didi" equivalent to if bool ("Didi")
>> Name
' WTF '
>> name = "WTF" if "" Else "Zhao" # # if "" Equals if bool ("") PS: two quotes without spaces
>> Name
' Zhao '
For loop
Grammar:
For loop rule:
Action Statement
Example 11:
For loop--string
>> Hello = "World"
>> for i in Hello:
... print I
...
W
O
R
L
D
Example 12:
For loop--list
>> Wtf_python = ["Data", "Grand", "Welcome You", "" "]
>> Wtf_python
[' Data ', ' grand ', ' welcome you ', ']
>> for World in Wtf_python:
.. print World
...
Data
Grand
Welcome
>>
Example 13:
For loop--tuple
>> C = ("WTF", "Datagrand")
>> type (c)
<type ' tuple ' >
>> for I in C:
... print I
...
wtf
Datagrand
Example 14:
For Loop--Dictionary
>> d = {"WTF": "Python", "Didi": "Data"}
>> Type (d)
<type ' Dict ' >
>> for I in D:
... print I
...
Didi
wtf
The number is not allowed for the for loop
Because the Int object is not iterative, that is, the object applied by the For loop should be iterative.
Example 15:
For loop--number (not for loop)
>> for I in 123:
... print I
...
Traceback (most recent):
File "<stdin>", line 1, in <module>
TypeError: ' int ' object is not iterable
A way to determine whether an object can be iterated:
Example 16:
>> Import Collections
>> isinstance (123,collections. iterable)
False # #不可迭代
>> isinstance ([1,2,3],collections. iterable)
True # #可迭代
Range
Grammar:
Range (Start,stop[,step])
Syntax Description:
Start: Starting value, default is 0, that is, if you do not write this, you think start=0
Stop: The value of the end must be written.
Step: Change the step size, the default is 1, that is, do not write, that is, the step size is 1. Resolute cannot be 0.
Range () function features:
(1) This function can create a list of numeric elements;
(2) often used for a For loop
(3) The parameter of the function must be an integer, starting from 0 by default.
(4) Step default is 1. If you do not write, just follow this value;
(5) Step can not be zero, if equal to zero error;
(6) If step is a positive number, the last value of the returned list does not contain the stop value, that is, the value of Start+istep is less than stop, and if step is negative, the value of Start+istep is greater than stop.
Example 17:
>> Range (5)
[0, 1, 2, 3, 4]
>> Range (0,5)
[0, 1, 2, 3, 4]
>> Range (0,5,1)
[0, 1, 2, 3, 4]
>> Range (1,5)
[1, 2, 3, 4]
>> Range (1,5,2)
[1, 3]
>> Range (0,-9,-1)
[0,-1,-2,-3,-4,-5,-6,-7,-8]
Example 18:
Find a positive integer less than 100 that can be divisible by 3
Cat range.py
#!/usr/bin/env python
#coding: Utf-8
WTF = []
For n in range (1,100):
If n% 3 = = 0:
Wtf.append (N)
Print WTF
Perform:
Python range.py
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
Python Learning Note (ix) Statement 1