You are on page 1of 10

What is the difference between a comment and the docstring?

What are the features of Python?


1)easy to learn and use
2)Free and open source
3)Object-Oriented language
4)Large Standard Library

List Standard data types in Python


• Numeric
• String
• List
• Tuple
• Set
• Dictionary

Give example of Multiline Comment in Python


#This is a comment
#written in
#more than just one line
print("Hello, World!")

List various Bitwise operators in Python


What is Unicode String?
Normal strings in Python are stored internally as 8-bit ASCII, while Unicode strings are stored as 16-bit
Unicode. This allows for a more varied set of characters, including special characters from most languages
in the world

What is string and Raw String in Python?


String: - a string is a sequence of characters. For example, "hello" is a string containing a
sequence of characters 'h', 'e', 'l', 'l', and 'o'.
Raw String: - Python raw string is created by prefixing a string literal with ‘r’ or ‘R’. Python
raw string treats backslash (\) as a literal character.

Write the syntax of for loop in Python?


for val in sequence:
#Statement(s)

What is Pass Statement?


the pass statement is a null statement which can be used as a placeholder for future code.

Write the syntax of find() method?


The find() method returns the index of first occurrence of the substring (if found). If not
found, it returns -1.
What is tuple? What is the difference between list and tuple?
Tuples are used to store multiple items in a single variable. Tuple is immutable

Which method is used to update lists in Python?


you can add to elements in a list with the append() method.

List some few common Exception type.


Exception type
1) OSError
2) OverflowError
3) OSError
4) RuntimeError
5) SyntaxError
6) RuntimeError

What is docstring?
Python docstrings are strings used right after the definition of a function, method, class, or
moduleThey are used to document our code.

How will you create sets in Python?


A set is created by placing all the items (elements) inside curly braces {}, separated by
comma, or by using the built-in set() function.
my_set = {1, 2, 3}
print(my_set)

Output

{1, 2, 3}

Write the definition of class method.


A class method is a method that is bound to a class rather than its object. It doesn't require
creation of a class instance, much like static method.

What is __init__ method?


The __init__ function is called every time an object is created from a class. The __init__
method lets the class initialize the object’s attributes and serves no other purpose. It is only
used within classes.

Write a syntax of ‘‘for loop’’ in Python.


for val in sequence:
# statement(s)

Give the characteristics of membership operator?


Demonstrate self in Python?
The self parameter is a reference to the current instance of the class, and is used to
access variables that belongs to the class.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)

p1 = Person("John", 36)
p1.myfunc()

Explain what is range() function and how it is used in lists?


The range() function returns a sequence of numbers, starting from 0 by default, and
increments by 1 (by default), and stops before a specified number.
it is used in list to display a list of specific range
list1 = list(= range(2, 20, 3))
# 3 is step default iteration is 1 here iteration is by 2
print(list1)

ou can also use Python For Loop, to iterate over each element of range, and then append the item to list.
list1 = list()
for x in range(2, 20, 3):
list1.append(x)
print(list_1)
What are the built-in functions that are used in Tuple?
len(),
max(),
min(),
tuple(),
index(),
count(),
sum(),
sorted()

How does del operation work on dictionaries? Give an example.


del keyword is used to delete a dictionaries,remove key-value pairs from dictionary
dict1 = {'small': 'big', 'black': 'white'}
# check if my_dict1 and my_dict2 exists
print(dict1)

del dict1["black"]

print(dict1)

What is the output of the following statements?


l= [1, 2, 3,4]
print (l [0: 2])
Output: [1, 2]
4. Write a python program to check whether the string is or Palindrome
def palindromChecker(str):
if(str==str[::-1]):
print("it is palindrom")
else:
print("not a palindrom")

str="aaa"
palindromChecker(str)

5. List and explain types of operators in Python.


6. Explain the term List, Set, Tuple and Dictionary
7. Explain the following statements:
a. If ii. If else iii. Break iv. Continue
8. Write a python program to reverse words in a given String
str="sumedh"
print("reverse of string:",reverseStr(str))

9. Write a Python program to remove all duplicates from the given string in Python.
def duplicate(str):
newStr=""
for char in str:
if char not in newStr:
newStr=newStr+char

return newStr

str="helllo world"
print(duplicate(str))

10. Write a short note on function arguments.


11. Write a python program to create tuple with different data types.
tuple1 = ("tuple", False, 3.2, 1)
print(tuple1)

12. Write a Python function to calculate the factorial of a number (a non-negative


integer). The function accepts the number as a argument.
def factorize(num):
sum = 1
if(num>0):
for val in range(1,num+1):
sum=sum*val
return sum
elif num==0:
return sum
else:
print("the number must be better than 0")
num=int(input("give num:"))
print(factorize(num))

13. Write a Python Program to Check Prime Number


def primeNum(num):
for value in range(2,num):
if num%value==0:
return "it is not prime"

return "it is prime"


print(primeNum(3))

14. Python Program to Convert Celsius To Fahrenheit


num=int(input(“enter number”))
print("C to F:",(num * 1.8) + 32)

15. Write a Python Program to Check Leap Year


def checkLeap(year):
if((year % 400 == 0) or (year % 100 != 0) and (year % 4==0)):
print("leap year")
else:
print("not leap year")

checkLeap(2005)

16. Write a Python Program to Print all Prime Numbers in an Interval


lower = 900
upper = 1000

print("Prime numbers between", lower, "and", upper, "are:")

for num in range(lower, upper + 1):


if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)

17. Write a Python Program to Print the Fibonacci sequence


def fibonacii(num):
default0=0
default1=1
count=0
if(num>1):
while count < num:
print(default0)
term=default0+default1
default0=default1
default1=term
count+=1
elif num<=0:
print("please enter positive integer")
elif num==1:
print("sequence is 1")

fibonacii(4)

18. Write a Python Program to Find Armstrong Number in an Interval


lower = 100
upper = 2000

for num in range(lower, upper + 1):

# order of number
order = len(str(num))

# initialize sum
sum = 0

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

if num == sum:
print(num)

19. Write a Python Program to Find the Sum of Natural Numbers


def sumOfNatural(num):
if num > 0:
sum=0
for val in range(1,num+1):
sum=sum+val
return sum
else:
return "enter natural number"

print(sumOfNatural(-1))

20. Write a Python program to copy the contents of a file to another file.
with open("file1.txt","r") as file1,open("file2.txt","a") as file2:
for line in file1:
file2.write(line)
21. Write a program that accepts sequence of lines as input and prints the lines after
making all characters in the sentence capitalized.

#i have doubts about this,output:SUMEDH


str="suMedh"
print(str.upper())

You might also like