Introduction to Python
Q.26- What is the role of Python Interpreter ?
Ans. Python Interpreter- It is a computer program that executes code written in python. Python interpreter takes an interactive command and executes it.
Q.27- Why we use python in AI ?
Ans. Python has rich library, it is also object oriented, easy to program. It can be also used as frontend language. That's why it is used in artificial intelligence.
Q.28- Write any 4 features of Python.
Ans. Features-
1)No Declaration required (2) Inbuilt OOP support/Library (3)Less Code
(4)Ease of learning (5)Platform independent (6) Non restrictive syntax.
Q.29- What is Python and its applications?
Ans. Python is a widely-used, interpreted, object-oriented, and high-level programming language with dynamic semantics, used for general-purpose programming.
Applications for Python
Web Development.
Game Development.
Machine Learning and Artificial Intelligence.
Data Science and Data Visualization.
Desktop GUI.
Web Scraping Applications.
Q.30- What do you understand by data types? Explain numeric data types in brief.
Ans. Data type- Data type is an attribute associated with a piece of data that tells a computer system how to interpret its value.
Numeric data types are numbers stored in database columns. These data types are typically grouped by: Exact numeric types, values where the precision and scale need to be preserved. The exact numeric types are INTEGER , BIGINT , DECIMAL ,NUMERIC , NUMBER , and MONEY .
Q.31- Who was created Python programme?
Ans. Guido van Rossum.
Q.32- What is Variable in Python? Give example.
Ans. A Python variable is a reserved memory location to store values. In other words, a variable in a python program gives data to the computer for processing.
Examples:-
Numbers, List, Tuple, Strings, Dictionary, etc.
Q.33- How to run Python Shell?
Ans. To run a python shell, follow the given steps below.
1. Open the power shell or command prompt on windows, and terminal window on Mac.
2. Write python there and then press enter.
3. A python prompt consisting of three greater than symbols i.e., “>>>” appears. Now, just enter the single statement and see the results accordingly.
Q.34- Explain String in Python with the help of example.
Ans. A string is a series of characters. In Python, anything inside quotes is a string. And you can use either single quotes or double quotes.
For example: message = 'This is a string in Python' message = "This is also a string"
Q.35- Write any four standard data types of python .
Ans. 1. Numeric 2. String. 3. List. 4.Tuple 5. Set.6. Dictionary.
Q.36- What are comments in python ? List down the various types of comments.
Ans. Comments in Python are the lines in the code that are ignored by the compiler during the execution of the program. Comments are non-executable statements in Python. It means neither the python compiler nor the PVM will execute them.
There are three types of comments in Python –
1. Single line Comments
2.Multiline Comments
3. Docstring Comments
Q.37- What are the different modes for coding in python?
Ans. n the Python programming language, there are two ways in which we can run our code:
1. Interactive mode
2. Script mode
Q.38- What is the use of Arithmetic Operators in Python? Write any four arithmetic
operators.
Ans. Arithmetic operators are used to perform mathematical operations like addition,subtraction, multiplication and division.
Q.39- Why is python the preferred programming language for AI?
Ans. Because of its features like:
1. Standard built in Library, Less Code,
2. Ease of learning,
3. Platform independent,
4. Massive Community Support,
5. Data Generation,
6. More effective algorithm,
7. Interpretive run time support
Q.40- What are data types in python. Give some example of data types.
Ans. Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data.
1. Example of data types
2. Numeric
3. Sequence Type
4. Boolean
5. Set
6. Dictionary
Q.41- Write a Python Code to find multiplication of two numbers. Display the result.
Ans. a=4
b=2
multi=a*b
print(multi) Result - 8
Q.42- Sh. Suresh has written following python code. This code is showing error,help Sh.
Suresh to rectify the error. Underline the error and rewrite the code.
a=10
20=b
a+b=sum
show (sum)
Ans. Correct Code-
a=10
b=20
sum=a+b
print(sum)
Q.43- Write the output of the following python statements.
(i) print('Good Morning, India')
(ii) print(0.25*0.5*2.5)
(iii) print (250+350*2//5)
Ans. (i) Good Morning, India
(ii) 0.3125
(iii) 390
Q.44- Identify the data type.
(i) dob = [19,"January",1990]
(ii) t = (5,'program',2.5)
(iii) a = {1,2,2,3,3,3}
Ans. (i) Lists
(ii) Tuple
(iii) Sets
Q.45- Write a python program to print weather the given number is prime or not (With
output)
Ans. number = int(input("Enter any number: "))
# prime number is always greater than 1
if number > 1:
for i in range(2, number):
if (number % i) == 0:
print(number, "is not a prime number")
break
else:
print(number, "is a prime number")
# if the entered number is less than or equal to 1
# then it is not prime number
else:
print(number, "is not a prime number")
Output - Enter any number : 13
13 is a prime number
Q.46- Match the Column 'A' with Column 'B'
Column A Column B
(i) + (Addition ) (i) Comparison operators
(ii) = = (ii) Logical operators
(iii) NOT (iii) Arithmetic operators
(iv) + = (Add AND) (iv) Assignments Operators
Ans. Column A Column B
(i) + (Addition ) (i) Arithmetic operators
(ii) = = (ii) Comparison operators
(iii) NOT (iii) Logical operators
(iv) + = (Add AND) (iv) Assignments Operators
Q.47- Write the Boolean value ( True or False) by the following relational expression in
python
(i) print(10!=23) (ii) print ( 10 == 23)
(iii) print ( 10 >= 23) (iv) print ( 10 >23)
Ans. (i) True (ii) False (iii) False (iv) False
Q.48- What output given by the following python code?
(i) var = "India"
print(len(var))
(ii) list1 =("Hindi", "Maths" , " Science")
print(list1);
(iii) print("Good"\
"Moring"\
"India")
Ans. (i) 5
(ii) ('Hindi', 'Maths', ' Science')
(iii) Good Moring India
Q.49- Write a program in Python to find the largest of two numbers.
Ans. number1 = int(input('Enter any number '))
number2 = int(input('Enter second number '))
if number1>number2:
print(number1,' is largest')
else:
print(number2,' is largest ')
Q.50- Write the output of the following python statements 5
i) print('Sunday' + 'Monday' + 'Holiday')
ii) print(16 * 4)
iii) print(1998 - 1964)
iv) print(100 // 25)
v) print(390 + 27 + 63 + 71)
Ans. i. SundayMondayHoliday
ii. 64
iii. 34
iv. 4
v. 551
Q.51- Write the Boolean value („True‟ or „False‟) returned by the following relational
expression in Python 3
a. 10140 > 10104
b. 356 < 78
c. 'Lion'=='Leon'
Ans. a) True
b) False
c) False
Q.52- Match the entries of Column „A‟ with Column „B‟
Column A Column B
len() mylist.append(245)
append() to return no of times an element found in list
sort() to calculate the number of elements in a list
count() arrange the items in the ascending order in list
Ans. Column A Column B
len( ) to calculate the number of elements in a list
append( ) mylist.append (245)
sort arrange the items in the ascending order in list
count( ) to return no of times an element found in list
Q.53- What output given by the following Python code?
SP = 3126
CP = 2500
x = SP - CP
if x > 0:
print('Profit gain')
else:
print('Loss suffer')
Ans. Ans : Profit gain
Q.54- Identify and write the name of variables used in the following python program .
player_name = 'Sachin'
century = 51
runs = 15000
print('Players name is ', player_name)
print('He made',century,'centuries')
print(runs,' Runs is the world record')
book = 'Autobiography'
price = 450
print('lets purchase the book', book)
print('Its cost only ', price,' Rs')
Ans. player_name, century, runs, book, price
Q.55- Python Program to Check if a Number is Odd or Even.
Ans. num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even number".format(num))
else:
print("{0} is Odd number".format(num))
Q.56- Python Program to check if a Number is Positive, Negative or Zero
Ans. def NumberCheck(a):
if a > 0:
print("Number given by you is Positive")
elif a < 0:
print("Number given by you is Negative")
else:
print("Number given by you is zero")
a = float(input("Enter a number as input value: "))
NumberCheck(a)
Q.57- Write a program to add two number in python.
Ans. num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
sum = (num1) + (num2)
print(sum)
Q.58- Write a Python program to display calendar.
Ans. import calendar
# Enter the month and year
yy = int(input("Enter year: "))
mm = int(input("Enter month: "))
# display the calendar
print(calendar.month(yy,mm))
Q.59- What is python IDEs. Write some Python IDEs.
Ans. It is specially designed for software development that consists of several tools which
is used for developing and testing the software.
There are some Python IDEs
PyCharm
Spyder
PyDev
Atom
Microsoft Visual StudioJupyter Notebook
Q.60- Find the output of given program.
(1) L1 = ["John", 102, "USA"]
(2) T2 = ("Apple", "Banana", "Orange")
print(type(L1))
print(type(T2))
Ans. <class 'list'>, <class 'Tuple'>