Python learning Day05

Formatted output

A placeholder

In the case of the program there is often need to use the fixed format allows the user to enter into a fixed format and print

Books such as requiring a user name and age, as well as height, printed in the following formatMy name is xxx,My age is xxx, My height is xxx

Obviously string comma-splicing, but this also requires the converted digital, very significant overall code redundancy.

Man of few words said, let us look at it that way said above:

name = 'jiangjiahui'
age = 18
print('My name is',name)
print('My name is age',age)

But it would seem very troublesome, so this time there will be a placeholder

name = 'jiangjiahui'
age = 18
print('My name is %s My age is'%(name,age))

However, when a placeholder, there is a problem% d (for all data types),% s (for all types)

Two, format format

The above-described manner placeholder when there are many parameters to clutter, so there is the following f-string format.

name = 'jiangjiahui'
age = 18
print("Hello, {}. You are {}.".format(name, age))
Print the results:. Hello, jiangjiahui You are 18.

Three, f-string formatted

Compared placeholder manner, python3.6 version adds f-string formatted manner, relatively simple to understand

name = 'jiangjiahui'
age = 18
print(f'My name is:{name} My age is:{age}.')
Print result: My name is: jiangjiahui My age is: 18
salary = 6.6666
print(f'{salary:.2f}')

6.67

Process to determine if

First, the definition:

Sims make a judgment, what if you do, and if so what do

1.1 if

Our computer simulations allow us to work, for example, today you have to go out, and the weather forecast says it might rain today, then out the door and you will judge whether or not to bring an umbrella

if 条件:
    代码1
    代码2             #代码123组合起来就是一个代码块
    代码三
    ....
1.2 if....else
if 条件:
    代码块
else :              #if...else表示代码成立会做什么,else表示不成立会做什么
    代码块
1.3 if...elif...else

In the actual process, we may encounter more than two or more cases so this time we can use if ... elif ... else statement

if 条件1:
    代码块1
elif 条件2:
    代码块2
else :
    代码块三

Two .if nesting

If it rains how to do, how to do if it does not rain, if walking in the road on the way how to do it?

This time we need to judge a variety of situations

#if嵌套的方式
if 条件表达式1:
    if 条件表达式2:
        代码块1
    else:
        代码块2
else :
    代码块三             #当然else里面也可以嵌套if语句

Guess you like

Origin www.cnblogs.com/ledgua/p/11278503.html