- Introduction to Python
- Execution Flow of Python Program
- Data Types in Python
- Type Casting in Python
- Frequently Asked Programs
- Output-Based Questions
- Viva Questions
- Quick Revision
Python is a high-level, interpreted, general-purpose, object-oriented, and dynamically typed programming language.
It was developed by Guido van Rossum and officially released in 1991.
Python is one of the most popular programming languages because of its simple syntax, readability, and powerful built-in libraries.
It is widely used in:
- Web Development
- Artificial Intelligence
- Machine Learning
- Data Science
- Automation
- Cloud Computing
- Cyber Security
- Software Development
- High-Level Language
- Interpreted Language
- Open Source
- Platform Independent
- Object-Oriented
- Dynamically Typed
- Easy to Learn
Python was developed by Guido van Rossum.
He started working on Python in the late 1980s at CWI (Centrum Wiskunde & Informatica) in the Netherlands.
Python was officially released in 1991.
Python is called a High-Level Language because it is easy for humans to read and write.
The programmer does not need to worry about low-level memory management or hardware-specific instructions.
Python provides simple English-like syntax, making programming easier.
Example:
print("Hello World")This is much simpler than writing equivalent code in low-level languages.
Python is called an interpreted language because its source code is executed by the Python Interpreter instead of being directly compiled into machine code.
Execution Process:
Python Source Code (.py)
│
▼
Python Interpreter
│
▼
Bytecode (.pyc)
│
▼
Python Virtual Machine (PVM)
│
▼
Machine Code
│
▼
Output
- Easier debugging
- Platform independent
- Faster development
- No manual compilation required
Python provides many powerful features.
Some important features are:
- Simple and Easy to Learn
- High-Level Language
- Interpreted Language
- Object-Oriented
- Dynamically Typed
- Platform Independent
- Portable
- Open Source
- Large Standard Library
- Automatic Memory Management
- Extensible
- Embeddable
Dynamic Typing means the programmer does not declare the data type of a variable.
Python automatically determines the data type based on the assigned value.
Example
age = 22
name = "Basha"
percentage = 91.5Python automatically identifies:
- age → int
- name → str
- percentage → float
Platform Independence means a Python program written on one operating system can also run on another operating system without modifying the source code.
For example,
A Python program written on:
- Windows
can also run on
- Linux
- macOS
provided that Python is installed.
This is possible because Python uses the Python Virtual Machine (PVM).
Portability means a Python program can be moved from one platform to another platform with little or no modification.
Example:
A Python program developed on Windows can be copied and executed on Linux or macOS.
Open Source Software is software whose source code is freely available to the public.
Anyone can:
- View the source code
- Modify the source code
- Improve the software
- Distribute the software
Python is an open-source programming language.
Python is popular because:
- Easy syntax
- Easy to learn
- Huge community support
- Rich standard library
- Used in AI and Machine Learning
- Used in Automation
- Used in Web Development
- Used in Data Science
- Cross-platform support
Python is used in many domains.
Examples include:
- Web Development
- Software Development
- Desktop Applications
- Data Science
- Artificial Intelligence
- Machine Learning
- Automation
- Robotics
- Cloud Computing
- Networking
- Cyber Security
- Game Development
Python is generally known as an interpreted language.
However, internally Python first compiles the source code into Bytecode (.pyc file), and then the Python Virtual Machine (PVM) interprets the bytecode and executes it.
Therefore, Python uses both compilation (to bytecode) and interpretation (by the PVM).
Python is continuously updated with new releases.
Instead of memorizing a specific version number, it is better to answer:
Python is actively maintained by the Python Software Foundation (PSF), and newer versions are released regularly with performance improvements, new features, and bug fixes.
This answer remains accurate even as new versions are released.
PVM stands for Python Virtual Machine.
It is responsible for executing the bytecode generated by the Python Interpreter.
PSF stands for Python Software Foundation.
It is a non-profit organization responsible for maintaining, developing, and promoting the Python programming language.
The PSF also manages Python releases and supports the Python community worldwide.
- Python was developed by Guido van Rossum.
- First released in 1991.
- Python is high-level and interpreted.
- Python is dynamically typed.
- Python is platform independent.
- Python is portable.
- Python is open source.
- Python uses the Python Virtual Machine (PVM).
- Python is maintained by the Python Software Foundation (PSF).
The execution flow of a Python program describes how Python converts the source code into executable instructions and produces the final output.
The execution process consists of the following steps:
- Write the Python source code (
.pyfile). - The Python Interpreter checks the source code for syntax errors.
- The source code is compiled into Bytecode.
- The generated Bytecode is stored as a
.pycfile. - The Python Virtual Machine (PVM) executes the Bytecode.
- The operating system converts the instructions into machine code.
- The final output is displayed.
Python Program (.py)
│
▼
Python Interpreter
│
▼
Bytecode (.pyc)
│
▼
Python Virtual Machine (PVM)
│
▼
Operating System
│
▼
Hardware
│
▼
Output
Source code is the program written by the programmer using Python syntax.
A Python source code file is saved with the .py extension.
Example:
print("Hello World")File Name
hello.py
A Python Interpreter is software that reads, checks, compiles, and executes Python programs.
Its responsibilities include:
- Reading the source code.
- Checking for syntax errors.
- Converting the source code into Bytecode.
- Passing the Bytecode to the Python Virtual Machine (PVM).
- Easy debugging.
- Immediate execution.
- Platform independent.
- No separate compilation step required.
Bytecode is the intermediate code generated by the Python Interpreter after compiling the source code.
Bytecode is:
- Platform independent.
- Not machine code.
- Executed by the Python Virtual Machine (PVM).
Source Code
↓
Bytecode
↓
Machine Code
A .py file is a Python source code file.
It contains the program written by the programmer.
Example
student.py
calculator.py
employee.py
A .pyc file is a compiled Python Bytecode file.
It is automatically generated by Python after successful compilation.
Advantages:
- Faster execution.
- No need to compile the same source code repeatedly.
- Improves program loading time.
The Python Virtual Machine (PVM) is the runtime engine responsible for executing Python Bytecode.
The PVM reads the Bytecode instruction by instruction and executes it.
- Executes Bytecode.
- Converts Bytecode into machine-level instructions.
- Produces the final output.
Yes.
The Bytecode generated by Python is platform independent, but the PVM implementation is available separately for each operating system.
This enables the same Python program to run on Windows, Linux, and macOS.
Python generates Bytecode because:
- It improves execution speed.
- It avoids recompiling unchanged source code.
- It provides platform independence.
- It acts as an intermediate representation before execution.
If a syntax error is present:
- The interpreter detects the error during compilation.
- Bytecode is not generated.
- The program execution stops.
- Python displays an appropriate error message.
Example
print("Hello"Output
SyntaxError: '(' was never closed
| Source Code | Bytecode |
|---|---|
| Written by the programmer | Generated automatically by Python |
| Human-readable | Intermediate code |
Saved as .py |
Saved as .pyc |
| Editable | Not intended for editing |
| Executed after compilation | Executed by the PVM |
| Bytecode | Machine Code |
|---|---|
| Generated by Python | Generated by the operating system/processor |
| Platform independent | Platform dependent |
| Executed by the PVM | Executed directly by the CPU |
| Intermediate code | Binary instructions |
Yes.
Python internally compiles the source code into Bytecode before execution.
Therefore, Python uses both:
- Compilation (Source Code → Bytecode)
- Interpretation (Bytecode → Execution by PVM)
Python is commonly called an interpreted language because the Python Virtual Machine (PVM) executes the Bytecode.
Although Python first compiles the source code into Bytecode, the Bytecode is interpreted during execution.
Hence, Python follows a hybrid execution model.
The complete execution architecture is:
Programmer
│
▼
Python Source Code (.py)
│
▼
Python Interpreter
│
▼
Bytecode (.pyc)
│
▼
Python Virtual Machine
│
▼
Operating System
│
▼
Hardware
│
▼
Output
.py→ Source Code.pyc→ Bytecode- Interpreter → Converts source code into Bytecode
- PVM → Executes Bytecode
- Bytecode → Platform Independent
- Machine Code → Platform Dependent
- Python internally compiles before execution
- PVM is responsible for running the Bytecode
If the interviewer asks:
"How does Python execute a program?"
Always explain the complete execution flow using the architecture diagram.
Remember the execution order:
Source Code
↓
Interpreter
↓
Bytecode
↓
PVM
↓
Operating System
↓
Hardware
↓
Output
A Data Type is a classification that specifies the type of value a variable can store.
It tells the Python interpreter:
- What kind of data is stored.
- How the data should be processed.
- What operations can be performed on it.
Every value in Python belongs to a specific data type.
age = 21 # int
salary = 25000.50 # float
name = "Basha" # str
status = True # boolData Types are required because different types of data need to be processed differently.
For example,
- Addition is performed on numbers.
- Concatenation is performed on strings.
- Logical operations are performed on Boolean values.
Without data types, Python cannot determine which operation should be performed.
- Efficient memory allocation.
- Better readability.
- Easy debugging.
- Proper execution of operations.
- Type safety during runtime.
Python Data Types are classified into two categories.
- int
- float
- bool
- str
- complex
- list
- tuple
- set
- dictionary
Dynamic Typing means Python automatically determines the data type of a variable based on the assigned value.
The programmer does not need to declare the data type explicitly.
number = 100
price = 99.99
name = "Python"
status = TruePython automatically identifies:
number→ intprice→ floatname→ strstatus→ bool
| Static Typing | Dynamic Typing |
|---|---|
| Data type must be declared. | Data type is determined automatically. |
| Checked during compilation. | Checked during execution. |
| More coding required. | Less coding required. |
| Example: Java, C++ | Example: Python |
The Integer (int) data type stores whole numbers without a decimal point.
Integers may be:
- Positive
- Negative
- Zero
age = 22
marks = 95
temperature = -10The Float (float) data type stores numbers containing a decimal point.
percentage = 92.5
price = 499.99
height = 175.8The Boolean (bool) data type stores only two values.
- True
- False
It is mainly used in:
- Decision making
- Conditional statements
- Loops
- Comparisons
is_logged_in = True
is_admin = FalseA String (str) is a sequence of Unicode characters enclosed within quotes.
Python supports:
- Single Quotes
- Double Quotes
- Triple Single Quotes
- Triple Double Quotes
name = "Basha"
city = 'Hyderabad'The Complex (complex) data type stores numbers having both a real part and an imaginary part.
General Form
a + bj
Where:
a→ Real Partb→ Imaginary Partj→ Imaginary Unit
number = 5 + 3jPython provides the type() function.
age = 22
print(type(age))<class 'int'>
No.
Python variables are not declared with a data type.
The interpreter automatically assigns the appropriate data type based on the assigned value.
number = 10
number = "Python"Python allows the same variable to store different types of values.
Some Python data types are mutable, while others are immutable.
- int
- float
- bool
- str
- complex
- tuple
- list
- set
- dictionary
| Mutable | Immutable |
|---|---|
| Can be modified after creation | Cannot be modified after creation |
| Memory remains the same | New object is created after modification |
| Examples: list, set, dictionary | Examples: int, float, str, tuple |
In Python, variables do not store values directly.
Instead, they store a reference to an object.
Example
age = 21Memory Representation
age
│
▼
+----------------+
| 21 |
| int Object |
+----------------+
Yes.
Since Python is dynamically typed, a variable can store different data types during program execution.
Example
value = 100
print(type(value))
value = "Python"
print(type(value))<class 'int'>
<class 'str'>
Advantages include:
- Less code.
- Easy to learn.
- Flexible programming.
- Faster development.
- Improved readability.
- Suitable for rapid application development.
Python provides Advanced Data Types to store multiple values.
They are:
- list
- tuple
- set
- dictionary
Example
marks = [80, 85, 90]The default data type of a decimal number in Python is float.
Example
number = 25.75
print(type(number))Output
<class 'float'>
Yes.
Python automatically performs Implicit Type Casting.
Example
number1 = 10
number2 = 5.5
result = number1 + number2
print(result)
print(type(result))15.5
<class 'float'>
- Every value belongs to a data type.
- Python supports Basic and Advanced Data Types.
- Python is dynamically typed.
- Variables store references to objects.
intstores whole numbers.floatstores decimal numbers.boolstores logical values.strstores text.complexstores complex numbers.- Mutable objects can be modified.
- Immutable objects cannot be modified.
type()returns the data type of a variable.
Type Casting is the process of converting a value from one data type into another data type.
Python provides built-in functions to perform type conversion whenever required.
number = 100
result = float(number)
print(result)100.0
Type Casting is required to:
- Convert one data type into another.
- Perform arithmetic operations between different data types.
- Accept and process user input.
- Avoid type mismatch errors.
- Improve code flexibility and readability.
number = "100"
print(int(number) + 50)150
Python supports two types of Type Casting.
- Implicit Type Casting
- Explicit Type Casting
Type Casting
│
├── Implicit Type Casting
└── Explicit Type Casting
Implicit Type Casting is the automatic conversion of one data type into another by the Python Interpreter.
The programmer does not perform the conversion manually.
number1 = 10
number2 = 2.5
result = number1 + number2
print(result)
print(type(result))12.5
<class 'float'>
number1is an integer.number2is a float.- Python automatically converts the integer into a float.
- The final result becomes a float.
Explicit Type Casting is the manual conversion of one data type into another using Python's built-in conversion functions.
number = 25.99
result = int(number)
print(result)25
| Implicit Type Casting | Explicit Type Casting |
|---|---|
| Done automatically by Python | Done manually by the programmer |
| No conversion function is required | Uses conversion functions |
| Safer for compatible types | Programmer has full control |
Example: 10 + 2.5 |
Example: int(25.8) |
Python provides the following built-in conversion functions.
| Function | Converts To |
|---|---|
int() |
Integer |
float() |
Float |
str() |
String |
bool() |
Boolean |
complex() |
Complex |
The int() function converts a compatible value into an integer.
print(int(25.99))
print(int(True))
print(int(False))25
1
0
The float() function converts a compatible value into a floating-point number.
print(float(10))
print(float(True))10.0
1.0
The str() function converts any compatible value into a string.
number = 500
text = str(number)
print(text)
print(type(text))500
<class 'str'>
The bool() function converts a value into either True or False.
| Value | Result |
|---|---|
0 |
False |
1 |
True |
"" |
False |
"Python" |
True |
[] |
False |
[10] |
True |
None |
False |
print(bool(0))
print(bool(10))
print(bool(""))
print(bool("Python"))False
True
False
True
The complex() function converts a number into a complex number.
number = complex(10)
print(number)(10+0j)
No.
Only strings containing valid numeric values can be converted.
print(int("150"))Output
150
print(int("Python"))Output
ValueError: invalid literal for int()
The decimal part is removed.
It is not rounded.
print(int(45.98))45
Yes.
Python performs Implicit Type Casting automatically.
result = 50 + 25.5
print(result)
print(type(result))75.5
<class 'float'>
Python raises an exception.
Example
print(int("Hello"))Output
ValueError: invalid literal for int()
A Type Mismatch occurs when an operation is performed between incompatible data types.
print("100" + 50)Output
TypeError
Correct Approach
print(int("100") + 50)Output
150
The most commonly used functions are:
int()float()str()bool()
These functions are frequently used while processing user input and performing calculations.
Advantages include:
- Easy data conversion.
- Better program flexibility.
- Reduces type mismatch errors.
- Improves compatibility between data types.
- Simplifies user input handling.
Consider a calculator application.
The user enters two numbers using the keyboard.
number1 = "50"
number2 = "25"Both values are stored as strings.
Before performing addition, they must be converted into integers.
result = int(number1) + int(number2)
print(result)75
Without type casting, Python would treat the values as strings instead of numbers.
- Type Casting converts one data type into another.
- Python supports Implicit and Explicit Type Casting.
- Implicit conversion is automatic.
- Explicit conversion uses built-in functions.
- Common conversion functions are:
int()float()str()bool()complex()
- Invalid conversions raise exceptions such as
ValueError.
These programs are commonly asked in Python Fresher Interviews, Technical Interviews, Campus Placements, and Viva Examinations.
Each program includes:
- Problem Statement
- Program
- Output
- Explanation
- Interview Questions
Write a Python program to display Hello World.
# Displaying a message
print("Hello World")Hello World
print()is a built-in function.- It displays the given message on the console.
"Hello World"is a string literal.
Q1. Is print a keyword?
No. It is a built-in function.
Q2. Can print() display variables?
Yes.
Example
name = "Basha"
print(name)Q3. Can print() display multiple values?
Yes.
Example
name = "Basha"
age = 22
print(name, age)Write a program to declare variables of different data types and display them.
# Integer
age = 22
# Float
percentage = 92.5
# String
name = "Basha"
# Boolean
is_student = True
# Complex
number = 5 + 3j
# Displaying values
print(age)
print(percentage)
print(name)
print(is_student)
print(number)22
92.5
Basha
True
(5+3j)
The program demonstrates the five basic Python data types.
- What are the basic data types?
- Which data type stores decimal values?
- Which data type stores logical values?
- What is a complex number?
Write a program to display the data type of different variables.
age = 22
salary = 35000.50
name = "Python"
status = True
print(type(age))
print(type(salary))
print(type(name))
print(type(status))<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
The type() function returns the data type of an object.
- What is the purpose of the
type()function? - Does
type()return a string? - Can
type()be used with all Python objects?
Write a program to demonstrate Dynamic Typing.
value = 100
print(value)
print(type(value))
value = "Python"
print(value)
print(type(value))100
<class 'int'>
Python
<class 'str'>
The same variable stores different data types.
This demonstrates Dynamic Typing.
- What is Dynamic Typing?
- Can a variable change its data type?
- Is Dynamic Typing available in Java?
Write a program to demonstrate Implicit Type Casting.
number1 = 20
number2 = 5.5
result = number1 + number2
print(result)
print(type(result))25.5
<class 'float'>
Python automatically converts the integer into a float before performing addition.
Write a program to convert a float into an integer.
number = 25.99
result = int(number)
print(result)25
The int() function removes the decimal part and returns the integer value.
- What is Explicit Type Casting?
- Which function converts float to int?
- Does
int()round the value?
Answer:
No.
It simply removes the decimal part.
number = 100
result = float(number)
print(result)100.0
The float() function converts an integer into a floating-point number.
number = 150
text = str(number)
print(text)
print(type(text))150
<class 'str'>
number = 1
print(bool(number))
number = 0
print(bool(number))True
False
- Zero becomes False.
- Non-zero becomes True.
number = 5 + 3j
print(number.real)
print(number.imag)5.0
3.0
.realreturns the real part..imagreturns the imaginary part.
print(type(100))<class 'int'>
print(type(100.5))<class 'float'>
print(type(True))<class 'bool'>
print(type("Python"))<class 'str'>
print(int(25.99))25
print(float(100))100.0
print(bool(0))False
print(bool(25))True
print(type(5 + 3j))<class 'complex'>
print(str(100))100
- Python is a High-Level Programming Language.
- Python is an Interpreted Language.
- Python supports Dynamic Typing.
- Variables do not require explicit data type declarations.
- Python has five basic data types:
- int
- float
- bool
- str
- complex
- Python supports Implicit and Explicit Type Casting.
type()returns the data type.print()displays output.bool(0)returnsFalse.bool(1)returnsTrue..realreturns the real part of a complex number..imagreturns the imaginary part of a complex number.
This interview guide covered:
- Introduction to Python
- Execution Flow of Python Program
- Python Data Types
- Type Casting
- 70 Interview Questions with Answers
- 10 Frequently Asked Interview Programs
- 10 Output-Based Questions
- Viva Questions
- Quick Revision Notes
This document serves as a complete interview preparation guide for the 01-Python-Basics folder and is suitable for freshers preparing for technical interviews, campus placements, and Python viva examinations.