0% found this document useful (0 votes)
2 views21 pages

Python Questions

Python is a high-level programming language developed by Guido van Rossum in 1991, used in various applications such as web development, data science, and AI. It features operators, functions, and data structures like strings, tuples, sets, and dictionaries, and supports both built-in and external libraries like NumPy and Tkinter. Key programming concepts include OOP principles such as encapsulation, abstraction, inheritance, and polymorphism.

Uploaded by

game2580bgmi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views21 pages

Python Questions

Python is a high-level programming language developed by Guido van Rossum in 1991, used in various applications such as web development, data science, and AI. It features operators, functions, and data structures like strings, tuples, sets, and dictionaries, and supports both built-in and external libraries like NumPy and Tkinter. Key programming concepts include OOP principles such as encapsulation, abstraction, inheritance, and polymorphism.

Uploaded by

game2580bgmi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. What is python?

2. what are the applications in python?


3. Who developed python?
4. Why we called python?
5. What are operators in python?
6. What is function in python?
7. What is string in python and what are the types in python?
8. What is tupple in python?
9. What is set and dictionary in python?
10. What is slicing in string in python?
11. What is mutable andimmutable?
12. What are the methods in python?
13. What is numpy in python?
14. What is tkinter in python?
15. What is maths in python?
16. How many types libraries in python?
17. What are the special methods in tkinter in python?
18. What array in python?
19. What is the use of dict?
20. What are difference between in set and frozenset in python?
1. What is Python?

Python is a high-level, easy-to-learn programming language used to build software, websites,


apps, and AI systems.

2. What are the applications in Python?

Python is used in:

 Web development
 Data science
 Machine learning & AI
 Automation
 Game development
 Cybersecurity
 Desktop applications

3. Who developed Python?

Python was developed by Guido van Rossum in 1991.

4. Why is it called Python?

It is named after the TV comedy show “Monty Python’s Flying Circus”.

5. What are operators in Python?

Operators are symbols used to perform operations.

Types:

 Arithmetic: + - * /
 Comparison: == != > <
 Logical: and or not
 Assignment: = += -=

6. What is a function in Python?

A function is a block of code used to perform a specific task.

Example:

def hello():
print("Hi")

7. What is string in Python and types?

A string is a sequence of characters.

Types:

 Single quotes: 'hello'


 Double quotes: "hello"
 Triple quotes: '''hello'''

8. What is tuple in Python?

A tuple is an ordered collection that cannot be changed (immutable).

Example:

t = (1, 2, 3)

9. What is set and dictionary in Python?

Set:

 Unordered collection
 No duplicate values

Dictionary:
 Stores data in key-value pairs

Example:

s = {1, 2, 3}
d = {"name": "Ram"}

10. What is slicing in string?

Slicing means extracting part of a string.

Example:

text = "Python"
print(text[0:3]) # Pyt

String Slicing in Python (Detailed Explanation)

String slicing means extracting a part (substring) from a string using index positions.

Basic Idea

In Python, every character in a string has an index number:

String: P Y T H O N
Index: 0 1 2 3 4 5

We use these indexes to cut (slice) the string.

Syntax of String Slicing


string[start : end : step]

Meaning:

 start → where to begin (included)


 end → where to stop (not included)
 step → jump size (optional)
1. Basic Slicing
text = "Python"

print(text[0:3])

Output:

Pyt

Starts at index 0, stops before 3

2. Slicing without start


text = "Python"

print(text[:4])

Output:

Pyth

Starts from beginning automatically

3. Slicing without end


text = "Python"

print(text[2:])

Output:

thon

Goes till the end

4. Negative Index Slicing

Negative index means counting from the end:


P Y T H O N
-6 -5 -4 -3 -2 -1
text = "Python"

print(text[-4:-1])

Output:

tho

11. What is mutable and immutable?

 Mutable → can be changed (list, set, dict)


 Immutable → cannot be changed (string, tuple)

[Link] (Definition)

A mutable object is an object whose value can be changed after it is created.

Meaning: You can modify the same object in memory.

Examples of mutable types:

 list
 dictionary
 set

Example:

numbers = [1, 2, 3]
numbers[0] = 100

