Python Basics to Sequences
🧾 Python Notes (Till If-Else Statements and Sequences)
✅ 1. Print Function in Python
🔹 Purpose:
Used to display output on the screen.
🔹 Syntax:
python print(object1, object2, ..., sep=' ', end='\n',
file=sys.stdout)
🔹 Examples:
python
print("Hello, World!")
print("Name:", "Aayush", sep=" - ")
print("Python", end=" is awesome\n")
🔹 Features:
You can print strings, numbers, variables, expressions, etc.
`sep`: Specifies separator between objects. Default is space.
`end`: What to print at the end. Default is newline (`\n`).
🔹 Comparison with C/C++:
`printf("Hello %s", name); `
`print("Hello", name) `
`%s ` `%d`
✅ 2. Data Types in Python
🔹 Main Data Types:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/7
`int`: Integers → `10`
`float`: Floating-point numbers → `3.14`
`str`: Strings → `"Hello"`
`bool`: Boolean → `True`, `False`
`NoneType`: Represents no value → `None` 🔹
Examples:
python
a = 5 # int b
= 3.2 # float
name = "Aayush" #
str is_student =
True # bool
🔹 Dynamic Typing:
In Python, no need to declare data type. Interpreter detects it. This is called
Dynamic Typing.
🔹 Comparison with C/C++:
`int a = 5; `
`a = 5 ` ←
✅ 3. Comments in Python
🔹 Types of Comments:
Single Line: Start with `#`
Multi-line: Use triple quotes `''' ... '''` or `""" ... """` 🔹
Examples:
python
# This is a single line comment
'''
This is a
multi-line
comment
'''
🔹 Use:
To explain code, disable execution, or for documentation.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/7
✅ 4. Variables in Python
🔹 What is a Variable?
A container for storing data.
🔹 Declaration:
python
name =
"Aayush" age =
21
🔹 No type mentioned (Dynamic Typing). 🔹 You
can reassign different data types:
python
x = 5 # int x
= "Hi" # now x
is str
🔹 Good to Know:
This behavior is called Dynamic Binding — variable types are bound to values at runtime.
✅ 5. Keywords and Identifiers
🔹 Keywords:
Reserved words in Python. e.g. `if`, `for`, `True`, `None`, `class`, `import` Cannot be
used as variable names.
🔹 Identifiers:
Names used for variables, functions, classes, etc.
🔹 Rules for Identifiers:
Can include letters (a–z, A–Z), digits (0–9), and underscores (_)
Cannot start with a digit
Are case-sensitive (`Age` and `age` are different)
🔹 Valid Examples:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/7
python name, _age, a1,
userName
✅ 6. Taking User Input 🔹
Syntax:
python
input(prompt)
Always returns a string, so type casting is needed for numbers.
🔹 Examples:
python
name = input("Enter your
name: ") age =
int(input("Enter age: ")) #
cast to int
🔹 Comparison with C/C++:
`scanf("%d", &age); `
`int(input()) `
✅ 7. Type Conversion
🔹 Types:
1. Implicit: Done automatically 2. Explicit:
Done using casting functions 🔹 Casting
Functions:
python
int("10") → 10
float("3.14") →
3.14 str(100) →
"100" bool(0) →
False
🔹 Example:
python
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/7
a = "10" b =
int(a) + 5 # →
15
✅ 8. Literals in Python
🔹 What are Literals?
Values or data assigned to variables.
🔹 Types:
Numeric: 10, 5.5
String: "Hello"
Boolean: True, False
Special: None
Collection: Lists, Tuples, Dicts, Sets 🔹
Example:
python
a = 100 #
Integer literal b =
10.5 # Float
literal c = "Hello"
# String literal d =
True # Boolean
literal
✅ 9. Operators in Python
🔹 Types of Operators:
1. Arithmetic: `+`, `-`, `*`, `/`, `//`, `%`, `**`
2. Relational: `==`, `!=`, `>`, `<`, `>=`, `<=`
3. Logical: `and`, `or`, `not`
4. Assignment: `=`, `+=`, `-=`, etc.
5. Bitwise: `&`, `|`, `^`, `~`, `<<`, `>>`
6. Membership: `in`, `not in`
7. Identity: `is`, `is not` 🔹 Example:
python
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/7
x = 10
print(x + 5)
# Arithmetic
print(x == 10)
# Relational
print(x > 5 and
x < 15) #
Logical
✅ 10. If-Else Statements
🔹 Syntax:
python
if condition:
# block elif
condition:
# block else:
# block
🔹 Example:
python
age = 18
if age >= 18:
print("Adult")
else:
print("Minor")
🔹 Nested If:
python
if age > 0:
if age < 18:
print("Minor")
else:
print("Adult")
else:
print("Invalid
age")
11 SEQUENCES
➔ General concept: ordered collection (str, list, tuple)
➔ Common operations: indexing, slicing, iteration, len(), min(), max(), in
LISTS
• Ordered, mutable, allows duplicates.
• Syntax: my_list = [1, 2, 3, "a"]
• Access: my_list[0], Negative indexing: my_list[-1]
• Methods: append(), insert(), pop(), remove(), sort(), reverse()
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/7
TUPLES
• Ordered, immutable, allows duplicates.
• Syntax: my_tuple = (1, 2, 3)
• Supports indexing, slicing, len(), count(), index()
• Used for fixed data, can be keys in dictionaries.
•
Note: mutability refers to whether or not an object’s value can be changed after it is created.
DICTIONARIES
• Key-value pairs, unordered (insertion ordered since Python 3.7), mutable.
• Syntax: my_dict = {"name": "Alice", "age": 25}
• Access: my_dict["name"]
• Methods: keys(), values(), items(), get(), update(), pop()
• Keys must be immutable (e.g., str, int, tuple).
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/7