Skip to content

sabhas808922/Master_py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 

Repository files navigation

๐Ÿ”น Python Basics โ€“ Learn with Code Examples & Comments

This guide covers essential Python concepts with detailed explanations and code examples for Lists, Dictionaries, Loops, and Functions.


1๏ธโƒฃ Lists in Python

A list is an ordered, mutable (changeable) collection of elements. It can store multiple data types.

๐Ÿ”น Creating Lists

# Creating a list
fruits = ["apple", "banana", "cherry", "mango"]

# Mixed data types
random_list = [10, "hello", 3.14, True]

# Empty list
empty_list = []

๐Ÿ”น Accessing List Elements

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

print(fruits[0])  # Output: apple (first element)
print(fruits[-1]) # Output: cherry (last element)

๐Ÿ”น Modifying Lists

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

# Changing an element
fruits[1] = "orange"
print(fruits)  # Output: ['apple', 'orange', 'cherry']

# Adding elements
fruits.append("grapes")  # Adds to the end
print(fruits)

fruits.insert(1, "blueberry")  # Adds at index 1
print(fruits)

# Removing elements
fruits.remove("apple")  # Removes by value
print(fruits)

deleted_item = fruits.pop(2)  # Removes item at index 2
print(f"Removed: {deleted_item}")

๐Ÿ”น List Slicing

numbers = [0, 1, 2, 3, 4, 5, 6, 7]

print(numbers[2:5])   # Output: [2, 3, 4] (index 2 to 4)
print(numbers[:4])    # Output: [0, 1, 2, 3] (start to index 3)
print(numbers[3:])    # Output: [3, 4, 5, 6, 7] (index 3 to end)
print(numbers[::2])   # Output: [0, 2, 4, 6] (step of 2)

2๏ธโƒฃ Dictionaries in Python

A dictionary is an unordered collection of key-value pairs.

๐Ÿ”น Creating a Dictionary

# Creating a dictionary
student = {
    "name": "John",
    "age": 20,
    "grade": "A",
    "subjects": ["Math", "Science"]
}

# Accessing values
print(student["name"])  # Output: John
print(student.get("age"))  # Output: 20

๐Ÿ”น Modifying a Dictionary

student["age"] = 21  # Modifying an existing key
student["city"] = "New York"  # Adding a new key
print(student)

๐Ÿ”น Deleting a Key

del student["grade"]  # Removes 'grade' key
print(student)

removed_value = student.pop("age")  # Removes and returns the value
print(f"Removed Age: {removed_value}")

๐Ÿ”น Looping Through a Dictionary

for key, value in student.items():
    print(f"{key}: {value}")

3๏ธโƒฃ Loops in Python

๐Ÿ”น for Loop

Used to iterate over sequences like lists, strings, and dictionaries.

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

for fruit in fruits:
    print(f"I like {fruit}")  

Looping with range()

for i in range(5):  # Loops from 0 to 4
    print(i)

Looping Through a Dictionary

student = {"name": "John", "age": 20, "grade": "A"}

for key, value in student.items():
    print(f"{key}: {value}")

๐Ÿ”น while Loop

Used when the number of iterations is unknown.

x = 0
while x < 5:
    print(f"x is {x}")
    x += 1  # Incrementing x

๐Ÿ”น break and continue

  • break: Stops the loop.
  • continue: Skips the current iteration.
for num in range(10):
    if num == 5:
        break  # Stops when num is 5
    print(num)

for num in range(5):
    if num == 2:
        continue  # Skips 2
    print(num)

4๏ธโƒฃ Functions in Python

A function is a reusable block of code that performs a specific task.

๐Ÿ”น Defining and Calling Functions

def greet():
    print("Hello, welcome!")

greet()  # Calling the function

๐Ÿ”น Function with Parameters

def greet_user(name):
    print(f"Hello, {name}!")

greet_user("Alice")  # Output: Hello, Alice!

๐Ÿ”น Function with Return Value

def add(a, b):
    return a + b

result = add(3, 5)
print(result)  # Output: 8

๐Ÿ”น Default Arguments

def greet(name="Guest"):
    print(f"Hello, {name}!")

greet()       # Output: Hello, Guest!
greet("Alex") # Output: Hello, Alex!

๐Ÿ”น Lambda Functions (Anonymous Functions)

square = lambda x: x * x
print(square(4))  # Output: 16

add = lambda a, b: a + b
print(add(3, 7))  # Output: 10

๐Ÿ”ฅ Summary

โœ… Lists โ†’ Ordered, mutable collection. Supports indexing, slicing, modification.
โœ… Dictionaries โ†’ Unordered, key-value pairs. Fast lookups.
โœ… Loops โ†’ for and while loops for iteration. Use break & continue.
โœ… Functions โ†’ Code reusability, parameters, return values, default arguments, lambda functions.


๐Ÿš€ Happy Coding! ๐ŸŽฏ

About

A Complete Beginner-Friendly Guide to Python to Advance Libraries NumPy Pandas Scikit learn and many more to discover AI and ML capabilities in DEEP LEARNING

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors