Python
- What are variables?
Ans- Variables are reserved memory locations that are used to store values and are referenced by the name.
Example : Assigning a contact number by referencing it by a contact name.
2.How to define a variable
Ans- Syntax to define a variable is as follows:
VariableName = Value
Example = phoneNumber = 123456
3.What are the rules for declaring a variable?
Ans- – Variable name should start with an alphabet or underscore (‘_’) followed by any number of alphabets, digits and underscores
– Variable names are case sensitive. Example: phoneNumber is different from PhoneNumber and phonenumber
– Variable name cannot be a reserved name Example: print, if, for, etc – Variable name cannot contain any special character other than underscore
4. How to print numpy version in system?
Ans – Run the following code
import numpy as np
print(np.__version__)
5.How to print Pandas version in system?
Ans – Run the following code
import pandas as pd
print(pd.__version__)
6.How many data types in python?
Ans – Python has five standard datatypes:
- Numbers
- String
- List
- Tuple
- Dictionary
7.What is the purpose of operator in python and explain its types?
Ans – Operators are used to perform operations on values and variables.
- Arithmetic Operators
- Logical operators
- Comparison operators
- Assignment operators
- Membership operators
- Identity operators
8. what is difference between arange and range function in python?
Ans – The range() function generates the integer numbers between the given start integer to the stop integer, which is generally used to iterate over for Loop.
The arange() function return evenly spaced values with a given interval.
For Free Demo classes Call: 8149467521
Registration Link: Click Here!
9. what is difference between numpy array and list in python?
Ans – Run the following code
import time
import numpy as np
size_of_vec = 1000
def pure_python_version():
t1 = time.time()
X = range(size_of_vec)
Y = range(size_of_vec)
Z = [X[i] + Y[i] for i in range(len(X)) ]
return time.time() – t1
def numpy_version():
t1 = time.time()
X = np.arange(size_of_vec)
Y = np.arange(size_of_vec)
Z = X + Y
return time.time() – t1
t1 = pure_python_version()
t2 = numpy_version()
print(t1, t2)
print(“Numpy is in this example ” + str(t1/t2) + ” faster!”)
10. What is use of regular expression?
Ans: Regular expression is a sequence of characters that defines a search pattern.
Regular expression can be used to search , match, edit and manipulate text data.
11. Remove digits from following programme using regular expression?
Ans –
d = ‘seven12345’
output = ”.join([i for i in d if not i.isdigit()])
print(output)
12. What is string and how to define one?
Ans- A string is a sequence of characters that are enclosed within single quotes or double quotes. Example: “Welcome to python programming.” or ‘Python is a very simple language’.
13. what is List and how to create one?
Ans -A list is a container that holds many objects under a single name
Syntax to define a list is as follows:
listName = [obj1, obj2, obj3]
Ex. listName = [‘yash’, ‘prakash’, ‘vinod’]
14. what is tuple and how to create one?
Ans: A tuple is a container that holds many objects under a single name. A tuple is immutable which means, a tuple once defined cannot be modified.
Syntax to define a tuple is as follows:
tupleName = (object1, object2, object3)
Example: bestFriends = [‘Rahul’, ‘Pranav’, ‘Raju’, ‘Ketan’]
15. What is dictionary and how to create one?
Ans: A dictionary is a set of key-value pairs referenced by a single name
Syntax to define a dictionary is as follows:
The syntax to create a dictionary is as follows:
dictionaryName = {“keyOne” : “valueOne”, “keyTwo”, “valueTwo”}
Example: Consider the following dictionary that stores the colour of fruits with key as the fruit name as value as its color.
coloroffruits = {“apple”: “red”, “mango”: “yellow”, “orange”: “orange”}
16. what are looping statements and implementation of For loop.
Ans – Looping is used to repeatedly perform a block of statements over and over again.
For loop is used to iterate over a sequence, starting from the first value to the last.
The number of iterations to be performed depends upon the length of the list.
Syntax:
for iteratingVariable in sequence:
statement 1
statement 2
– – – – – –
statement n
Example: numbers = [1, 2, 3, 4, 5] for number in numbers: print(number, end=‘ ‘)
17.What are functions and how to define one?
Ans : A function is a named block of reusable code that performs a specific task.
Syntax to define a function is as follows:
Syntax:
def functionName():
statement 1
statement 2
– – – – – –
statement n
18. What is exception handling and how to handle exceptions in python?
20. write a programe to create list of 6 numbers?
Ans – L =[22, 12, 33, 44 ,55, 66]
21. update the value of 4rd element of list
Ans – L[2] = 15
print(L)
22. Create another list of 3 elements . Now create a final result as a concatenation of the first two lists.
Ans : # Create a list to store 5 odd numbers
oddNumbers = [1, 3, 5, 7, 9]
# Update the element at the third position
oddNumbers[2] = 15
# create a list to store even numbers
evenNumbers = [2, 4, 6]
# Perform concatenation to join both the lists by using ‘+’ operator
concatenatedList = oddNumbers + evenNumbers
print(concatenatedList)
For Free Demo classes Call: 8149467521
Registration Link: Click Here!
23. Write a program to create a tuple of 5 elements
Ans – # Create a tuple of 4 elements
leapYear = (2016, 2020, 2024, 2028)
print(leapYear)
24. Convert this tuple into a list
Ans : –
leapYear = (2016, 2020, 2024, 2028)
# To convert the tuple into a list, perform a typecasting
leapYearList = list(leapYear)
print(leapYearList)
25. Now delete the first element in this list and convert it back to tuple
Ans : –
# Create a tuple of 4 elements
leapYear = (2016, 2020, 2024, 2028)
# To convert the tuple into a list, perform a typecasting
leapYearList = list(leapYear)
# Delete first element of list
del leapYearList[0]
# To convert the list to tuple, perform a typecasting
convertedLeapYearTuple = tuple(leapYearList)
print(convertedLeapYearTuple)
26. Write a program to create a dictionary with 4 key-value pairs
Ans: # Create a dictionary to store the age of 4 employees
ageOfEmployees = {‘Ben’: 25, ‘Matthew’: 32, ‘Mark’: 28, ‘Mary’: 21}
print(ageOfEmployees)
27. Update the value of a key
Ans – # Create a dictionary to store the age of 4 employees
ageOfEmployees = {‘Ben’: 25, ‘Matthew’: 32, ‘Mark’: 28, ‘Mary’: 21}
# Update the age of Matthew
ageOfEmployees[‘Matthew’] = 35
print(ageOfEmployees)
28. Copy this dictionary to another dictionary and clear the first dictionary
Ans:
# Create a dictionary to store the age of 5 employees
ageOfEmployees = {‘Ben’: 25, ‘Matthew’: 32, ‘Mark’: 28, ‘Mary’: 21}
# Update the age of Matthew
ageOfEmployees[‘Matthew’] = 35
copyOfAgeOfEmployees = ageOfEmployees.copy()
ageOfEmployees.clear()
print(‘copyOfAgeOfEmployees = {}’.format(copyOfAgeOfEmployees))
print(‘ageOfEmployees = {}’.format(ageOfEmployees))
29. Write a program to check if a number is less than 10
Ans :
# Check if 3 is less than 10
if 3 < 10:
print(‘3 is less than 10’)
30. Write a program to check if a given number is odd or even
Ans :
Even numbers are numbers that are divisible by 2. If the number is not divisible by 2, that’s an odd number
number = 11
if number % 2 is 0:
print(‘Even number’)
else:
print(‘Odd number’)
For Free Demo classes Call: 8149467521
Registration Link: Click Here!
31. Write a program to check if a number is divisible by both 10 as well as 50. If it is divisible by 30 as well, print “This number is divisible by 10, 50 and 30”. If not, print “This number divisible by 10 and 50 but not 30.
Ans –
number = 150
if number % 10 is 0 and number % 50 is 0:
if number % 30 is 0:
print(‘This number is divisible by 10, 50 and 30’)
else:
print(‘This number is divisible by 10 and 50 but not 30’)
32. What are conditional statements?
Ans : Condition statements are a block of statements whose execution depends on a certain condition.
33. What are the different types of conditional statements in Python ?
Ans : – if, if_else, if_elif_if
34. What is if statements?
Ans – A “simple if” condition is one where a block of statements get executed if the condition mentioned in the “if” statement evaluates to true.
Example:
distance = 100
if distance == 100:
print(“Distance is 100”)
35.How if_else statement works?
Ans : – An “If-Else” statement is one where a block of statements under “if” condition gets executed if the condition evaluates to true. If the condition evaluates to false, the block of statements under “else” is executed.
Example :
distance = 200
if distance <= 100:
print(“Distance is less than or equal to 100”)
else:
print(“Distance is greater than 100”)
36. How if_elif_else statements works?
Ans – An “If-Elif-Else” statement is one where multiple “if” conditions are evaluated one after another if an “if” statement evaluates to false. “elif” stands for else-if. If all the if conditions evaluates to false, the block of statements under “else” gets executed.
distance = 400
if distance <= 100:
print(“Distance is less than or equal to 100”)
elif distance <= 200:
print(“Distance is less than or equal to 200”)
elif distance <= 300:
print(“Distance is 300”)
else:
print(“Distance is greater than 300”)
37. How nested if statement works?
Ans :-
An if statement within another if statement is called a nested if statement.
Example:
distance = 50
if distance < 100:
if distance == 50:
print “Distance is 50”
38. Why is Looping used?
Ans :-
Looping is used to repeatedly perform a block of statements over and over again.
39. What are different types of looping?
Ans : –
For, while and nested are the 3 types of Looping.
40. what is for loop and how to create it?
Ans – For loop is used to iterate over a sequence, starting from the first value to the last. The number of iterations to be performed depends upon the length of the list.
Syntax:
for iteratingVariable in sequence:
statement 1
statement 2
– – – – – –
statement n
41. How do you use while loop?
Ans – “While loop” is used to repeatedly execute a block of statements as long as the condition mentioned in the “while loop” holds true.
Synatx:
Syntax:
while condition:
statement 1
statement 2
– – – – – –
statement 3
43. Write a program to print numbers from 1 to 10
Ans:-
For loop to print numbers in range 1 to 10
for number in range(1, 11):
print(number, end = ‘ ‘) # Use the parameter end = ‘ ‘ to add a whitespace at the end of the print statement instead of a new line
44. Write a program to print the sum of odd numbers from 1 to 10
Ans : –
Declare a variable to keep track of the sum of odd numbers
oddSum = 0
# Run a for loop from 1 to 10
for number in range(1, 11):
# Check if the number is odd by checking if it is not divisible by 2
if number % 2 is not 0:
oddSum = oddSum + number
print(oddSum)
For Free Demo classes Call: 8149467521
Registration Link: Click Here!
Call the Trainer and Book your free demo Class for now!!!
© Copyright 2019 | Sevenmentor Pvt Ltd.
Very much usefull
Concepts got cleared
Nice
Very helpful