print(numbers)

Output:

[100, 2, 3]

The list is changed (modified)


2. Immutable (Definition)

An immutable object is an object whose value cannot be changed after it is created.

Meaning: If you try to change it, a new object is created.

Examples of immutable types:

 string
 tuple
 int
 float

Example:

name = "Python"
name[0] = "J"

12. What are methods in Python?

Methods are functions that work with objects.

Example:

[Link]()

Types of Methods in Python

1. String Methods

Used to work with strings.

Examples:

text = "hello"

print([Link]()) # hello
print([Link]()) # HELLO
print([Link]()) # Hello
2. List Methods

Used to modify lists.

Examples:

numbers = [1, 2, 3]

[Link](4) # add element


[Link](2) # remove element

print(numbers)

Output:

[1, 3, 4]

Python List Methods: add, append, extend, remove, del (Detailed Explanation)

These are used to add, remove, or modify elements in a list.

1. append() method

👉 Adds one element at the end of the list

Syntax:

[Link](element)

Example:

numbers = [1, 2, 3]
[Link](4)

print(numbers)

Output:

[1, 2, 3, 4]

Adds only single item


2. extend() method

👉 Adds multiple elements at the end of list

Syntax:

[Link](iterable)

Example:

numbers = [1, 2, 3]
[Link]([4, 5, 6])

print(numbers)

Output:

[1, 2, 3, 4, 5, 6]

Adds many items at once

3. add() method

Used in sets only, not lists


Adds a single element to a set

Syntax:

[Link](element)

Example:

s = {1, 2, 3}
[Link](4)

print(s)

Output:

{1, 2, 3, 4}
4. remove() method

Removes a specific element from list

Syntax:

[Link](value)

Example:

numbers = [1, 2, 3, 4]
[Link](3)

print(numbers)

Output:

[1, 2, 4]

If value not found → error occurs

5. del keyword

Used to delete:

 element
 or entire list

Syntax:

del list[index]

Example 1 (delete item):

numbers = [1, 2, 3, 4]
del numbers[1]

print(numbers)

Output:

[1, 3, 4]
Example 2 (delete full list):

numbers = [1, 2, 3]
del numbers

3. Dictionary Methods

Used with key-value data.

Examples:

student = {"name": "Anu", "age": 20}

print([Link]())
print([Link]())

4. Set Methods

Used for set operations.

Examples:

s = {1, 2, 3}

[Link](4)
[Link](2)

print(s)

5. Built-in Functions vs Methods

Function Method

Independent Belongs to object

Example: len(list) Example: [Link]()

13. What is NumPy in Python?


NumPy is a library used for numerical operations and arrays.

14. What is Tkinter in Python?

Tkinter is a library used to create GUI (Graphical User Interface) applications like windows and
buttons.

Tkinter __init__() in Python (Detailed Explanation)

In Tkinter, __init__() is a constructor method used inside a class to initialize (set up) the
GUI application when an object is created.

It is mainly used when we build Tkinter apps using Object-Oriented Programming (OOP).

What is __init__()?

__init__() is a special method (constructor) in Python that runs automatically when a class
object is created.

In Tkinter:

 It creates the main window


 Sets title, size, widgets (buttons, labels, etc.)
 Initializes the GUI setup

Basic Structure of Tkinter with __init__()


import tkinter as tk

class MyApp:
def __init__(self, root):
[Link] = root
[Link]("My Tkinter App")
[Link]("300x200")

label = [Link](root, text="Hello Tkinter")


[Link]()

root = [Link]()
app = MyApp(root)
[Link]()

15. What is math in Python?

Math is a built-in module used for mathematical functions.

Example:

import math
[Link](25)

16. How many types of libraries in Python?

Two main types:

 Built-in libraries (math, os, random)

Built-in Libraries (Standard Libraries) 🧰

These come already installed with Python, so you don’t need to install them separately.

Examples:

 math
 os
 random

a) math library

Used for mathematical operations.

Example:

import math

print([Link](25)) # square root


print([Link](5))
print([Link])

Used in calculations, engineering, science problems.

b) os library

Used to interact with the operating system (files, folders).

Example:

import os

print([Link]()) # current working directory


[Link]("test_folder") # create folder

Used in file handling, system tasks.

c) random library

Used to generate random values.

Example:

import random

print([Link](1, 10)) # random number between 1 and 10

Used in games, OTP generation, simulations.

 External libraries (numpy, pandas, tensorflow)

External Libraries 🧰

These are not built-in, you must install them using pip.

pip install numpy pandas tensorflow

a) NumPy
Used for numerical computing and arrays.

Example:

import numpy as np

arr = [Link]([1, 2, 3])


print(arr)

Used in:

 Data science
 Scientific calculations
 Matrix operations

b) Pandas

Used for data analysis and working with tables (like Excel).

Example:

import pandas as pd

data = {"Name": ["A", "B"], "Age": [20, 21]}


df = [Link](data)

print(df)

Used in:

 Data analysis
 Excel-like data handling
 Machine learning preprocessing

c) TensorFlow

Used for Artificial Intelligence and Machine Learning.

Example:
import tensorflow as tf

print(tf.__version__)

Used in:

 AI models
 Deep learning
 Image recognition
 Chatbots

17. What are special methods in Tkinter?

Important Tkinter methods:

 mainloop() → runs program


 Label() → displays text
 Button() → creates button
 Entry() → input box
 pack(), grid(), place() → layout methods

18. What is array in Python?

An array is a collection of similar data types.


In Python, we use lists or NumPy arrays.

Example of Array in Python

In Python, we usually use lists or NumPy arrays as arrays.

1. Using List as an Array (Most common)

numbers = [10, 20, 30, 40, 50]

print(numbers)
print(numbers[0]) # first element
print(numbers[2]) # third element

2. Using Array module

import array

arr = [Link]('i', [1, 2, 3, 4, 5])

print(arr)
print(arr[1])

'i' means integer type array.

3. Using NumPy Array (Advanced)

import numpy as np

arr = [Link]([1, 2, 3, 4, 5])

print(arr)

19. What is the use of dict?

Dictionary is used to store data in key-value pairs, useful for storing structured data like student
info.

A dictionary is written using curly braces {} with key : value pairs.

Basic Syntax:

dict_name = {
key1: value1,
key2: value2,
key3: value3
}

Example:
student = {
"name": "Anu",
"age": 20,
"course": "Python"
}

20. Difference between set and frozenset:

Set Frozenset
Mutable Immutable
Can add/remove items Cannot change
Written as {} Written as frozenset()

21. OP Concepts in Python (Object-Oriented Programming)

OOP is a programming style where we organize code using objects and classes. Python supports
OOP fully.

There are 4 main OOP concepts:

1. Encapsulation

Encapsulation means wrapping data and methods together in one unit (class) and restricting
direct access to data.

Purpose:

 Protect data from outside modification


 Control access using methods

Example:

class Bank:
def __init__(self, balance):
self.__balance = balance # private variable

def get_balance(self):
return self.__balance

b = Bank(1000)
print(b.get_balance())

__balance is private (cannot be accessed directly)

2. Abstraction

Abstraction means hiding internal implementation and showing only necessary details.

Purpose:

 Hide complex logic


 Show only important features

Example:

from abc import ABC, abstractmethod

class Car(ABC):
@abstractmethod
def start(self):
pass

User only knows “start”, not how engine works internally

3. Inheritance

Inheritance means one class inherits properties of another class.

Purpose:

 Code reusability

Example:
class Animal:
def sound(self):
print("Animal sound")

class Dog(Animal):
pass

d = Dog()
[Link]()

Dog inherits Animal properties

4. Polymorphism

Polymorphism means same method name, different behavior.

Purpose:

 One name, multiple forms

Example:

class Bird:
def sound(self):
print("Chirp")

class Dog:
def sound(self):
print("Bark")

b = Bird()
d = Dog()

[Link]()
[Link]()

🧰 Summary Table
Concept Meaning Purpose

Encapsulation Data hiding Protect data


Concept Meaning Purpose

Abstraction Hide complexity Show only essentials

Inheritance Parent-child relationship Code reuse

Polymorphism Many forms of same method Flexibility

You might also like