Revision- Python


Good Morning boys

Today we will take a review of FOR loop and build on how to find SUM and PRODUCT using FOR loop

-------------------------------------------------------------------------------------------------------------------
The Google meet link fro today's class is:

-----------------------------------------------------------------------------------------------------------------
By the end of this session you will be able to :
  • understand how the FOR loop works to create natural numbers
  • Also understand how the FOR loop works to build up the sum of natural numbers. (Accumulate values)
  • Acquire the skill to use FOR loop to find the PRODUCT of natural numbers.
--------------------------------------------------------------------------------------------------------------------
PYTHON PROGRAMING

Review of IF , FOR and WHILE


Q3. Write a program to print the first 10 natural numbers

To do the coding for this question, we have to note that FOR loop will need 

  • a variable to create the natural numbers.
  • This variable will begin with the value 1
  • This variable will end with the value 10  
So, the program will be:
print("The first 10 Natural Numbers are:-")
for num in range(1,11):
      print(num)


Now, Let us look at the following question:

Q4. Write a program in Python to print the sum of first 10 natural numbers.

To do the coding for this question, we will need 2 variables-

  • The first variable (num) will handle the creation of the natural numbers(1,2,3,4......10)have to note that FOR loop will need 
  • The second variable (sum) will be needed to accumulate the sum.

  • The variable num is used to create the natural numbers.
    • This variable will begin with the value 1
    • This variable will end with the value 10  
  • The variable sum is used to keep adding the numbers created so that by the end of the loop, we will have 1+2+3+4........+10 stored in it.
So, the coding for the above question will be:

sum=0     # variable to hold the sum, initial value is 0
print("The sum of first 10 Natural Numbers are:-")
for num in range(1,11):        # for loop controls the creation of natural numbers from 1 till 10 
          sum+=num                 # new sum= old sum+num
print("Sum of the first 10 natural numbers is-",sum)      # display the result


Now, Please try the coding for the following question:

Q5  Write a program in Python to print the  product of first 10 natural numbers

--------------------------------------------------------------------------------------------------------------------
EXERCISE
Q1. Write a program in Python to accept the cost  and the number of items purchased. Find out the amount. If the amount is >5000, offer a discount of Rs 500 on the bill and display the amount.



Q2. Delhi University Hostel allows men and women to use the hostel facility .Men have to pay Rs. 300/- per day while women have to pay Rs 200/- . Accept the gender of the student, and the number of days. Write a Python program to find the total rent to be paid.

Comments

Post a Comment

Popular posts from this blog

HOLIDAY HW