0% found this document useful (0 votes)
7 views

Python Interview

python interview question

Uploaded by

prajwalpawar100k
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python Interview

python interview question

Uploaded by

prajwalpawar100k
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1)Python Data type :-

#premetive :
1)int 2)float 3)Bolen 4)str
#non primitive:-
1)list:[]
2)set:{1,2,3,4,5}
3)tuple:()
4)dictionary:{keys:values}
5)frozen set
2)type casting :
to convert one Datatype in another , int, eval

3)condition Statement :
1) if, else,

if condition:
print("condition is true")
else:
print("condition is false")

2)Nested if else loop,


If i==0:
Print(i)
If i==1:
Print(i)
3) else if ladder loop :
if condition:
print("condition is true")
elif condition:
print("condition is false")
else:
print("not define")
1)for loop
range (start,stop,step)
for i range ()
pass/ or print()

# in if loop the value is increase automatic without changing


for i in range(1,11,1) #(start,stop,step)
print(i)
here i will increase by one

#if we want to increase existing value


for i in range(1,11)
temp=1
temp=1+i

#list:
-list is coma seprate value in square bracket []
-mutable
-heterogenous [ 1,"a",1.1,"pra"]
-order
-allow dublicate values

# access element in list


1)forword -
"from 0 - size-1"

2)back-word
from -1 to until finish from right to left
[-5 -4 -3 -2 -1] #index backword
[10,20,30,40,50]
[1 , 2 , 3 ,4 , 5] #index forword
I)indixing - only acces one element
//syntex
variablename[indexvalue]
l[0]=1
l[-1]=5

II) slicing - to access multiple element

//syntex
variablename[start:stop]
l[1:4]

# methods of list to insert elememnt in list


1)append

variablename.append() /// at last position


2)insert
variablename.insert(index, value)

3) update
var[index]=newvalue
eg l[2]=66
4)remove
remove through value or number
var.remove(value)
l.remove(33)

5)pop
remove by the index number
var.pop(index)
l.pop(0)
6) delet
del var[index]
del l[2]
#iterate in list
n=[]
for i in n:
print(i)

#how to acces data of dictonary


1)access data of dictionary using key
dictname[key];

#add data in dictonary


dictionary[key]=value

#method of dictionary
1)key()
-it returns list of keys only
print(dictname.keys())
2)value()
-it returns list of values only
print(dictname.values())

3)items()
-it returns list of key value pairs

#set
union()
intersetion()- common element
difference()-it show
1)list :-
duplicate is not allow
mutable
order
[]
2)tuples
duplicate allow
immutable
order
()

1. What is Python?
Python is a high-level, interpreted programming language known for its clear syntax, readability, and
versatility. It supports multiple programming paradigms, including procedural, object-oriented, and
functional programming. Python is widely used for web development, data analysis, artificial
intelligence, machine learning, automation, and more.

2. Applications of Python
- Web Development : Frameworks like Django and Flask.
- Data Science : Libraries such as Pandas, NumPy, and Matplotlib for data manipulation and
visualization.
- Machine Learning : Libraries like TensorFlow and Scikit-Learn for building predictive models.
- Automation/Scripting : Automating repetitive tasks and writing scripts for system management.
- Game Development : Libraries like Pygame for creating games.
- Internet of Things (IoT) : Used in Raspberry Pi for various IoT applications.

3. How to Check the Data Type of a Variable


You can use the `type()` function to check the data type of a variable:
x = 10
print(type(x)) # Output: <class 'int'>

5. Looping with if-else


You can use a `for` loop or a `while` loop in combination with `if-else` statements. Here’s an example
using a `for` loop:
for i in range(5):
if i % 2 == 0:
print(i, "is even")
else:
print(i, "is odd")

7. How to Add Data to a List


You can add data to a list using the `append()` method or the `insert()` method:
my_list = [1, 2, 3]
my_list.append(4) # Adds 4 to the end of the list
my_list.insert(0, 0) # Inserts 0 at index 0

8. Difference Between `pop()` and `remove()`


- `pop(index)` : Removes and returns an item at the specified index. If no index is specified, it
removes and returns the last item.
my_list = [1, 2, 3]
my_list.pop(1) # Removes and returns 2

- `remove(value)` : Removes the first occurrence of the specified value.


my_list.remove(2) # Removes the first occurrence of 2

9. What is Indexing?
Indexing refers to accessing elements in a sequence (like a list or string) using their position, which
starts from 0. For example:
my_list = [10, 20, 30]
print(my_list[1]) # Output: 20

10. What is Slicing?


Slicing is a technique to access a subset of elements in a sequence by specifying a start and end index:
my_list = [1, 2, 3, 4, 5]
print(my_list[1:4]) # Output: [2, 3, 4]
11. What is a List?
A list is a mutable collection in Python that can hold an ordered collection of items, which can be of
different types. Lists are defined using square brackets:

my_list = [1, 'apple', 3.14]

12. Can We Add a New Key to a Dictionary?


Yes, you can add a new key to a dictionary simply by assigning a value to it:

my_dict = {'a': 1, 'b': 2}


my_dict['c'] = 3 # Adds a new key 'c'

13. How to Update a Dictionary Value


We can update the value of an existing key by assigning a new value:
my_dict['a'] = 10 # Updates the value of key 'a' to 10

14. What is a Dictionary?


A dictionary is an unordered, mutable collection of key-value pairs in Python. It is defined using curly
braces:
my_dict = {'key1': 'value1', 'key2': 'value2'}

15. Methods of a Dictionary


Common dictionary methods include:
- `keys()`: Returns a view of the dictionary’s keys.
- `values()`: Returns a view of the dictionary’s values.
- `items()`: Returns a view of the dictionary’s key-value pairs.
- `get(key)`: Returns the value for the specified key, or `None` if the key does not exist.
- `pop(key)`: Removes and returns the value for the specified key.

16. Delete Data in a Dictionary


You can delete an entry from a dictionary using the `del` statement
the `pop()` method:
del my_dict['key1'] # Deletes key1
my_dict.pop('key2') # Deletes key2 and returns its value

17. How to Access an Element in a Dictionary


We can access an element in a dictionary by referencing its key:
value = my_dict['key1'] # Retrieves the value associated with 'key1'

21. What are Mutable and Immutable Data Types?


- Mutable : These data types can be changed after they are created. Examples include lists,
dictionaries, and sets.
- Immutable : These data types cannot be changed once created. Examples include tuples, strings, and
integers.

22 difference between tuple and list &set &dictionary

TUPLE LIST Dictionary SET

# duplicate allow #duplicate is not allow #duplication not allow #duplication not
allow

# immutable # mutable # mutable #mutable

# order # order #ordered # unordered

# () not mandatory likely # [] mandatory with #{} define in curly #{} coma
we can defin as t coma separated value bracket with key and separated value in
=”string” value curly bracket

#less memory required #required more memory #more memory #required less
required memory
#fast speed #slow speed # slower speed # faster

#fix value or data #changeable value #The keys are fixed # fix value or data
once created
#it support unpacking #support only #Supports unpacking #Supports
and packing unpacking for keys, values, or unpacking for
both. elements.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy