Lab_Ass_Python


Q.1 What is python?
Ans:
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Created by Guido van Rossum and first released in 1991, Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance.

Q.2 Explain some key features of python.?
Ans:
Some key features of python are as follows:
1) Easy to Learn and Use.
2) More expressive means that it is more understandable and readable.
3) interpreted language means interpreter executes the code line by line, makes debugging easy.
4) Free and Open Source.
5) Object-Oriented Language.
6) Extensible and Large Standard Library.
7) GUI Programming Support and Cross-platform Language.

Q.3 Is python keywords are case sensitive or not?
Ans:
Keywords are the reserved words in Python. In Python, keywords are case sensitive. There are 33 keywords in Python 3.7. This number can vary slightly over the course of time. All the keywords except True, False, and None are in lowercase and they must be written as they are.

Q.4 What is a python identifier?
Ans:
An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate one entity from another.

Q.5 What are the ways of using “comment” in pythons?
Ans:
Comments are programmer-coherent statements, that describe what a block of code means.
Comments In Python: 1. Single-Line Comments: Starts with #
#Comments
print(“Hello World”)
2 Multi-Line Comments: Starts with ’’’ends ’’’with triple quotes.
“””Multi Line Comments
in Python”””
print("Hello World")

Q.6 What is a string in python?
Ans:
In Python, Strings are arrays of bytes representing Unicode characters. Strings in Python can be created using single quotes or double quotes or even triple quotes.
Example:
str1 = 'Hi'
str2 = "I'm Shivam"
str3 = '''My name is "Shivam Namdeo"'''
str4 = """My Name
Is Shivam Namdeo
"""
Q.7 What is the list? Explain it by taking one example.
Ans:
Lists are just like dynamically sized arrays, A single list may contain DataTypes like Integers, Strings, as well as Objects. Lists are mutable. The elements in a list are indexed sequence wise.
Example:
#Creating Blank List In Python
list = []
#Creating Number List In Python
list = [1,2,3,4,5,6]
#Indexing In List In Python
print(list[0])
#Output = 1
#Slicing In List In Python
print(l[0:2])
#Output = [1,2]

Q.8 What is a tuple?
Ans:
A tuple in Python is similar to a list. The difference is that we cannot change the elements of a tuple
means it’s immutable.
Example
# Blank Tuple
tup = ()
print(tup)
#Tuples With Integers
num_tuple= (1, 2, 3)
print(num_tuple)
#Output
()
(1, 2, 3)

Q.9 What is set in python?
Ans:
A set is an unordered collection of items. Every set element is unique (no duplicates) and must be immutable (cannot be changed).
Example
# set of integers
set = {1, 2, 3}
print(set)

Q.10 What is the difference between list and tuple?

























Ans:






















Q.11 Is variable in python are case sensitive or not?
Ans:
Yes, variable in python are case sensitive
Example
var = "Shivam"
Var = "Shivam Namdeo"
print(var)
print(Var)
#Output
Shivam
Shivam Namdeo

Q.12 What is a pass in Python? Where we use the pass in python?
Ans:
pass is a null operation when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed.
Example:
str = "Shivam"

if 'S' in str:
pass

Q.13 What is a Dictionary in python?
Ans:
Python dictionary is an unordered collection of items. While other compound data types have only value as an element, a dictionary has key/value pairs.
Example:
# Blankdictionary
dict = {}
# dictionary with integer keys
dict = {1: 'Shivam', 2: 'Sahil'
print(dict[1])

Output:
Shivam

Q.14 What is slicing?
Ans:
Python slicing is about obtaining a sub-string from the given string by slicing it respectively from start to end.
Example:
str = "Shivam"
s = slice(0,4)
print(s)
print(str[0:2])
print(str[0:])

Output:
slice(0, 4, None)
Sh
Shivam

Q.15 What is the work of split() in python?
Ans:
Python string method split() returns a list of all the words in the string.
Example:
word = '1s1s1s1s1s1'
print(word.split('s'))
Output:
['1', '1', '1', '1', '1', '1']

Q.16 Is indentation optional in python?
Ans:
No, Indentation required in Python, indentation is a way of telling the python interpreter that statements belong to a particular block of code.
Example:
if 2>1:
print("2 is greater than 1")

Output:
2 is greater than 1

Q.17 What would be the output if you run the following code block?
list1 = [2, 13, 22, 24, 28]
print(list1[-2])
Ans:

24

Q.18 What is the difference between append() and extend() methods?
Ans:
append: Appends object at the end.
Example:
list = [10, 12, 15]
list.append([40, 45])
print (list)
Output:
[10, 12, 15, [40, 45]]

extend: Extends list by appending elements at the end.
Example:

list = [10, 12, 15]
list.extend([40, 45])
print (list)
Output:
[10, 12, 15, 40, 45]


Q.19 Explain the use of range in python.
Ans:
The range() function is used to generate a sequence of numbers in the list form.
Example:
r1 = range(10)
print(r1)
r2 = range(1,10)
print(r2)
r3 = range(1,10,2)
print(r3)

Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 3, 5, 7, 9]

Q.20 What is the difference between .py and .pyc files?
Ans:
Python compiles the .py files and saves it as .pyc file. The .pyc contain the compiled bytecode of python source files, A .pyc is not created for the main program file that you execute (only for imported modules). The .pyc file contains encoded python bytecode.


Q.21 What are negative indexes and explain by taking one example?
Ans:

Python programming language supports negative indexing, the negative indexing starts from where the array ends.
Example:
list = [1,2,3,4,5,6]

print(list[-1])
print(list[-2])
print(list[-3])

Output:
6
5
4

Q.22 How to access elements from a list?
Ans:
Example:
# Using Indexing

list = [1,2,3,4,5,6]
print(list[-1])
print(list[1])
Output:
6
1
# Using Slicing
li = list[0:3]
print(li)
Output:
[1, 2, 3]

Q.23 How to delete or remove elements from a list?
Ans:
# delete element from list
list = [1,2,3,4,5,6]
del list[0:2]
print(list)
Output:
[3, 4, 5, 6]
# remove element from list
list.remove(5)
print(list)
Output:
[1, 2, 3, 4, 6]

Q.24 Explain the use of count method in the list.?
Ans:
count() is an inbuilt function in Python that returns the count of how many times a given object occurs in the list.
Example:
# count 5 in list
list = [1,2,3,4,5,6,5]
print(list.count(5))

Output:
2

Q.25 What is if...else statement in Python?
Ans:
Python logical conditions if..else statement.

Example:
# logical condition
if 2>1:
print("Greater")
else:
print("Lower")

Output:
Greater

Q.26 What is for loop in Python? Write the Syntax for Loop.
Ans:
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

Example:
# For Loop
list = [1,2,3,4,5]
for i in list:
if i==6:
print(i)
else:
print("Not Found")

Output:
Not Found

Q.27 Write a program to find the sum of all numbers stored in a list using for loop?
Ans:
# Sum of all numbers of list using for loop
list = [1,2,3,4,5]
sum = 0
for i in list:
sum+=i

print("Sum : ",sum)
Output:
('Sum : ', 15)
Q.28 Explain how to open, read, write, and close a file in python and also write their syntax.?
Ans:
#”a” : will append to the end of the file
file = open("demo.txt","a")
file.write(" This is Shivam compiling python program")
# closing the file
file.close()
#”r” : will append to the end of the file
file = open("demo.txt","r")
print(file.read())
Output:
Welcome This is Shivam compiling python program

Q.29 What is a function in Python? How to call a function in python?
Ans:
A function is a block of code that only runs when it is called, you can pass data, known as parameters, into a function. the function can return data as a result. In python a function is defined using the def keyword.

#Function defined using def keyword
def sum(num1,num2):
return num1+num2

#calling function
print(sum(5,10))

Output:
15

Q.30 What are the types of functions used in python?
Ans:
Types of function in python 1.Built-In function, 2. User defined function, 3.lambda function
Example:
#1.Built-in function
list = [1,2,3,4,5]
print(len(list))
print(sum(list))
Output:
5
15

#2. User defined function
def sum(num1,num2):
return num1+num2

print(sum(5,10))
Output:
15

lambda arguments : expression
The expression is executed and the result is returned:

#3. Lambda
list = lambda a : range(a)
print(list(5))
Output:
[0, 1, 2, 3, 4]





Comments

Popular posts from this blog

HTML Signup Login Form With Source Code