KEMBAR78
Python Notes Unit 1 | PDF | Python (Programming Language) | Reserved Word
0% found this document useful (0 votes)
9 views8 pages

Python Notes Unit 1

The document provides an introduction to Python, highlighting its features such as simplicity, interpretability, and extensive libraries. It covers key concepts including identifiers, keywords, indentation, comments, built-in data types, control structures, user-defined functions, and data structures like lists, tuples, dictionaries, and sets. Each section includes examples to illustrate the concepts discussed.
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)
9 views8 pages

Python Notes Unit 1

The document provides an introduction to Python, highlighting its features such as simplicity, interpretability, and extensive libraries. It covers key concepts including identifiers, keywords, indentation, comments, built-in data types, control structures, user-defined functions, and data structures like lists, tuples, dictionaries, and sets. Each section includes examples to illustrate the concepts discussed.
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
You are on page 1/ 8

Python Notes – Unit I

🔹 1. Introduction to Python

Python is an interpreted, object-oriented, high-level programming language with simple


syntax, making it easy to learn and use.

Features of Python:

1. Simple & Easy to Learn – Syntax is similar to English.

2. Interpreted – Executes line by line (no need to compile).

3. Cross-Platform – Works on Windows, Linux, macOS.

4. Free & Open Source – Freely available.

5. Extensive Libraries – NumPy, Pandas, Matplotlib, TensorFlow.

6. Dynamically Typed – No need to declare variable types.

7. Supports OOP – Object-Oriented Programming concepts like classes, inheritance.

8. Portable – Code can run anywhere.

Example:

print("Hello, Python!")

Output:

Hello, Python!

🔹 2. Identifiers & Keywords

• Identifiers: Names given to variables, functions, classes.

o Must start with a letter or underscore (_).

o Cannot start with a digit.

o Case-sensitive.

o Example: my_var, _count, Student1.


• Keywords: Reserved words (cannot be used as identifiers).

o Example: if, for, while, class, def, return, import.

# Example

name = "Robin" # identifier

print(name)

# if = 5 ❌ invalid (because "if" is a keyword)

🔹 3. Indentation

Python uses indentation (spaces/tabs) instead of {} to define code blocks.

if True:

print("This is indented properly") # 4 spaces

🔹 4. Comments

• Single-line comment: # This is a comment

• Multi-line comment: Triple quotes """ ... """

# This is single-line comment

"""

This is

multi-line

comment

"""

🔹 5. Built-in Data Types


Type Example

int a = 10

float b = 3.14

str name = "Python"

bool x = True

list [1, 2, 3]

tuple (10, 20, 30)

set {1, 2, 3}

dict {"id": 1, "name": "Alice"}

Control Structures

🔹 1. for loop

for i in range(5):

print(i)

Output:

🔹 2. Nested for loop

for i in range(1, 4):


for j in range(1, 3):

print(i, j)

Output:

11

12

21

22

31

32

🔹 3. while loop

n=1

while n <= 5:

print(n)

n += 1

Output:

User Defined Functions

🔹 Function Definition & Call


def greet():

print("Hello, welcome to Python!")

greet()

Output:

Hello, welcome to Python!

🔹 Function Arguments

1. Positional Arguments

def add(a, b):

print("Sum:", a + b)

add(5, 10)

Output:

Sum: 15

2. Keyword Arguments

def student(name, age):

print("Name:", name, "Age:", age)

student(age=20, name="Alex")

Output:

Name: Alex Age: 20

3. Default Arguments

def greet(name="Guest"):
print("Hello", name)

greet()

greet("Robin")

Output:

Hello Guest

Hello Robin

4. Variable-length Arguments

def total(*numbers):

print(sum(numbers))

total(10, 20, 30)

Output:

60

Data Structures

🔹 1. List

A list is a mutable ordered collection.

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

✅ List Methods

fruits.append("orange") # ['apple', 'banana', 'cherry', 'orange']

fruits.copy() # ['apple', 'banana', 'cherry', 'orange']

fruits.count("apple") #1

fruits.extend(["grape"]) # ['apple', 'banana', 'cherry', 'orange', 'grape']


fruits.index("banana") #1

fruits.pop() # removes last → 'grape'

fruits.remove("banana") # removes 'banana'

fruits.reverse() # ['orange', 'cherry', 'apple']

fruits.sort() # ['apple', 'cherry', 'orange']

🔹 2. Tuple

A tuple is an immutable ordered collection.

my_tuple = (10, 20, 30, 20)

print(my_tuple.count(20)) # 2

print(my_tuple.index(30)) # 2

🔹 3. Dictionary

A dictionary is a key-value pair collection.

student = {"id": 1, "name": "Alex"}

✅ Dictionary Methods

student.clear() # {}

student = {"id": 1, "name": "Alex"}

print(student.copy()) # {'id': 1, 'name': 'Alex'}

print(student.get("name")) # Alex

print(dict.fromkeys(["a","b"],0)) # {'a': 0, 'b': 0}

print(student.items()) # dict_items([('id', 1), ('name', 'Alex')])

print(student.keys()) # dict_keys(['id', 'name'])

student.pop("id") # removes id
student.popitem() # removes last item

student.setdefault("age", 20) # adds new key if not exists

student.update({"name": "Robin"}) # updates name

print(student.values()) # dict_values(['Robin', 20])

🔹 4. Set

A set is an unordered collection of unique elements.

s1 = {1, 2, 3}

s2 = {3, 4, 5}

✅ Set Methods

s1.add(6) # {1, 2, 3, 6}

s1.copy() # {1, 2, 3, 6}

s1.clear() # set()

print(s1.difference(s2)) # {1, 2}

print(s1.union(s2)) # {1,2,3,4,5}

print(s1.intersection(s2))# {3}

print(s1.symmetric_difference(s2)) # {1,2,4,5}

print(s1.isdisjoint(s2)) # False

print(s1.issubset(s2)) # False

print(s1.issuperset(s2)) # False

You might also like