Python-Basic IQ

How will you delete a variable?
# Assigning a value to x  
x = 60  
print(x)  
# deleting a variable.   
del x  
print(x) 
How will you print Single and Multiple Variables in Python?
a = 5  
b = 6  
# printing single value   
print(a) 
# printing multiple variables  
print(a,b)
What are tokens in Python?

Keywords.
Identifiers.
Literals.
Operators.

Why do we not define the type of the variable while declaring it?

Python is a dynamically typed language. That’s why we do not define the variable type while declaring it.
The interpreter implicitly binds the value with its type.

What is purpose of type() function?

It returns the type of the variable.

a=10  

print(type(a)) 

<type ‘int’>

What are Standard data types?

Numbers-Integer,Complex Number,Float

Sequence Type-Strings,List,Tuple

Boolean

Set

Dictionary

What is Elif?

It is short for else-if.

Program to print the largest of the two numbers.

a = int(input("Enter a? "));
b = int(input("Enter b? "));
if a>b :
print("a is largest");
if b>a :
print("b is largest");
Program to check whether a number is even or not.
num = int(input("enter the number?"))  
if num%2 == 0:  
    print("Number is even...")  
else:  
    print("Number is odd...") 
 Explain range() function

It is used in python

  • to generate the sequence of the numbers.
  • If it is range(10) then we get the numbers from 0 to 9.
  • Syntax:
  • range(start,stop,step size)
  • The start -the beginning of the iteration.
  • The stop – the loop will iterate till stop-1.
  • The range(1,10) will generate numbers 1 to 9 iterations.
  • The step size – to skip the specific numbers from the iteration.
  • It is not mandatory to use. By default, the step size is 1. It is optional.
Explain for loop in different ways.

way 1: 
str = "Java"  
for i in str:  
    print(i)  
J
a
v
a

way2:
list = [1,2,3]  
n = 10  
for i in list:  
    c = n*i  
    print(c)

10
20
30

way3:
for i in range(5):  
    print(i,end = ' ') 
	
0 1 2 3 4

How will you Create String in python?
Creating String in Python

#Using single quotes  
str1 = 'Hello clouvikas'  
print(str1)

#Using double quotes  
str2 = "Hello clouvikas"  
print(str2)

#Using triple quotes  
str3 = '''''python'''   
print(str3)
How will you access character H in Hadoop string?

str ="Hadoop"
print(str[0])--H
print(str[0:])--Hadoop  
# Starts 1th index to 4th index  
print(str[1:5])--adoo  
# Starts 2nd index to 3rd index  
print(str[2:4])  --do
# Starts 0th to 2nd index  
print(str[:3])--Had
print(str[-1])  --P
print(str[-2:]) --op
print(str[-3])  --0
print(str[-4:-1])--doo
How can we use format method?
format method can be used following ways:

# Using Curly braces  
print("{} and {} are programming languages".format("python","java"))  
--python and java are programming languages
  
#Positional Argument  
print("{1} and {0} best players ".format("vikas","ram"))  
--ram and vikas best players
  
#Keyword Argument  
print("{a},{b},{c}".format(a = "ram", b = "vikas", c = "mohan")) 
--ram,vikas,mohan 
How is memory managed in Python?

Memory management is handled by the Python Memory Manager.
private heap space: The Python Memory Manager allocates memory in private heap space.
All Python objects are stored in private heap space and it used to be private.
Python has an in-built garbage collection to recycle the unused memory for the private heap space.

What is init?

init is a constructor method in Python and is automatically called to allocate memory when a new object/instance is created. All classes have a init method associated with them. It helps in distinguishing the methods and attributes of a class from local variables.

Example of class definition

class Student:
def init(self, fname, lname, age, section):
self.firstname = fname
self.lastname = lname
self.age = age
self.section = section

creating a new object

stu1 = Student(“vikas”, “rajan”, 22, “A2”)