Unit 4: Introduction to Python
Question and AnswerQuestion 1: Explain the features of Python language.
Answer: The Python language is one of the favourite of AI programmers because it:
1. Can work on different operating systems and platforms (like Windows, Mac, OS, Linux, Raspberry Pi etc.)
2. Has simple English-like syntax
3. Runs on the interpreter system, i.e., the code is written in Python can be executed as soon as it is written
4. Can create workflows
5. Can interact with databases
6. Is capable of handling big data
7. Can be used for performing complex mathematical operations.
Question 2: Explain print() function with an example.
Answer: print() function is used for printing the information on the screen.
Syntax of print() function is:
print("message/value")
Example:
print("Welcome to Python programming")
Output: Welcome to Python programming
Question 3: Explain comments in Python with an example.
Answer: Comments are used in program for explaining the code. They make the code more reliable and allow different individuals to understand the code more effectively and easily. The comments are ignored by Python. Comments in Python can be written in 2 ways:
1. Single line comments: Single line comments are created by adding # at the beginning of the line .
Syntax:
#statement
Example:
print("Welcome to Python programme")
#this program is used to display a line of text
Output: Welcome to Python programme
2. Multi-line comments: Multi-line comments
are created by enclosing the multiple lines within triple quotes(“””).
Syntax:
“””
Text
“””
Ex:
“””
This line
of text
is ignored by Python
“””
print("Hello World")
Output: Hello World
Question 4: Explain variables with an example.
Answer: Variables are containers used for storing data values. The value of a
variable can be changed based on the program.
Syntax: variable_name = variable_value
Ex: a=15
b= 40
The text values are assigned to variables by including them in single or double
quotes.
Ex: name="Mickey"
name ='Mickey'
Question 5: List rules for naming variables in Python.
Answer: The variable names in Python must follow the following rules:
1. The name of the variable must start with a letter or underscore (_)
Example: Name, NAME, _name are valid.
The following variable names will result in an error:
Example: 1name, &name, $name
2. The variable name can contain alphabetical characters and underscore.
3. The variable names are case sensitive.
Example: NAME differs from name.
4. White spaces or blank spaces are not allowed.
Example: Student name, father name are invalid.
Question 6: Explain the various types of data types supported by Python.
Answer: Data type represents the type of data that a variable can store. There
are 3 basic data types:
1. Integer Type (int)
Integer data type (int) includes whole numbers without decimals. Integers can
be positive and negative integers.
Example: 20, -12, 89, 98, -90 etc.
2. Float type (float)
Float data type (float) includes number with decimals and the numbers in the
scientific notation that is with e or E. Float data type can take both negative
and positive values.
Examples: 5.17, 3.14, -7.54e9, 76E20 etc.
3. String type (str)
String data type (str) represents a sequence of character data. The string data
type is enclosed in single or double quotes.
Ex: "Welcome to Python"
Question 7: Explain type function with example.
Answer: Type function is used to find the type of the data stored in variable.
Syntax:
type(variable_name)
Example:
a = 20
b = 3.19
c = "Hello World"
print(type(a))
print(type(b))
print(type(c))
Output:
<class 'int'>
<class 'float'>
<class 'str'>
Question 8: Explain arithmetic operators supported by Python with an example.
Answer: Arithmetic operators are symbols used to perform mathematical calculations on variables and values. Python support 7 arithmetic operators:
Operator |
Name |
Example |
Purpose |
+ |
Addition |
a+b |
Returns sum |
- |
Subtraction |
a-b |
Returns the difference of |
* |
Multiplication |
a*b |
Returns the |
/ |
Division |
a/b |
Returns the quotient of the division |
% |
Modulus |
a%b |
Returns the remainder of |
** |
Exponentiation |
a**b |
Returns the exponential |
// |
Floor division |
a//b |
Returns the integral quotient of the division |
Example:
a=20
b=5
c=a+b
d=a-b
e=a*b
f=a/b
g=a%b
h=a**b
i= a//b
print(c,d,e,f,g,h,i)
Output:
25 15 100 4.0 0 3200000 4
Question 9: Explain statements and expressions with an example.
Answer: Statements are unit of code which the interpreter can execute.
Ex: a=10
Expressions are the combination of values, variables, function and operators which the interpreter has to evaluated before execution.
Ex:
x=30
y=50
z=x+y
Question 10: Explain input() function with an example.
Answer: input() function is used to get the input from the user.
Syntax:
input(prompt)
Example:
print("Enter your name: ")
name = input()
print("Hello " + name)
Output:
Enter your name: ABCD
Hello ABCD
Note: By default input function returns str data type.
Question 11: Write a Python program to print a line of text.
Answer: print("message/value")
Example: print("Hello!")
Output: Hello!
Question 12: Write a program to display a number.
Answer:
a=int(input("Enter a number: "))
print=int(input(a)) or print(a)
Output:
Enter a number: 12
12
Question 13: Write a program to display an integer number, a float number and a string.
Answer:
For integer number:
a=int(input("Enter a number: "))
print=int(input(a)) or print(a)
Output:
Enter a number: 12
12
For float number:
name = input("Enter a number: ")
print=float(input(name)) or print(name)
Output:
Enter a number: 12.2
12.2
For string:
name = input("Enter a value: ")
print=input(name) or print(name)
Output:
Enter a value: ABCD
ABCD
Question 14: Write a program to find three different types of data.
Answer:
For integer number:
a=15
print(type(a))
Output: <class 'int'>
For float number:
b=3.15
print(type(b))
Output: <class 'float'>
For string:
c="Hello"
print(type(c)
Output: <class 'str'>
Question 15: Write a program to perform addition of two numbers.
Answer:
a=input("Enter a number: ")
b=input("Enter a number: ")
sum=int(a)+int(b)
print(sum)
Output:
Enter a number: 10
Enter a number: 20
30
Question 16: Write a program to perform addition of three numbers.
Answer:
a=input("Enter a number: ")
b=input("Enter a number: ")
c=input("Enter a number: ")
sum=int(a)+int(b)+int(c)
print(sum)
Output:
Enter a number: 10
Enter a number: 20
Enter a number: 30
60
Question 17: Write a program to perform all arithmetic operations on two values.
Answer:
a=10
b=5
print(a+b,a-b,a*b,a/b,a%b,a**b,a//b)
Output:
15 5 50 2.0 0 100000 2
Question 18: Write a program to read and display a number.
Answer:
a=int(input("Enter a number: "))
print=int(input(a)) or print(a)
Output:
Enter a number: 6
6
Question 19: Write a program to read and display name.
Answer:
a=str(input("Enter your name: "))
print=input(a) or print(a)
Output:
Enter your name: ABCD
ABCD
Question 20: Write a program to perform all arithmetic operations by reading two numbers.
Answer:
a=int(input("Enter a number: "))
b=int(input("Enter a number: "))
sum=a+b
difference=a-b
multiplication=a*b
division=a/b
modulus=a%b
exponentiation=a**b
floor_division=a//b
print(sum,difference,multiplication,division,modulus,exponentiation,floor_division)
Output:
Enter a number: 10
Enter a number: 5
15 5 50 2.0 0 100000 2
I Got 97% in Board CBSE By Getting this
ReplyDeleteCongratulations! Our website's main motto is to help students get good marks...
Delete