Python If condition judgment, datetime

Source: Internet
Author: User

1. If simple judgment

If condition:

Statement block

Else

Statement block

If 1>2:

Print (' fake ')

Else

Print (' true ')

2. Multiple if elif

If condition 1:

Statement block

Elif Condition 2:

Statement block

Else

Statement block

If a==2:

Print (' a=2 ')

Elif a==3:

Print (' A=3 ')

Else

Print (' a=0 ')

3, and and

score=input (' Please enter your score: ')
Score=int (score) #强制类型转换
if score<=100 and score>=90:
print (' excellent ')
elif score<90 and score>=80:
print (' good ')
elif score<80 and score>=60:
print (' Pass ')
elif score<60 and score>=0:
print (' fail ')
Else:
print (' Please enter the correct number ')

4, or OR

Sex=input (' Please enter your gender: ')
If sex== ' male ' or sex== ' female ':
Print (' Sex legal ')
Else
Print (' Sex not legal ')

5. DateTime

Import the DateTime module using import first

   Import datetime

Set a user name (used with datetime concatenation)

user= ' xxx '

class that represents the date and time

Today=datetime.datetime.today ()

Convert datetime to String type

TODAY=STR (today)

Welcome XX login, today's date is xxx example:

notation 1: msg= ' welcome ' +user+ ' login ' + ' Today's date is ' +today (The way to stitch strings)

Print (msg)

notation 2: msg= ' Welcome%s login, today's date is%s '% (user,today)

Print (msg)

%s placeholder preceded by several%s after parentheses write several variables
%s represents a string type (others are also available) %d integers%f decimals retain several decimals, plus%.2f in front of F
Retain two decimal places you can also use round (score,2) to retain decimals after several (rounding 0)
\ nthe line break

Python If condition judgment, datetime

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.