I. Python's Annotation method
Python uses # and ' ' to annotate, where # is used for a line of comments, ' ' for multiline comments. It is important to note that the content assigned to a variable can be used to print multiple lines.
1 # Author:sun 2 3 " " Author 4 Sun " "
Name=input ("pleaseinput your name:")The Age =int (input ("please input Your age:")) Sex=input ("pleaseinput your sex:") Info ="'name:%s \nage:%d \nsex:%s"% (name,age,sex)
Two. Python's input/output method.
Python uses print ("text") to output, using Paramater=input ("text") to enter from the keyboard.
Print ("HelloWorld") name=input ("pleaseinput your name:" )print("", name)
Three. Python runs under Linux
If you want to bring Python with non-linux like python3.6, you should configure the default Python in advance. The first is the environment variable, and the second is the character format.
Python files under Linux can be run directly with the./***. However, some settings need to be made to insert the following code at the beginning:
# ! user/bin/env python # -*-coding:utf-8-*- # author:***
Four. Concatenation of strings in Python
There are three ways to string concatenation in Python:
1. Stitching with the + sign: You can stitch the string directly with the + number
Name=input ("pleaseinput your name:")The Age =input ("pleaseinput your Age:") Sex=input ("pleaseinput your sex:") Print("name:"+ name + "" " \nsex:"+ Sex"
where/n is a newline escape character.
2. Stitching in%
Name=input ("pleaseinput your name:")The Age =int (input ("please input Your age:")) Sex=input ("pleaseinput your sex:") Info ="name:%s \nage:%d \nsex:%s"%(name,age,sex)print( Info
Note that you need to convert the type at%d
3. Use. Format ().
Name=input ("pleaseinput your name:")The Age =int (input ("please input Your age:")) Sex=input ("pleaseinput your sex:") Info ='name:{_name} \nage:{_age} \nsex:{_sex}'. Format (_name=name,_age=age, _sex=Sex)print(info)
You can also specify no parameters. Use {0}{1}{2} and so on.
Name=input ("pleaseinput your name:")The Age =int (input ("please input Your age:")) Sex=input ("pleaseinput your sex:") Info ='name:{0} \nage:{1} \nsex:{2}'. Format (name,age,sex) Print(info)
Note that these three kinds of + numbers are the most inefficient.
Five. Looping statements in Python
Python has a while loop with a for loop, which works slightly differently from C + +. Python can be followed directly with a judgment statement, unlike C + + needs parentheses. While can also be combined with else. form for
While judging condition: EXECUTE statement ...
Else
Count = 3 while count! = 0 :print("hello", Count ) Count=count-1else: print("exit" )
The For statement is similar to the use of the new C + + feature range for. The syntax format for the For loop is as follows:
For in sequence: statements(s)
for inch ' Python ': print' current letters:', letter
Or
for in range: if i <10 print("hello", i)
How to use the range () function in Python
# represented from 1 to 5 (not including 5) [1, 2, 3, 4] # represents from 1 to 5, Interval 2 (not including 5) [1, 3] # represented from 0 to 5 (not including 5) [0, 1, 2, 3, 4]
Six. Continue and break
Continue and break are used as in C + +.
Python Learning Note 01