current position:Home>Daily python, Part 8 - if statement
Daily python, Part 8 - if statement
2022-01-31 05:32:52 【Qing'an slag】
「 This is my participation 11 The fourth of the yuegengwen challenge 8 God , Check out the activity details :2021 One last more challenge 」.
This is Qing'an , Welcome to this chapter .
if sentence
if Keyword or conditional judgment :
Conditions ( That's the expression ):
Conditions can also be output statements
name = ' Qing'an '
if name == ' Qing'an ':
print(' It's Qing'an ')
# As a result, :
# It's Qing'an
Copy code
else:
name = ' Qingan has nothing else to do '
if name == ' Qingan has nothing else to do ':
print(' It's Qing'an ')
else:
print(' Not Qing'an ')
# As a result, :
# It's Qing'an
Copy code
Look at this interesting example :
# Create a list of animals
animal = ['cat','dog','duck','fish']
# Use for Loop through the list , Define a new variable for the loop animal
for animals in animal:
# Use if Make a judgment , Judge whether the newly defined variable has been traversed , And judge whether there is... In the list cat This string
if animals == 'cat':
# If there is , Print in full character uppercase
print(animals.upper())
else: # otherwise
# Print first character uppercase
print(animals.title())
# CAT
# Dog
# Duck
# Fish
Copy code
Here we see a new knowledge , Is the comparison symbol ==, This is a comparison operator to determine whether it is equal , I will slowly tell my friends here , And then there's != The judgment of inequality , For numbers, there are more than >、 Less than <、 be equal to =、 Wait for such a judgment .
Next, let's look at inequality !=
request_name = 'dog'
if request_name != 'cat':
print(' The comparison results are not equal ')
Copy code
ad locum , We define a variable as 'dog' String , And then we use if Sentence to judge , Determine when it is equal to the string 'cat', If not equal to , Print a result to tell us . Here we can also judge , The final conclusion of this procedure is inequality .
Read the above , Are you absolutely still crazy , So let's talk about it in detail ! Along with the comparison symbols , Study hard , Take a good look at , practice . If you don't understand, you can understand it twice !!!
age = 1
if age > 19:
print('Yes')
else:
print('No')
Copy code
explain : Here we define an age variable age, use if-else Make a judgment on it , Give Way age On 19 Compare , If age This number is greater than 19, So we output yes Otherwise output no,pyhton The backstage will make a judgment , Then give us the results we want , If you don't add print Print , The program will report an error !
elif
Next we learn if-elif-else sentence :
Take a very easy topic , Define an age age = ?, If you are older than 18, you will be charged 20 element , Over ten and under eighteen 10 element , Less than ten years old free !
Let's look at the program :
age = 13
if age < 10:
print(' Less than ten years old , free ')
elif age < 18:
print(' Over ten but under eighteen , charge 10 element ')
else:
print(' Older than eighteen , charge 20 element ')
Copy code
explain : Here we don't repeat to judge whether we are more than ten years old and less than eighteen years old , Here you can 10<age<18 It's OK, too , We use the elif I made a judgment before , So there is no need to judge again , Writing like this also brings us convenience . Because of the latter two conditions, we have met , So in the tail else Just print directly there ,else Is that what it means , There's no need to sit on the condition judgment again .
We can also improve the program , That's it Omit else Code , Only elif, And you can also use multiple elif
age = 65
if age < 10:
print(' free ')
elif age <= 18:
print(' charge 10 element ')
elif age <= 65:
print(' charge 20 element ')
elif age > 65:
print(' free ')
Copy code
explain : Here is also the last example , An additional judgment condition is added, that is <=
actual combat
animal = ['cat', 'dog', 'duck', 'fish', 'eagle', 'tortoise']
new_animal = ['cat', 'fish', 'duck']
for new_animals in new_animal:
if new_animals in animal:
print(f"{new_animals}: Pets and ")
else:
print(f" sorry ,{new_animals}: There are no pets ")
# cat: Pets and
# fish: Pets and
# duck: Pets and
Copy code
explain : Here we see a new content is in, This is to judge whether it is included , Whether the animals the user wants to buy are included in the pet store , use for Loop through the pets required by the user , Reuse if Make a judgment , If the user needs a pet , The pet shop has , Then print a result , otherwise , Print , sorry , These pets you need are gone .
copyright notice
author[Qing'an slag],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310532507934.html
The sidebar is recommended
- My friend's stock suffered a terrible loss. When I was angry, I crawled the latest data of securities with Python
- Python interface automation testing framework -- if you want to do well, you must first sharpen its tools
- Python multi thread crawling weather website pictures and saving
- How to convert pandas data to excel file
- Python series tutorials 122
- Python Complete Guide - printing data using pyspark
- Python Complete Guide -- tuple conversion array
- Stroke the list in python (top)
- Analysis of Python requests module
- Comments and variables in Python
guess what you like
-
New statement match, the new version of Python is finally going to introduce switch case?
-
Fanwai 6 Different operations for image details in Python opencv
-
Python crawler native code learning (I)
-
Python quantitative data warehouse building series 2: Python operation database
-
Python code reading (Part 50): taking elements from list intervals
-
Pyechart + pandas made word cloud pictures of 19 report documents
-
[Python crawler] multithreaded daemon & join() blocking
-
Python crawls cat pictures in batches to realize thousand image imaging
-
Van * Python | simple crawling of a planet
-
Input and output of Python practice
Random recommended
- Django ORM details - fields, attributes, operations
- Python web crawler - crawling cloud music review (3)
- Stroke list in python (bottom)
- What cat is the most popular? Python crawls the whole network of cat pictures. Which one is your favorite
- [algorithm learning] LCP 06 Take coins (Java / C / C + + / Python / go / trust)
- Python shows the progress of downloading files from requests
- Solve the problem that Django celery beat prompts that the database is incorrectly configured and does not support multiple databases
- Bamboolib: this will be one of the most useful Python libraries you've ever seen
- Python quantitative data warehouse construction 3: data drop library code encapsulation
- The source code and implementation of Django CSRF Middleware
- Python hashlib module
- The cover of Python 3 web crawler development (Second Edition) has been determined!
- The introduction method of executing Python source code or Python source code file (novice, please enter the old bird and turn left)
- [Python basics] explain Python basic functions in detail, including teaching and learning
- Python web crawler - crawling cloud music review (4)
- The first step of scientific research: create Python virtual environment on Linux server
- Writing nmap scanning tool in Python -- multithreaded version
- leetcode 2057. Smallest Index With Equal Value(python)
- Bamboolib: this will be one of the most useful Python libraries you've ever seen
- Python crawler actual combat, requests module, python realizes capturing a video barrage
- [algorithm learning] 1108 IP address invalidation (Java / C / C + + / Python / go / trust)
- Test platform series (71) Python timed task scheme
- Java AES / ECB / pkcs5padding encryption conversion Python 3
- Loguru: the ultimate Python log solution
- Blurring and anonymizing faces using OpenCV and python
- How fast Python sync and async execute
- Python interface automation test framework (basic) -- common data types list & set ()
- Python crawler actual combat, requests module, python realizes capturing video barrage comments of station B
- Python: several implementation methods of multi process
- Sword finger offer II 054 Sum of all values greater than or equal to nodes | 538 | 1038 (Java / C / C + + / Python / go / trust)
- How IOS developers learn python programming 3-operator 2
- How IOS developers learn python programming 2-operator 1
- [Python applet] 8 lines of code to realize file de duplication
- Python uses the pynvml tool to obtain the working status of GPU
- Data mining: Python actual combat multi factor analysis
- Manually compile opencv on MacOS and Linux and add it to Python / C + + / Java as a dependency
- Use Python VTK to batch read 2D slices and display 3D models
- Complete image cutting using Python version VTK
- Python interface automation test framework (basic) -- common data types Dict
- Python specific text extraction in actual combat challenges the first step of efficient office