python-base 2-variables, input and output, conditional statements

Source: Internet
Author: User

Used to store things

#定义变量
Name=' Feifei '#定义变量, string
Age=18.9#整型或者小数定义变量不需要加引号
Print (name)
Print (age)
Name2= "What ' s your name"#里面有单引号外面就用双引号
Name3= ' You look very handsome'#里面是双引号外面就用单引号
Name4=""you look very handsome "#里面既有双引号又有单引号就用三引号标记
#ctrl +/Quick Notes
#3个单引号还能进行段落的注释

Rules for defining variables:

Variable name to see the name to know, can not write, blind write to the back of their own can not understand what the variable is dry, do not use pinyin, so very low, not to use Chinese as a variable name, it is 2b programmers do, but Python can be used in Chinese as the variable name!

Variable names can only be any combination of letters, numbers, or underscores
The first character of a variable name cannot be a number

3. Input, Output

How does python receive user input, use the input function, use Raw_input in Python2, receive a string, output, the first program has been written using print, code into the following:

1

2

Name=input (' Please enter your name: ') #把接收到的值赋给name变量

Print (name) #输出接收到的输入

Input at the time of receiving inputs, you can see the value you entered, if you enter a password like this, do not want to let others see your password, how to do it, you need to use a standard library, Getpass, what is the standard library, is not need you to install, loaded Python on the library, is the standard library, Getpass is a standard library, import in, directly using the Getpass.getpass method can be entered at the time, do not echo, the code is as follows:

1

2

3

Import Getpass #导入getpass模块

Password = getpass.getpass (' Please enter your password: ') #接收输入的密码

Print (password)

#输入输出
# name=input (' Please enter your name: ') #输入用户名
# print (name)
#输入密码, the password is not visible
#导入一个模块
passwd=getpass.getpass ("Please enter your password")
Print (passwd)#注意: Getpass cannot be executed in Pytcharm

4. Conditional judgment

The conditional judgment in Python uses if else to judge, multi-branch words using if elif ... else, that is, if how to do what, otherwise, how to do so, the format is as follows:

1

2

3

4

If it rains tomorrow:

Then bring the umbrella tomorrow.

Else

Not with an umbrella tomorrow.

Python is indented to represent the code block, so it looks like the code is very organized, such as the above example, if it rains tomorrow, then tomorrow will bring an umbrella, tomorrow with an umbrella is equivalent to the son of if, so you can also think that there is a parent-child relationship, write an example of the actual point, as follows:

1

2

3

4

5

6

7

8

9

score = Int (input (' Enter your score: ')) #接收输入, because input receives a string, you need to force the type into an integer type with the INT function

If score==100: #如果成绩等于100分的话

Print (' Little genius, you are full score ')

Elif score >=90 and score <: #如果成绩大于等于90分小于100分的话

Print (' brother, your score is not low ah, good ')

Elif score > Score <90: #如果成绩大于60分小于90分的话

Print (' brother, this exam is so sloppy. ')

else: #如果分数小于60分的话

Print (' brother, what are you doing, the effort ')

If sex== ' male ' or sex== ' female '

Satisfies one is or, all satisfies is and

Condition Judgment:

= = equals

! = Not equal to

> Greater than

< less than

>= greater than or equal to

<= less than or equal to

Print (1//2) Auto-rounding, no 45 way to erase the decimals directly

Non-0 is true, non-null is True

python-base 2-variables, input and output, conditional statements

Related Article

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.