Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python 20 Questions and answers

The document provides an overview of various data types and concepts in Python, including string, list, boolean, integer, float, and tuple data types. It explains expressions, statements, and the significance of indentation in Python code, along with the PEMDAS rule for operator precedence. Additionally, it covers the creation of tuples, including single-item tuples, and the types of expressions such as constant, floating, arithmetic, integral, relational, and logical expressions.

Uploaded by

deeps.3d
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python 20 Questions and answers

The document provides an overview of various data types and concepts in Python, including string, list, boolean, integer, float, and tuple data types. It explains expressions, statements, and the significance of indentation in Python code, along with the PEMDAS rule for operator precedence. Additionally, it covers the creation of tuples, including single-item tuples, and the types of expressions such as constant, floating, arithmetic, integral, relational, and logical expressions.

Uploaded by

deeps.3d
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1. What do u mean by python code execution?

Python's traditional runtime execution model: source code you type is translated to byte
code, which is then run by the Python Virtual Machine. Your code is automatically compiled,
but then it is interpreted.

2. Define string data type


Strings in Python are arrays of bytes representing Unicode characters. A string is a collection
of one or more characters put in a single quote, double-quote, or triple-quote. In python there
is no character data type, a character is a string of length one. It is represented by str class.
For Example :
str="Hello World!"
Here the value “Hello World!” will be assigned to string str

3. Define list data type


Lists are just like arrays, declared in other languages which is an ordered collection of data.
It is very flexible as the items in a list do not need to be of the same type.
The values in the list can be modified, i.e., it is mutable. The values that make up a list are
called its ‘elements’.
For Example :
list1=[100, 200, 300, 400, 500]
list2=[“Raman”, 100, 200, 300, “Ashwin”]
list3=[‘A’, ‘E’, ‘I’, ‘O’, ‘U’]

4. Define Boolean data type


Python boolean type is one of the built-in data types provided by Python, which represents
one of the two values i.e. True or False. Generally, it is used to represent the truth values of
the expressions.
For example, 1==1 is True whereas 2<1 is False.
5. Define integer data type
Integers – This value is represented by int class. It contains positive or negative whole
numbers (without fractions or decimals). In Python, there is no limit to how long an integer
value can be.
Eg:
• var1 = 1
• var2 = 10
6. Define float data type
float (floating point real values) - Also called floats, they represent real numbers and are
written with a decimal point dividing the integer and the fractional parts.
Eg:
• Var1=1.0
• Var2=2.5
7. Define expressions in python
• An expression is a combination of operators and operands that is interpreted to produce
some other value.
• In any programming language, an expression is evaluated as per the precedence of its
operators.
So that if there is more than one operator in an expression, their precedence decides which
operation will be performed first.
8. What do u mean by python indentation
• Indentation refers to the spaces at the beginning of a code line.
• Where in other programming languages the indentation in code is for readability only, the
indentation in Python is very important.
• Python uses indentation to indicate a block of code.
• Example
If 5>2 :
print("Five is greater than two!")
9. Define associativity rule
• Associativity Rule
• All the operators, except exponentiation (**) follow the left to right associativity.
• It means the evaluation will proceed from left to right, while evaluating the expression.
• value of expression will be, 43+13-21 = 56-21= 35.
10. Define statements
• A Python statement is an instruction that the Python interpreter can execute. There are
different types of statements in Python language as Assignment statements, Conditional
statements, Looping statements, etc.
• The token character NEWLINE is used to end a statement in Python.
11. Define Tuple
• Tuples are used to store multiple items in a single variable.
• Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3
are List, Set, and Dictionary, all with different qualities and usage.
• A tuple is a collection which is ordered and unchangeable.
• Tuples are written with round brackets.
• Example:
• Create a Tuple:
thistuple = ("apple", "banana", "cherry")
print(thistuple)
OUTPUT:
('apple', 'banana', 'cherry')
12. Define ordered tuple
When we say that tuples are ordered, it means that the items have a defined order, and that
order will not change.
13. Define unchangeable tuple
Tuples are unchangeable, meaning that we cannot change, add or remove items after the
tuple has been created.
14. What do u mean by Constant expression, Floating expression, arithmetic
expression, Integral expression, relational expression, logical expression
* Constant expression : An expression in Python that contains only constant values
are known as a constant expression.
# Constant Expressions
x = 15 + 1.3
print(x)
Output
16.3
Floating expression : These are the kind of expressions which produce floating point
numbers as result after all computations and type conversions
# Floating Expressions
a = 13
b =5
c =a /b
print(c)
Output
2.6
Arithmetic expression : An arithmetic expression is a combination of numeric values,
operators, and sometimes parenthesis. The result of this type of expression is also a
numeric value. The operators used in these expressions are arithmetic operators like
addition, subtraction,
# Arithmetic Expressions
x = 40
y = 12
add = x + y
sub = x - y
pro = x * y
div = x / y

print(add)
print(sub)
print(pro)
print(div)
52
28
480
3.3333333333333335
Integral expression : These are the kind of expressions that produce only integer results
after all computations and type conversions.
# Integral Expressions
a = 13
b = 12.0

c = a + int(b)
print(c)
Output
25

Relational expression : In these types of expressions, arithmetic expressions are written


on both sides of relational operator (> , < , >= , <=). Those arithmetic expressions are
evaluated first, and then compared as per relational operator and produce a boolean
output in the end. These expressions are also called Boolean expressions.
Relational Expressions
a = 21
b = 13
c = 40
d = 37
p = (a + b) >= (c - d)
print(p)
Output
True

Logical expression : These are kinds of expressions that result in either True or False. It
basically specifies one or more conditions. For example, (10 == 9) is a condition if 10 is
equal to 9. As we know it is not correct, so it will return False.
Example :
P = (10 == 9)
Q = (7 > 5)

# Logical Expressions
R = P and Q
S = P or Q
T = not P

print(R)
print(S)
print(T)
Output
False
True
True

15. Define comments in python


• Python has commenting capability for the purpose of in-code documentation.
• Comments start with a #, and Python will render the rest of the line as a comment:
• Example
• Comments in Python:
• #This is a comment.
• print("Hello, World!")
16. Define tuple items
• Tuple items are ordered, unchangeable, and allow duplicate values.
• Tuple items are indexed, the first item has index [0], the second item has index [1] etc.
17. Define tuple length
• To determine how many items a tuple has, use the len() function:
• Example
• Print the number of items in the tuple:
• thistuple = ("apple", "banana", "cherry")
• print(len(thistuple))
• OUTPUT: 3
18. Explain the PEMDAS rule in two lines.
• Operator precedence in python follows the PEMDAS rule for arithmetic expressions. The
precedence of operators is listed below in a high to low manner.
• Firstly, parentheses will be evaluated, then exponentiation and so on.
• P – Parentheses
• E – Exponentiation
• M – Multiplication
• D – Division
• A – Addition
• S – Subtraction
• In the case of tie means, if two operators whose precedence is equal appear in the
expression, then the associativity rule is followed.
19. How is “ 10 + 20 * 30 “ computed using associativity rule.
According to Arithmetic operator precedence rules, Addition Operator has lower precedence
than Multiplication operator, hence in 10 + 20 * 30, 20 * 30 will be solved first.
Next, 10 + 600 will be solved, since only one operator is left in expression the final answer
will be 610.

20. Explain how to create tuple with only one item.


To create a tuple with only one item, you have to add a comma after the item, otherwise
Python will not recognize the variable as a tuple.
thistuple = ("apple",)
print(thistuple)
print(type(thistuple))
Output
('apple',)
<class 'tuple'>

You might also like