Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

9. Ipython. (Interactive Python).

It is a command shell for interactive computing in multiple programming languages, originally developed for the Python programming language
To Start the ipython type "ipython" in the command
prompt shell
Ipython Shell

10. Python Interpreter Architecture.


(1) Parser. Parser uses the source code to generate an abstract syntax tree.
(ii) Compiler. Complier turns the abstract syntax tree into Python bytecode.
(ii) Interpreter. Python Interpreter executes the Source code line by line in a REPL (Read-Evaluate-Print- Loop) fashion.

11. Different types of files in python:


File Extension on python

12. Comments.
These are the statements which are added with code to make the source code more understandable and that will not be complied with source
code.
Single-line comments are created simply by beginning a line with the hash (#) character and they are automatically terminated by the end of
the line.
For example:
#We are learning Python.
Multiple line comment. If we want to write the paragraph or to make multiple line as comments. it should be started in triple quotes (" ")
For example:
"We are learning Python. Python is a general purpose language written by "Guido van Rossum" in the year 1981. He got the idea of naming it as Python
from the comedy series Monty Python's Flying Circus."
13. Tokens.
A token is the smallest element of a Python script that is meaningful to the interpreter. The following categories of tokens exist: identifiers,
keywords, literals, operators, and delimiters.
• Identifiers: The name of any variable, constant, function, or module is called an identifier. Examples of identifiers are: print, int, type etc.
Keywords: The reserved words of Python which have a special fixed meaning for the interpreter are called keywords. No keyword can be
used as an identifier.
Literal: A fixed numeric or non-numeric value is called a literal.
Examples of literals are: 2, -23.6, "abc", "Independence' etc.
Operators: A symbol or a word that performs some kind of operation on given values and returns the result.
Examples of operators are: +, -, **, / etc.
Delimiters: Delimiters are the symbols which can be used as separators of values, or to enclose some values.
Examples of delimiters are: () { } [ ] : ;.
Note: # symbol used to insert comments is not a token. Any comment itself is not a token.
14. print(). is a function which is used to display the specified content on the screen. The content (called argument) is specified within the
parentheses. e.g.
>>> print ("Hello World")
Hello World
>>>
15. input(). input() function is used to get data type (default String) from the user during script execution.
For Example:
>>> str=input("Enter")
Enterrachna
>>> str
'rachna'
>>> type(str)
<class 'str'>
>>> str-input("Enter")
Enter7
>>> type(str)
<class 'str'>
16. Data types. In Python, each value is an object and has a data type associated with it. Data type of a value refers to the kind of value it is.
Following are the built-in data types in Python:
int (Integer): Represents integral numbers (numbers without any fractional part). Examples of integral numbers are -3, 8, 4, 0, 13434, -547554677854545.
Supported precisions: 8, 16, 32 (default) and 64 bits.
Review of Python-1 21

uint (Unsigned integer types). Supported precisions: 8, 16, 32 (default) and 64 bits.

float: Represents floating point values (numbers with fractional part). The fractional part of a floating point number may be 0 also. Examples
of floating point numbers are 3.14, -23.9, 8.0, 56667.78. Supported precisions: 16, 32, 64 (default) bits and extended precision floating point.

str(String): Represents strings of characters enclosed within quotation marks (Or ""). Examples of strings are 'Hello', "Programming", "108',
'29 States of India', "xyz@gmail.net".Supported precisions: 8-bit positive multiples.

bool (Boolean): Represents a True or False value. Any expression which can be True or False has the data type bool. Examples of Boolean
expressions are 3>4, 5<=18.Supported precisions: 8 (default) bits

complex: Represents complex numbers of the for a+bi, where a is the real part and b is the imaginary part. In Python, j is used instead of i to represent the
imaginary part. It means that in Python notation a+bj is a complex number. Supported precisions: 64 (32+32), 128 (64+64, default) bits.

Time: Data/time types. Supported precisions: 32 and 64 (default) bits.

Enum: Enumerated types. Precision depends on


base type.
17. Type(). It is used to display the datatype of the variable.

>>> x=8
>>>type (x)
<class 'int'>
>>> type (x)
<class 'str'>
>>>x=9.8
>>>type(x)
<class 'float'>
>>>

18. eval(). It is used to read the expression or any other type


data and convert that into the respective datatype.
For example
>>>>> x=eval(input("Enter"))
Enter3
>>> type(x)
<class 'int'>> x=eval (input("Enter"))
Enter3.4
>>> type(x)
<class 'float'>
>>> x=eval(input("Enter"))
Enter "naveen"
>>> type(x)
<class 'str'>
>>> x=eval(input("Enter"))
Enter5>6 and 6<7
>>> type(x)
<class 'bool'>
>>> x=eval(input("Enter"))
Enter[1,2,3,4]
>>> type(x)
<class 'list'>
19. Operator. They are used to perform operations on operands(variables/values).

20. Arithmetic operators. These are used in mathematical expressions.

21. Assignment Operator. The sign (=) is known as assignment operator which is used to assign the right side value to the left side operand.
e.g. n=7.

22. Augmented Assignment Operators. An Augmented Assignment operator (or compound assignment operators or shorthand operators) is a combination
of a binary operation and assignment. Different augmented assignment operators available in Python are:
23. Identity Operators. Identity operators are used to compare the objects (Class or type) and return the result in True or False. Different
types of Identity operators are:

24. Membership operators. These are used to test if a sequence is presented in an object such as strings, lists, or tuples. It also returns the
result either in True or False. Different types of Membership operators.

25. Comparison or Relational Operators. In order to depict comparison of two values. Python provides Relational operators (also called
Comparison operators) to compare two values. These relational operators are:
26. Logical Operators.
A simple condition can be formed using a relational operator. Sometimes a condition contains more than one comparisons. For example, to
check if the marks are in the range 28 to 32. In this case, we have to check that marks are greater than or equal to (>=) 28 as well as marks
are less than or equal to (<=) 32. Each relational operator forms a simple condition. If a condition contains more than one relational operators
then we can say that the condition is complex and the complex condition is formed by joining simple conditions using Logical operators. A
complex condition can also be formed by negating a simple condition. For example, to check that a condition we have to is not true. For
forming complex conditions, use logical operators with simple conditions. Different logical operators available in Python are:
27. Operator Precedence.
Operator precedence determines which operators are applied before others when an expression contains more than one operators. Following
is the order of precedence of operators:
(**)>(unary+, unary -)> (*, /, //, %)> (binary +, binary-)

28. Variables. A variable is a named memory location which is used to store data. The contents of a variable may change during the programs execution. A
variable can store one and only one data value at a time. When a new value is stored in a variable, its previous value gets overwritten.

29. None Keyword. It is used to store NULL value or no value. None is neither 0 nor false nor empty string. For example:
>>> x=None
>>> type(x)
<class 'NoneType'>

30. pass. pass statement does nothing. This statement in python is used for empty block of statement(s).
For example:
X. [10,20,30]
For ele in X:
if not ele
Pass
print(ele)
31. String. Collection of character is known as String. String is a datatype and class in python.

32. Str.format(). This function is used to allow us to do multiple substitutions and value formatting. This built- in String class method, lets us to
join elements within a string through positional formatting.
For example:
>>> x=7
>>> y=5
>>print("),hello world[]".format(x,y))
7, hello world5

33. Type Casting. It is a process of converting one datatype to another forcefully.


Example:
>>>X=int(input("Enter") # String is changing to int
or
>>> x=25
>>> type(x)
<class 'int'>
>>> x=x/3
>>> type(x) #Default conversion was float
<class 'float'>
>>> x=int(x/3) #Float is converting to int
>>> type(x)
<class 'int'>

34. Implicit type conversion. Conversion of one data type to another data type without the programmer intervention is known as implict type
conversion.
>>> x=25
>>> type(x)
<class 'int'>
>>> x=x/3
>>> type(x)
<class 'float'>

35. Statements. Statements are the instructions that are given to the computer to perform any kind of action.
Types of statements: There are three types of statements: Empty, Simple,Compound.
(i) Empty: The simplest statement is the empty statement which does nothing.
For example, pass
(ii) Simple statement: Any single executable statement is a simple statement. For example,
print (name)
(iii) Compound statement: - A group of statements executed as a unit and in python we can group the statements using (:) colon. In python,
indentation is compulsory.
For example:
for i in range(5):
Print(i)
36. Indentation. Indentation is shifting of a statement to the right of another statement. In Python, indentation is used to form suites (blocks of
statements). It is a way of writing the code on the right side of the above statement.
For example:
if a>0:
print("hello") # It is Indentation
37. Conditional Construct/Decision Control. We can control the flow of the program in such a path along these lines, to the point that it
executes specific code based on the result of a condition (i.e., true or false). Types of conditional construct in python is if...else and loop
38. If....else... If. It is also known as Conditional Construct,
which executes its statement according to the given test expression. If the condition is true, then it executes a specific set of code or otherwise
the false statement code. It returns the boolean value, i.e., true or false.
For example:
if(Test expression):
True Statements code
else:
False Statement Code

39. Loops/Iteration. Loop is used to execute the statements multiple times till the condition is not false. In python, we have 2 types of loops:
while and for loop.

40. While loop (while :) while. loop is used for repeated execution as long as an expression (condition) is true.
The syntax of while loop is:
"While" expression “:”
Suite-1
["else" ";"
Suite-2
]
Here
(i) the expression is a Boolean expression which evaluates to True or False. Such an expression represents a condition.
(ii) else is an optional clause of while loop

41. Statements required to execute loop:


. Control variable
. Initialization statement
. Update statement

42. Control variable. If the looping condition (expression specified in the while statement) checks the value of a single variable, then this
variable is generally called the control variable. For example, in the above code, the looping condition checks the value of variable 1. So, iis
the control variable in the above loop.

43. Initialization statement. The value of control variable should be initialized to a particular value before the loop starts. The statement to do
so is called initialization statement. For example, 1-1 is the initialization statement in the above code.

44. Update statement. The value of control variable is updated in the loop so that the looping condition becomes false after some time. The
statement to do this is called the update statement. Without an update statement, the loop becomes an infinite loop. In the above code i+-1 is
the update statement.

45. For Loop. for loop is used for repeated execution of the elements of a sequence. In its simplest form, the syntax of for loop is:
"for" variable "in" "range (" start "," stop "," step ")" ":"
Suite-1
["else" “:”
Suite-2
]
Here:
(a)Variable is any variable name.
(b) range() is a built-in function that produces a sequence of integers from start (inclusive) to stop (exclusive), by step. Range() can be used in
three forms:
(i) with one parameter: range (stop) produces a sequence of integers from 0 to stop-1 in steps of 1. For example: range (10) produces a
list of integers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
(ii) With two parameters: range (start, stop) produces a sequence of integers from start to stop-1 in steps of 1. For example: range (2,10)
produces a list of integers 2, 3, 4, 5, 6, 7, 8, 9.
(iii) With three parameters: range (start, stop, step) produces a sequence of integers from start to stop-1 in steps of step. For example: range
(2,10,3) produces a list of integers 2, 5, 8.
(c) else is an optional clause of for loop.

46. range (). It is a built-in function which generates the sequence in the range which is written as the parameter. By default, its lower limit is an
inclusive and upper limit is excluded.
For example:
>>> list(range 1,5))
47. break. The break statement prematurely ends execution of the current while or for loop. It brings the control to the statement immediately following the
current control structure, skipping the optional "else" clause, if the loop has one.

48. continue. The continue statement is used to skip the execution of the current iteration of a loop and continues with the next. continue does
not terminate the loop, but continues with the next iteration of the loop.

49. Module. It is a python file (.py) containing definitions and statements. A module can define functions, classes and variables.

50. Math Module. It is used to access the mathematical functions defined by the C standard. To use mathematical functions, we need to
import the module import math.
51. Random module(). It is used to generate the random number.
.random.random()
This function returns the next random floating point number in the range (0.0, 1.0).
>>>random.random()

Output:
0.4927384121240921

>>>for i in range (5):


print (random.random(), end="")

Output:
0.49711110901310307 0.6659246064728136 0.9832004894846276 0.6364627700802349
0.7764847536554754
. random. randrange(stop) or random.randrange (start, stop, step)

This function returns a randomly selected element from a range(start, stop, step).
For example:
>>> for i in range(5):
print (random.randrange (1,10,2), end="")

output:
15719
>>> for i in range(5):
print (random.randrange (1,10), end="")
output: 24899
⚫ random.randint(a, b)
This function returns a random integer N such
that a <= N <= b.
For example:
>>> for i in range(5):
print (random.randint (1,10), end=" ")
Output:
6 10 3 2 2
52. Syntax error. A syntax error is a grammatical error in a script. All the statements written in a programming language must exactly follow the
grammatical rules of that language. If a statement does not follow the rules, the interpreter cannot interpret it and shows a Syntax error.
Examples of syntax errors are: mismatching parentheses or quotation marks, unnecessary indentation etc. Syntax errors are caught by the
interpreter and a script with syntax errors does not execute at all. Example.
>>> str=Input("Enter") #input I is capital, ie. Syntax error
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module> str-Input("Enter")
Name Error: name 'Input' is not defined
53. Run time error. An error which occurs during program execution due to incorrect or illegal values used in some operations. Examples of run-
time errors are: division by zero, finding log of a negative number etc. Run-time errors are also called exceptions and there are ways to handle
exceptions properly. Without proper
SOLVED QUESTION BANK
Objective Type Questions
1 Mark
1. Python Language was first developed in
(a) USA
(b) UK
(c) Netherlands
(d) India
Ans. (c)
exception handling, a run-time error causes the script
to terminate.
>>> x=5
>>> y=0
>>> x/y
#Number cannot be divided by zero
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module> x/y
ZeroDivisionError: division by zero
54. Logical errors. These are the errors in the logic of the script. Examples of logical errors are incorrect sequence of statements in the script,
errors in the formulae used, incorrect initialization of variables etc.
>>> x=25
>>> X #Logical Error, Capital X is not defined Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module> X
NameError: name 'X' is not defined
55. Exceptions. An exception is a run-time error in a script. When an exception occurs in a script, the execution of the script is abruptly
terminated. Types of exceptions:

You might also like