DEV Community

bhuvaneswari nandhagopal
bhuvaneswari nandhagopal

Posted on

Python Interview Preparation Notes:

What is python?
python is general purpose high level programing language.

what is high level programing language?
A High-Level Programming Language is a type of programming language that is easy for humans to read and write.
It is closer to human language (English-like) and farther from machine language (0s and 1s)

Examples:
Python
Java
C++
JavaScript

Key points:
Easy to understand
Easy to learn
Portable (works on different computers)

What is compiled & interpreted?
Both or object oriented programing languages. A compiled language translates the entire source cod into machine code before execution. A interpreted language translates and executes code in line by line at runtime.

Compiled languages:
Examples:

  1. c++
  2. c

Interpreted languages:
Examples:

  1. Python
  2. Java script
  3. Ruby

ADVANTAGES AND DISADVANTAGES OF COMPILED & INTERPRETED:

COMPILED:
Advantages:

  1. Faster execution
  2. Better performance
  3. Errors caught before running

Disadvantages:

  1. Compilation step can take time.
  2. platform-specific binaries.

INTERPRETED:
Advantages:

  1. Easier debugging
  2. Portables across systems
  3. Faster development cycle

Disadvantages:

  1. Slower execution
  2. Some errors appear only during runtime

            **VON NEUMAN ARCHITECTURE: **
    

What is von neuman architecture?
The von neuman architecture is a computer design model proposed by JOHN VON NEUMAN. It describes a computer stores and processes data using the same memory for:
1. Instruction (programs)
2. Data
In the same memory this is called the stored program concept.

What is String Data Type?
String is a data type used to store text or a sequence of characters."

Example:
name = "Bhuvana"
Bhuvana is string data types.

What is String Functions?
String functions are built-in methods in Python used to perform operations on strings such as changing case, finding length, replacing text, and removing spaces.

  1. len() → Find length of string
print(len("Hello"))
Enter fullscreen mode Exit fullscreen mode

output:
5

  1. upper() → Convert to uppercase
print("hello".upper())
Enter fullscreen mode Exit fullscreen mode

output:
HELLO

  1. lower() → Convert to lowercase
print("HELLO".lower())
Enter fullscreen mode Exit fullscreen mode

output:
hello

  1. strip() → Remove extra spaces
print("  Hi  ".strip())
Enter fullscreen mode Exit fullscreen mode

output:
Hi

  1. replace() → Replace words
print("I like Java".replace("Java", "Python"))
Enter fullscreen mode Exit fullscreen mode

output:
I like python

If you want, I can also give:

What is String Operations?
String operations are the actions performed on strings, such as concatenation, indexing, slicing, and repetition.

  1. Concatenation (Joining strings)
a = "Hello"
b = "World"
print(a + b)
Enter fullscreen mode Exit fullscreen mode

output:
HelloWorld

  1. Indexing (Getting one character)
a = "Hello"
print(a[1])
Enter fullscreen mode Exit fullscreen mode

output:
e

  1. Slicing (Getting part of string)
a = "Hello"
print(a[1:4])
Enter fullscreen mode Exit fullscreen mode

output:
ello

  1. Repetition (Repeating string)
print("Hi" * 3)
Enter fullscreen mode Exit fullscreen mode

output:
HiHiHi

What is Integer Data Type?

An integer data type is used to store whole numbers (numbers without decimal points).

Positive numbers → 10, 25, 100
Negative numbers → -5, -20
Zero → 0

What is Float?
"Float is a data type used to store numbers with decimal points like 10.5, 3.14, etc."

What is Boolean?
Boolean is a data type in Python that represents only two values: True and False. It is used in conditions and comparisons.

                  LOOP
Enter fullscreen mode Exit fullscreen mode

Explain for loop in Python?
A for loop is used to repeat a block of code for a sequence like range, list, or string. It helps to execute code multiple times easily.

for loop program with list:

fruits = ["apple", "banana", "mango"]

for i in fruits:
    print(i)
Enter fullscreen mode Exit fullscreen mode

Output:
apple
banana
mango

String program for loop:

name = "Python"

for i in name:
    print(i)
Enter fullscreen mode Exit fullscreen mode

output
p
y
t
h
o
n

What is list?
List is used to store multiple values in a single variable list can be changed often creation.

What is tuple?
tuple is used to multiple values it is similar to a list but its values cannot be changed.

What is dictionary?
Dictionary store data in key values pairs it is useful for searching data quickly.

What is set?
Set is used to store unique values. duplicate values are not allowed.

                     **FILE** 
Enter fullscreen mode Exit fullscreen mode

How do you create a file in python?
We can create a file in python using open() function with "w" mode.

Example:

file = open("data.txt", "w")
file.close()
Enter fullscreen mode Exit fullscreen mode

Write a program to create a file name(sample.txt)?
This program creates a file named sample.txt using the open() function in write mode.
Example:

file = open("sample.txt", "w")
file.close()

print("File created successfully")
Enter fullscreen mode Exit fullscreen mode

How do you create and write data into a file?
We use the open() function with write ('w') mode to create a file and write() method to write data into it.

Example:

file = open("sample.txt", "w")

file.write("Hello World")

file.close()

print("Data written successfully")
Enter fullscreen mode Exit fullscreen mode

Difference between read and write mode?

Read mode (r):

  • It is used to read data from a file.
  • The file must already exist.
  • We cannot modify the file content.

Write mode (w):

  • It is used to create a file and write data into it.
  • If the file already exists, it will overwrite the existing data.

Top comments (0)