[go: up one dir, main page]

0% found this document useful (0 votes)
21 views20 pages

Wa0009.

Uploaded by

riddle2023.yt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views20 pages

Wa0009.

Uploaded by

riddle2023.yt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

CST 362 PROGRAMMING IN PYTHON

Model Questions.

1) List out the rules for declaring a variable in python program.


● there is no variable declaration in python
● can just initialize values to variable directly without
specifying the datatype
● keywords should not be used as variable name
● must begin with alphabet or _
● can contain any number of characters or digits or _s
● no other special symbols
● case sensitive
● <variable_name>=<expression>

2) What is keyword? Give examples.


● reserved words which define the language’s syntax
rules and structure
● cannot be used as variable names
● special words to provide special meaning
some are

3) What is Floor division in python


● also sometimes known as integer division
● the // operator
● divide the first argument by the second and round the
result down to the nearest whole number
● equivalent to the math. floor() function
4) Write the Syntax for if-elif statements.

5) Draw the flowchart for the following loops


For loop, While loop

Conditio False
n

Tru

Statements in
body of the loop
6) Develop a python program to find the biggest of three numbers

7) List out various symbols used in flowchart

8) Write the answers of the following in reference to given string


str=”welcome to my blog”
a)print (str[3:18]
b)print(str[2:14:2])
c)print(str[8:-1:1])
9) Strings are immutable, Explain with one example
● a string value cannot be updated
# Can not reassign
t= "Tutorialspoint"
print type(t)
t[0] = "M"
● will show error
10) Explain any five python string handling function with
example.
11) Explain any 3 escape sequences in python with examples.
sequence of characters that does not represent itself but is
converted into another character or series of characters
a character is preceded by a backslash (\)
12) What is logical operators in python.? Show in example.

13) Write any 4 bitwise operators with examples.


14) Differentiate python membership operators and identity
operators.
● Membership operators are operators used to validate the
membership of a value. It test for membership in a sequence,
such as strings, lists, or tuples.
● in operator : The ‘in’ operator is used to check if a value
exists in a sequence or not
● ‘not in’ operator- Evaluates to true if it does not finds a
variable in the specified sequence and false otherwise.
● identity operators used to determine whether a value is of a
certain class or type
● ‘is’ operator – Evaluates to true if the variables on either
side of the operator point to the same object and false
otherwise
● ‘is not’ operator – Evaluates to false if the variables on
either side of the operator point to the same object and true
otherwise
15) What is the general syntax of range() function.
range(start, stop, step)

start Optional. An integer number specifying at which position to start.


Default is 0

stop Required. An integer number specifying at which position to stop


(not included).

step Optional. An integer number specifying the incrementation. Default


is 1

16) Differentiate break and continue statement with example.


used to break out a loop
i = 1
while i < 9:
print(i)
if i == 3:
break
i += 1
The continue keyword is used to end the current iteration and continues to
the next iteration
i = 0
while i < 9:
i += 1
if i == 3:
continue
print(i)
17) What is pass statements in python.
● used as a placeholder for future code
● When the pass statement is executed, nothing happens,
but you avoid getting an error when empty code is not
allowed
● Empty code is not allowed in loops, function
definitions, class definitions, or in if statements
18) Write a nested for loop program to print multiplication table
in Python

19) Write a program to check prime number or not.


n=int(input(“Enter the number to check : “))
prime_flag = 0

if(n > 1):


for i in range(2, n//2):
if (n % i == 0):
prime_flag = 1
break
if (prime_flag == 0):
print("true")
else:
print("false")
else:
print("false")

20) Write a Python program to reverse a given number


num = 1234
reversed_num = 0

while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10

print("Reversed Number: " + str(reversed_num))

21) Write a python program to find factorial of a number.

num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or


zero
if num < 0:
print("Sorry, factorial does not exist for
negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

22) Write a program to check Armstrong number.


num = int(input("Enter a number: "))

# initialize sum
sum = 0

# find the sum of the cube of each digit


temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10

# display the result


if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

23) Write a program to print Fibonacci sequence up to nth term.

nterms = int(input("How many terms? "))

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1

24) Explain any five string handling function with example.


25) Explain any 3 escape sequences in python with examples.

26) Write a Python program to reverse a number and also find the
sum of digits of the number. Prompt the user for input.

27) Illustrate escape sequences with examples.


28) Explain Various decision making statements in python.
if, if…else, if…elif
29) Explain for loop with else case with an example.
In most of the programming languages (C/C++, Java, etc), the use of
else statement has been restricted with the if conditional statements. But
Python also allows us to use the else condition with for loops. The else block
just after for/while is executed only when the loop is NOT terminated by a
break statement

for i in range(1, 4):


print(i)
else: # Executed because no break in for
print("No Break")

print("with break")
for i in range(1, 4):
print(i)
break
else: # Not executed as there is a break
print("No Break")
output
1
2
3
No Break
with break
1

