Python Programming tutorial 11.

 AND / OR operators

In our previous lesson, we created a program to calculate the grades and output the results of a student. We used the nested if structure to solve our problem. And now we are going to learn two new operators, the AND or OR operators, and solve this problem in a different way. What those operators do is test multiple conditions at the same time. So watch the alternate solution to the nested if structures; however, with the 'and' operator, the two conditions/test must be true in order to know if the student was approved. Let's visualize this:

NB: refer to our previous tutorial on conditionals.

Instead of this nested if structure:





we can test these two conditions at the same time by using the 'and' operator and check if the attendance rate is greater than 80 percent.




The print statement is going to run if both of the conditions before and after the and operator are true.

After that, we can start an 'elif' statement and test the opposite condition:





In this case we know the student has failed for both reasons. Let's do another elif statement:






If we enter here, we know that the student has failed due to an average grade lower than 6. If he had failed for both reasons, the code would have ended at the first elif statement. As a result, he hasn't been approved and he hasn't failed for both reasons. Also the average grade is greater than 0.8, so he failed because of his grade. 

Now we need an else statement:







We know if he didn't fail because of his grade then he probably failed because of his lower attendance rate. This is a different way of solving the same problem so let's test if this is working.








And he has been approved. Let's try some more:







Now he has failed for both reasons.













And this is how the 'and' operator works. The ' or ' operator works similar. The difference is that when we use 'or' only one condition has to be true for the test to return true:







So if the highted returns true and the attendance rate returns false, this is okay. Only one has to be true for it to run. That is how we use the 'OR' statement or operator. This is not going to be useful to use the or operator, but we are going to ge the opportunity to use it more later in the course. 

This was all for today's lesson, I will see you in the next tutorial.  


Summary of code:

grade1 = float ( input("Type the grade of the first test: ") )    
grade2 = float ( input("Type the grade of the second test: ") )
absences = int ( input("Type the number of absences: ") )
total_classes = int ( input("Type the the total number of classes: ") )

avg_grade = (grade1 + grade2) / 2
attendance = (total_classes - absences) / total_classes

print("Average grade: ", round(avg_grade,2) )
print("Attendance rate: ", str(round((attendance * 100),2))+'%' )

if (avg_grade >= 6 or attendance >= 0.8):
        print("The student has been approved.")
elif(avg_grade < 6 and attendance < 0.8):
    print("The student has failed due to an average grade less than 6.0 and an attendance rate less than 80%.")
elif(attendance >= 0.8):
     print("The student has failed due to an average grade less than 6.0.")
else:
    print("The student has failed due to an attendance rate less than 80%")
     
        

These are the lines of code use for the lesson, see you next time.



Python Programming Tutorial 10, Conditionals.


Conditionals(if, elif, else)

In this lesson, we will continue talking about conditionals. We already wrote a simple conditional structure in the previous lesson; now we are going to make it more complex. We learned that the 'if ' structure is a logical structure. It evaluates boolean values to decide whether to execute blocks of code or not.

We are going to create a program to evaluate the grades of a student and see how we can test multiple conditions using 'nested if structures'.

Firstly, we would ask for grades to test made by a student, so grade 1 and grade 2. Then ask for the number of absences, which is an integer value, and the total number of classes. Now that we have the inputs, we can calculate the average grades. using Python code as seen below.

These are our parameters we are going to evaluate with conditionals:





 

 

Let's calculate:









Rules:

- Grades are 0 to 10

- Students need an average grade of 6 or higher to get approval

- Students need an attendance rate of 80% or higher to get approval

There will always be multiple solutions for the same problem. Let's start with one solution that is going to use ''nested 'if' blocks'' to solve the problem. Let's start by using our conditionals.

Let's start by checking if the average grade is greater than or equal to 6. If this is true, we don't know if the student was approved yet. We still need to check if he had more than 80 percent of the attendance rate, so inside the 'if'' structure, we are going to open another 'if structure'. If the code runs from here, if (avg_grade >= 6), to here, if (attendance >= 0.8), it means that the average grade is higher than 6 and the attendance rate is higher than 0.8 or 80 percent, and that means the student was approved. Otherwise, we must run an 'else statement' that he was not approved or has failed.

After all, we still need another 'else statement' to be a part of the 'if' structure. In this case, the student has also failed. We can still do some improvements, but first let's test if this is working!

See below:
















Let's run the code to see if it's working:











Now let's try grade 7 and 2 absences in 20 classes:










In this case the student has been approved, which is correct. 

Let's try it again: 









This time the student has failed because the average grade 5 was less than 6.

Let's try one last time:








The student has failed because the attendance rate was lower than 80 percent. 

We just solve our problem using nested if structures that are if structures inside if structures. It would be a bit better if we knew the reason why the student has failed, right? So let's complete this program. In case we are in this yellow highlighted else statement, 


 








It means that the average grade is higher than 6, but the attendance rate is lower than 80 percent, or 0.8. However, if we get to the last else statement or the code runs to the last statement, it means the average grade was less than 6 in due to the grade, but we don't know about the attendance, so let's write another line of code. In this case, we will start the elif statement. If we get to this elif statement, it means this: if (avg_grade >= 6) has failed. So we will test attendance again, and if the attendance is greater than 0.8, then we know the student failed due to a grade lower than 6. If we get to the else statement, it means the grade is not lower than 6 and the attendance rate is not greater than 0.8, and this means the student failed due to lower grade and lower attendance.

So let's complete the statement, having that in mind:

 









Now before we test this, let's just print the average grade and the attendance rate before giving the result. We can round the average grade to 2 decimal places and multiply the attendance rate by 100 because it is in decimal form. So we will multiply by 100, round it, and convert it to a string so we can concatenate (+) with a percent sign (%). 

Now let's see if this is working:










And our code is beautifully working. Let's try again:









And it seems to be working fine. We should always test all our conditions.








It's working, so let's check the last case which is for both reasons:







Now we have a working example. There are still some problems with our program but I will talk about them and teach you how to fix them later in this section.

In the next lesson we will learn how to use use less lines of code for the same program by using the 'AND' or 'OR' operator.

I'll see you then.









20 popular things created with Python programming:

Python’s versatility allows it to be used for everything from web development to machine learning and scientific computing.

1. Instagram: The backend of this social media platform uses Python.

2. Spotify: Python helps with backend services and data analysis.

3. Dropbox: Developed its desktop client and services using Python.

4. YouTube: Uses Python for various functionalities, including video processing.

5. Google Search: Python is part of Google’s internal systems and tools.

6. Reddit: The original version of Reddit was written in Python.

7. Pinterest: Uses Python for image-based search functionality.

8. Quora: The Q&A platform was built with Python.

9. NASA: Uses Python for data analysis and scientific computing.

10. Facebook: Python is used in its infrastructure management.

11. Netflix: Python powers data analytics and recommendations.

12. Uber: Uses Python for geospatial analysis and management.

13. Dropbox: Desktop applications are built using Python.

14. BitTorrent: The file-sharing protocol was originally implemented in Python.

15. OpenStack: Cloud computing software that heavily uses Python.

16. PyTorch: A machine learning library built with Python.

17. TensorFlow: While it supports multiple languages, its main API is Python.

18. Ansible: An automation tool written in Python.

19. Blender: A 3D creation suite that uses Python for scripting.

20. Flask/Django: Two of the most popular Python web frameworks for building websites and APIs.



Python Programming Tutorial 15

 Error Handling Anytime we work with user input, we should make sure not only the data is valid and within the range we want, like what we d...