Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Python language learning

Rating
-
Sold
-
Pages
16
Uploaded on
26-11-2024
Written in
2024/2025

This is good python learning resorces.

Institution
Course

Content preview

1. Python Data Structure
Lists
Creating a List
my_list = [1, 2, 3, 'a', 'b', 'c']


Accessing Elements
print(my_list[0]) # Output: 1
print(my_list[-1]) # Output: 'c' (last element)




De
Slicing
print(my_list[1:3]) # Output: [2, 3]
print(my_list[:2]) # Output: [1, 2] (first two elements)

Modifying Elements
my_list[0] = 10

e_
Adding Elements
my_list.append(5)
my_list.insert(1, 'x')
cod
# Add to the end
# Insert 'x' at index 1




es
Removing Elements
my_list.remove(3) # Remove element with value 3
element = my_list.pop(2) # Remove and return element at index 2
my_list.clear() # Remove all elements

Other List Methods
my_list.sort() # Sort list in ascending order
my_list.reverse() # Reverse the list
my_list.count('a') # Count occurrences of 'a'
len(my_list) # Get the number of elements



2. Tuples
Creating a Tuple
my_tuple = (1, 2, 3, 'a', 'b')

Accessing Elements
print(my_tuple[1]) # Output: 2

,Slicing
print(my_tuple[1:3]) # Output: (2, 3)

Tuple Unpacking
a, b, c = (1, 2, 3) # Assign 1 to a, 2 to b, 3 to c

Tuple Methods
my_tuple.count(1) # Count occurrences of 1
my_tuple.index('a') # Find the index of 'a'

Note: Tuples are immutable, meaning elements cannot be modified after creation.




De
3. Sets
Creating a Set

e_
cod
my_set = {1, 2, 3, 'a'}

Adding/Removing Elements
my_set.add(4) # Add an element
my_set.remove(2) # Remove an element (raises error if not found)




es
my_set.discard(5) # Remove element if present (no error if not
found)

Set Operations
set1 = {1, 2, 3}
set2 = {3, 4, 5}

union = set1 | set2 # Union: {1, 2, 3, 4, 5}
intersection = set1 & set2 # Intersection: {3}
difference = set1 - set2 # Difference: {1, 2}

Other Set Methods
set1.isdisjoint(set2) # Check if sets have no elements in common
set1.issubset(set2) # Check if set1 is a subset of set2
len(my_set) # Get the number of elements



4. Dictionaries
Creating a Dictionary
my_dict = {"name": "John", "age": 25, "city": "New York"}

, Accessing Elements
print(my_dict["name"]) # Output: John
print(my_dict.get("age")) # Output: 25


Adding/Modifying Elements
my_dict["email"] = "" # Add new key-value pair
my_dict["age"] = 26 # Modify existing value

Removing Elements
del my_dict["city"] # Delete the key-value pair 'city'
my_dict.pop("email") # Remove and return value of 'email'




De
my_dict.clear() # Remove all elements

Iterating Over a Dictionary




e_
for key, value in my_dict.items():
print(key, value)

Dictionary Methods




cod
my_dict.keys() # Get all keys
my_dict.values() # Get all values
my_dict.items() # Get all key-value pairs



2. OBJECT ORIENTED PROGRAMMING
1. Creating a Class
class MyClass:
def __init__(self, name):
self.name = name

def greet(self):
# Constructor
# Instance attribute

# Instance method
print(f"Hello, {self.name}!")
es
# Creating an object (instance of MyClass)
obj = MyClass("Alice")
obj.greet() # Output: Hello, Alice!

__init__ Method (Constructor)

 Used to initialize objects.

class Person:
def __init__(self, name, age):
self.name = name
self.age = age



2. Class Attributes vs Instance Attributes

Written for

Institution
Secondary school
School year
5

Document information

Uploaded on
November 26, 2024
Number of pages
16
Written in
2024/2025
Type
Class notes
Professor(s)
Dr avinabh juneyja
Contains
All classes

Subjects

$31.80
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
mohd2125csit1129

Get to know the seller

Seller avatar
mohd2125csit1129 kiet
Follow You need to be logged in order to follow users or courses
Sold
2
Member since
1 year
Number of followers
2
Documents
14
Last sold
1 year ago

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions