Python Programing Lab EXERCISE - 3(d)

Aim: 
             Write a program using a while loop that asks the user for a number, and prints a countdown from that number to zero.

Description: 
                       A loop statement allows us to execute a statement or group of statements multiple times. Here, We are using while loop.
while loop statement: It repeatedly executes a target statement as long as the given condition is true. The general form while statement is as follows: 

                                           while expression: 
                                            statements(s) 

                Here, the statement(s) may be a single a statement or a block of statements. The condition may be any expression, and is true for any non-zero value. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line immediately following the loop.

Algorithm:

Input: A Number

Output: Print the values from given number to zero 
Step1: Start 
Step2: Read num 
Step3: Repeat Steps 4 and 5 while num>=0 
Step4: Display num 
Step5: Decrement num by 1 
Step6: Stop

Program:

num=int(input("Enter number:"))
print("Countdown Starts:")
while(num>=0):
      print(num)
      num=num-1

Output:


Conclusion: I gets the knowledge on conditional statements and looping statements. 

netaji gandi Sunday, June 30, 2019
Python Programing Lab EXERCISE - 3(c)

Aim: 
            Write a program using a for loop that loops over a sequence. What is sequence?

Description: 


A Sequence is the generic form for an ordered set. There are several types of sequences in Python. The following 3 are most important.
Lists: There are the most versatile sequence type. The elements of a list can be any object and lists are mutable.

Tuples: These are like lists, But Tuples are immutable.

Strings: These are a special type of sequence that can store only characters and having special notations.

Algorithm:

Output: Elements of sequence(List). 

Step1: Start 
Step2: Initialize the days=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
Step3: Repeat Step4 until the last element in the list is reached 
Step4: Print ith element 
Step5: Stop

Program:


days=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
print("Days of the week:")
for day in days:
      print(day)

Output:

Days of the week:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday



netaji gandi
Python Programing Lab EXERCISE - 3(b)


Aim: 

          Using a for loop, write a program that prints out the decimal equivalents of 1/2, 1/3, 1/4, . 1/10.

Description: 
         A loop statement allows us to execute a statement or group of statements multiple times. We can use for loop to calculate the decimal equivalents of given set of numbers.
for loop statement: 
It has the ability to iterate over the items of any sequence, such as a list or a string. Iterating over a sequence is called Traversal. The general form of for loop statement is as follows: 
                       
                              for iterating_var in sequence: 
                                    Statement(s) 
     
          To find the decimal equivalents, we need to use the method pow(). This method is not accessible directly, so we need to import math module and then we need to call this method using math static object.

Algorithm:

Output: Decimal equivalents of 1/2, 1/3, .. ,1/10 

Step1: Start 
Step2: Import math module 
Step3: Initialize i = 2 
Step4: Repeat Steps 5,6 until i = 11 
Step5: Print math.pow(i,-1) 
Step6: Increment i by 1 
Step7: Stop

Program:


print("Decimal Equivalents of:")

for i in range(2,11):
      dec_eq=1/i
      print("1/"+str(i)+"= ",dec_eq)

Output:


netaji gandi
Python Programing Lab EXERCISE - 3(a)

Aim: 

                Write a Program for checking whether the given number is a even number or not.
Description: 
                            If a number is exactly divisible by 2(Remainder is 0) then it is said to be an Even number otherwise, the number is said to be an Odd number. In Python, We use modulus(%) operator to find the remainder. 

                            Decision making is used to specify the order in which the statements are executed. We can use if…else statement to check whether the number is even or odd.
if…else statement: 
                                   This is a two-way decision making statement that decides what to do when the condition is true and what to do when the condition is false. The general form of if…else statement is as followes: 

                                   if condition: 
                                           intended statement block for true condition 
                                    else: 
                                           intended statement block for false condition

Algorithm:

Input: A Number


Output: A Message 


Step1: Start 

Step2: Read num 

Step3: Check whether the num is divisible by 2 or not. If yes, goto Step4. else, goto Step5 

Step4: Display “The number is even” and goto step6 

Step5: Display “The number is odd" 

Step6: Stop

Program:

n=int(input("Enter a number---->"))
if n % 2 == 0:
          print ("EVEN Number");
else:

          print ("ODD Number");

Output:



netaji gandi
Python Programing Lab EXERCISE - 2(b)

Aim: 

             Write a program add.py that takes 2 numbers as command line arguments and prints its sum.
Description: 

                        Command line arguments are the arguments that are passed to the program when the program is invoked for execution. Python provides a getopt module that helps us to pass command line arguments and options. The Python sys module provides access to any command line arguments via sys.argv. This serves two purposes: 1. sys.argv is the list of command line arguments. 2.len(sys.argv) is the number of command line arguments. The first argument is always script name and it is also being counted in number of arguments. As the command line arguments are strings, here we need to type-cast those arguments to the suitable type.

Algorithm:


Input: Two numbers from command line


Output: Sum of two numbers 


Step1: Start 

Step2: Import sys module 

Step3: Read the arguments using commandline and store the values in a and b 

Step4: Type cast a to integer and b to integer then calculate the addition of a and b and store the result in sum

Step5: Print sum 

Step6: Stop

Program:

step 1: open notepad and write the below program
import sys;
n1=int(sys.argv[1]);
n2=int(sys.argv[2]);
print (n1+n2)
step 2: SAVE
step 3: Open DOS SHELL and go to file saved location (D:\)
step 4: python filename.py argument1 argument2

Output:

Conclusion:
                  gets the knowledge on math functions and how the values are passed from the command line.

netaji gandi

NPTEL Programming in Java Jan 2024 Week 11

  Week 11 : Programming Assignment 1 Due on 2024-04-11, 23:59 IST The following code is missing some information needed to run the code. Add...