30) What is the purpose of break statement.?


used to bring the control out of the loop when some external
condition is triggered. break statement is put inside the loop body
(generally after if condition)

31) Differentiate while loop and while loop with else case with
examples.

In Python, the while statement may have an optional else clause:


while condition:
# code block to run
else:
# else clause code block
Code language: PHP (php)
In this syntax, the condition is checked at the beginning of each
iteration. The code block inside the while statement will execute as long
as the condition is True.
When the condition becomes False and the loop runs normally, the else
clause will execute. However, if the loop is terminated prematurely by
either a break or return statement, the else clause won’t execute at all.

32) What is the purpose of continue statement?


used to end the current iteration in a for loop (or a while loop), and
continues to the next iteration.
i = 0
while i < 9:
i += 1
if i == 3:
continue
print(i)

output
1
2
4
5
6
7
8
9

33) Explain different types of loops with examples.


for, while, for with else, while with else
34) Write a python program to construct the following pattern, using a
nested for loop.
*
**
***
n=int(input("Enter the number of lines : "))
for i in range(0,n):
for j in range(0,i+1):
print("* ",end="")
print()

35) Write a program to count the number of vowels.


count = 0
str=input("Enter the string :")
# Creating a set of vowels
vowel = set("aeiouAEIOU")
# Loop to traverse the alphabet
# in the given string
for alphabet in str:
# If alphabet is present
# in set vowel
if alphabet in vowel:
count = count + 1
print("No. of vowels :", count)
36)Briefly describe data types in Python.

41)How will you create a dictionary in python.


dictionary can be created by placing a sequence of elements within
curly {} braces, separated by ‘comma’. Dictionary holds pairs of values,
one being the Key and the other corresponding pair element being its
Key:value. Values in a dictionary can be of any data type and can be
duplicated, whereas keys can’t be repeated and must be immutable.
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)

42)What are the uses of //,**,*= operators in python.


// - integer division
**- power function
*= multiply and assign
43)What is string in Python? Why is len function used in string.?
sequence of characters - immutable - len to find length of string
44) What is the Slice operator ? Explain with example.
:
str[:]
str[:6]
slice(start, end, step)
45)What will be the output of the given code
str1=”hello world”
str2=”welcome to programming”
print (str1[4])
print(str1*5)
print(str1[6:])
print(str1[5:10])

46) What is list ? Explain with an example.


● used to store multiple items in a single variable
● thislist = ["apple", "banana", "cherry"]
● print(thislist)
47) What is tuple in pythons ? Compare tuple and list.
A tuple is a collection which is ordered and unchangeable.

Tuples are written with round brackets.

thistuple = ("apple", "banana", "cherry")


print(thistuple)

48)What are the built-in tuple functions in Python.


50)What is dictionary ? Explain with example.

51)Explain the concept of scope and lifetime of variables in


Python programming language, with a suitable example.

52)Write a Python code to check whether a given year is a leap


year or not [An year is a leap year if it’s divisible by 4 but
not divisible by 100 except for those divisible by 400].

53)What are the possible errors in a Python program. Write a


Python program to print the value of 22n+n+5 for n provided
by the user.

54)Write a Python code to determine whether the given string is


a Palindrome or not using slicing.

55) How can a class be instantiated in Python? Write a


Python program to express the instances as return
values to define a class RECTANGLE with parameters
height, width, corner_x, and corner_y and member
functions to find center, area, and perimeter of an instance.
56) Give some overview of turtle graphics in Python.
57) Explain any five turtle methods in python.
58) Draw a star using turtle methods.
59) Write program to Filling Radial Patterns with
Random Colors
60) Explain any six image methods in python.
61) Write a program to convert a color image to back
and white
62) Explain Tkinter Graphical User interface in python
63) Differentiate between terminal based and GUI based
programming in Python?
64) Write a program to draw a hexagon using turtle
65) Write a note on the image processing in Python
66) Describe the features of event driven Programming?
67) What is exceptions and how to handle it in python
68) Write a program to program to catch on Divide by
zero exception.Add a finally block too.
69) Differentiate default constructor and parameterized
constructor.
70) Explain the OOP Principle inheritance in python
with example.
71) Write a function that has a class Animal with a
method legs. Crete two sub classes Tiger and Dog.
Now access the method leg explicitly with the class Dog
and implicitly with the class tiger.
72) Explain inheritance in Python. Give examples for
each type of inheritance.
73) Explain the os and os.path modules in Python with
examples. Also, discuss the walk( )and getcwd( )
methods of the os module.
74) What are the important characteristics of CSV file
format.
75) Briefly Explain the role of Numpy in Python.
76) Write a Python program to add two matrices and
also find the transpose of the resultant matrix.
77) How to Reading and Writing CSV Files in Python
78) What are the advantages of Pandas.

You might also like