What is python interpreter?
A python interpreter is a computer program that converts each high-level
program statement into machine code. An interpreter translates the
command that we write out into code that the computer can understand.
Python is an interpreted language because the source code of a Python program is
converted into bytecode that is then executed by the Python virtual machine.
What is Atoms in python?
In python, an atom is something that has a value. Identifiers, Literals, strings,
lists, tuples, sets, dictionaries etc. are all atoms.
Python Relational Operators
Relational operators are used to compare two values:
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Python Logical Operators
Logical operators are used to combine conditional statements:
Operator Description Example
and Returns True if both statements are x < 5 and x < 10
true
or Returns True if one of the x < 5 or x < 4
statements is true
not Reverse the result, returns False if not(x < 5 and x < 10)
the result is true
Data types in Python
Data-types specifies the type and size of values that can be stored in a variable
name. Variables in python can hold values, and every value has a data-type.
Python is a dynamically typed language; hence we do not need to define the
type of the variable while declaring it. The interpreter implicitly binds the value
with its type.
The data types defined in Python are given below.
1. Numbers
2. Sequence Type
3. Boolean
4. Set
5. Dictionary
Indentation in Python
Indentation refers to the spaces at the beginning of a code line. Python uses indentation to
indicate a block of code.
Modules:
A module is a collection of functions, classes and datastructure, statements etc. written as a
python code.
For example, In math module there is a collection of a large number of built-in mathematical
function – trigonometric, exponential, logarithmic etc.
To import any module write the following statement-
import module_name
Or import module_name as name
for loop in Python
If we want to do something repeatedly, we put the statements inside a for
loop. The general form of for loop is:
for x in range(begin, end, step) :
statement1
statement2
………………..
The statements in the loop are executed repeatedly for the values of x starting
from begin to end-1 with step.
Function in Python:
Python function is a block of statements that returns the specific task. We can pass data, known as
parameters, into a function. A function can return data as a result.
The syntax to declare a function is
def function_name(parameters):
statements //body of the function
return expression
Write a python program to find factorial n using function.
def factorial(n):
p=1
for i in range(1,n+1):
p=p*i
return p
n=int(input('Enter the value of n :'))
p=factorial(n) //function calling
print('Factorial = ',p)
Write a python program to check a number is prime or not.
n=int(input('Enter the value of n :'))
t=1
m=int(n/2)
for i in range(2,m+1):
if n%i == 0:
t=0
break
if t==1:
print(n,'is prime')
else:
print(n,'is not prime')
Why Python is an interpreted language?
Python is an interpreted language because the source code of a Python program is
converted into bytecode that is then executed by the Python virtual machine. Python
is different from major compiled languages, such as C and C + +, as Python code is
not required to be built and linked like code for these languages.
What is Python variable?
A Python variable is a symbolic name that is a reference or pointer to an object.
Once an object is assigned to a variable, you can refer to the object by that name.
What is Python Shell
The Python Shell is the interpreter that executes our Python programs, other pieces of
Python code, or simple commands.
Differences between List and Tuple
Both lists and tuples can store any data such as integer, float, string, and
dictionary. Lists and Tuples are similar in most factors but following are the main
difference between them.
List Tuple
Lists are mutable. Tuples are immutable.
Iteration in lists is time consuming. Iteration in tuples is faster
Tuples are appropriate for accessing the
Lists are better for insertion and deletion operations
elements
Lists consume more memory Tuples consume lesser memory
Tuples have comparatively lesser built-in
Lists have several built-in methods
methods.
Tuples operations are safe and chances of error
Lists are more prone to unexpected errors
are very less
Creating a list is slower because two memory blocks
Creating a tuple is faster than creating a list.
need to be accessed.
Python break and continue Statement
The break statement is used to terminate the loop immediately when it is
encountered. The syntax of the break statement is:
break
The continue statement is used to skip the current iteration of the loop and the
control flow of the program goes to the next iteration. The syntax of
the continue statement is:
continue