Python Learning Note 01

Source: Internet
Author: User

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

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.