0% found this document useful (0 votes)
3 views11 pages

Python Fundamentals - Comprehensive Notes

The document provides comprehensive notes on Python fundamentals, covering topics such as variables, data types, string operations, and numeric functions. It highlights Python's popularity due to its simplicity and versatility, and includes best practices for variable naming and common mistakes to avoid. Additionally, it offers insights into exam and interview questions related to Python programming.

Uploaded by

suhanibansal32
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)
3 views11 pages

Python Fundamentals - Comprehensive Notes

The document provides comprehensive notes on Python fundamentals, covering topics such as variables, data types, string operations, and numeric functions. It highlights Python's popularity due to its simplicity and versatility, and includes best practices for variable naming and common mistakes to avoid. Additionally, it offers insights into exam and interview questions related to Python programming.

Uploaded by

suhanibansal32
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

Python Fundamentals – Comprehensive Notes

Table of Contents
1. Introduction to Python
2. Variables and Naming Conventions
3. Comments in Python
4. Output Using the print() Function
5. Python Data Types
6. Strings in Python
7. String Operations
8. Numeric Data Types (Integers & Floats)
9. Type Conversion & Built-in Numeric Functions
10. Augmented Assignment Operators

1. Introduction to Python

What is Python?

Python is a general-purpose, high-level programming language known for its simplicity, readability,
and versatility. It allows developers to focus more on problem-solving rather than syntax complexity.

Why Python is Popular

• Easy to learn and beginner-friendly


• Large ecosystem of libraries and frameworks
• Strong community support
• Cross-platform compatibility

Common Uses of Python

• Data Science & Machine Learning (Pandas, NumPy, TensorFlow, Scikit-learn)


• Web Development (Django, Flask, FastAPI)
• Automation & Scripting
• Cybersecurity & Ethical Hacking
• IoT & Embedded Systems (Raspberry Pi, MicroPython)

1
2. Variables and Naming Conventions

What is a Variable?

A variable is a named reference to a value stored in memory.

Variable Declaration

Python uses dynamic typing, so no data type declaration is required.

name = 'John Doe'


age = 25

Rules for Naming Variables

• Must start with a letter or underscore (_)


• Can contain letters, digits, and underscores
• Case-sensitive ( age , Age , AGE are different)
• Cannot be Python reserved keywords ( if , class , def , etc.)

Best Practices (Naming Conventions)

• Use snake_case
• Use descriptive names
• Avoid single-letter variables (except in loops)

user_age = 30 # Good
x = 30 # Avoid

3. Comments in Python

Single-line Comment

# This is a comment

Multi-line Comment

# Line one
# Line two
# Line three

2
Purpose of Comments

• Explain logic
• Improve readability
• Leave reminders
• Note: Avoid explaining obvious code—use meaningful variable names instead.

4. Output Using the `` Function

Basic Usage

print('Hello World!')

Printing Multiple Values

print('Colors:', 'Red', 'Blue', 'Green')

Python automatically inserts spaces between comma-separated values.

5. Python Data Types

Dynamic Typing

Python determines the data type at runtime.

Common Built-in Data Types

Data Type Description Example

int Integer values 10

float Decimal numbers 3.14

str Text data 'Hello'

bool True/False True

list Ordered, mutable [1, 'a', True]

tuple Ordered, immutable (1, 2)

set Unordered, unique {1, 2}

dict Key-value pairs {'a':1}

3
Data Type Description Example

range Number sequence range(5)

NoneType No value None

Checking Data Type

type(variable)

**Using **``

isinstance(42, int) # True

6. Strings in Python

What is a String?

A string is a sequence of characters enclosed in single or double quotes.

name = "Python"

Multi-line Strings

text = '''This is
a multiline string'''

Escape Characters

msg = 'It\'s sunny'


quote = "She said, \"Hello!\""

7. String Operations

String Immutability

Strings cannot be modified after creation.

4
greeting = 'Hi'
greeting[0] = 'H' # Error

String Length

len('Hello') # 5

Indexing

text = 'Hello'
text[0] # H
text[-1] # o

String Slicing

text[1:4] # ell
text[::-1] # Reverse string

String Concatenation

'Hello' + ' World'

String Interpolation (f-strings)

name = 'John'
age = 25
f'My name is {name} and I am {age}'

8. Common String Methods

Method Description

upper() Convert to uppercase

lower() Convert to lowercase

strip() Remove whitespace

replace() Replace substring

5
Method Description

split() Convert to list

join() Join list into string

find() Find substring index

count() Count occurrences

startswith() Check prefix

endswith() Check suffix

9. Numeric Data Types (Integers & Floats)

Arithmetic Operations

Operator Description

+ Addition

- Subtraction

* Multiplication

/ Division

// Floor Division

% Modulus

** Exponentiation

Mixed-Type Operations

5 + 2.5 # Result is float

10. Type Conversion & Numeric Functions

Type Casting

int('45')
float('7.8')

6
Built-in Numeric Functions

Function Purpose

round() Rounds numbers

abs() Absolute value

pow() Power calculation

11. Augmented Assignment Operators

What Are They?

They combine operation + assignment in one step.

x += 5 # Same as x = x + 5

Common Augmented Operators

Operator Meaning

+= Add

-= Subtract

*= Multiply

/= Divide

//= Floor divide

%= Modulus

**= Exponentiate

With Strings

text = 'Hi'
text *= 3 # HiHiHi

• Python does not support ++ or -- operators.

7
Exam & Interview Enhancements

High-Value Exam Definitions (Write Exactly Like This)

Python

Python is a high-level, interpreted, dynamically typed, general-purpose programming language known for
its simplicity and readability.

Variable

A variable is a named memory location used to store data values that can change during program
execution.

Dynamic Typing

Dynamic typing means the data type of a variable is determined at runtime based on the value assigned to
it.

String Immutability

String immutability means once a string object is created, its contents cannot be modified; any modification
results in a new string object.

Frequently Asked Exam Questions (With Focus Points)

Q1. Why is Python called a dynamically typed language?

Answer Focus:

• No explicit type declaration


• Type decided at runtime
• Faster development but runtime errors possible

Q2. What is the difference between = and == ?

Operator Purpose

= Assignment

== Comparison

8
Q3. Explain string slicing with example.

Key Points to Mention:

• Uses [start:stop:step]
• Stop index is non-inclusive
• Original string remains unchanged

Common Exam Mistakes & Traps


• ❌ Using ++ or -- operators in Python
• ❌ Trying to modify strings using indexing
• ❌ Forgetting that / always returns a float
• ❌ Assuming Python checks types before execution

Interview-Oriented Explanations (Short & Clear)

Explain Python in one line

Python is an interpreted, dynamically typed language that prioritizes readability and rapid
development.

Why are strings immutable in Python?

Immutability improves memory efficiency and makes strings safer to use as dictionary keys.

Why doesn’t Python support ``?

Python avoids ambiguous syntax and prefers explicit operations like += 1 for clarity.

Interview Output-Based Questions

Question 1

x = '5'
y = 2
print(x * y)

Output: 55

9
Explanation: String repetition, not arithmetic multiplication.

Question 2

a = 10
b = 3
print(a / b)
print(a // b)

Output:

3.3333333333333335
3

Concept Comparison (Very High Interview Value)

List vs Tuple

Feature List Tuple

Mutability Mutable Immutable

Performance Slower Faster

Use Case Dynamic data Fixed data

\*\* vs \*\*

Feature type() isinstance()

Checks exact type Yes No

Supports inheritance No Yes

Real-World Usage Mapping (Interview Gold)

Concept Real-World Use

List User inputs, API responses

10
Concept Real-World Use

Tuple Fixed configuration values

Set Removing duplicates

Dictionary JSON data, APIs

String slicing Data cleaning

Last-Day Revision Checklist


• ✔ Python is dynamically typed
• ✔ Strings are immutable
• ✔ / → float, // → floor division
• ✔ += replaces ++
• ✔ type() vs isinstance() difference

11

You might also like