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

Python

Ddd

Uploaded by

Gowri
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 views9 pages

Python

Ddd

Uploaded by

Gowri
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

1)

✅ Dictionary in Python
A dictionary in Python is a collection of key-value pairs.
Each key is unique and used to access its associated value.

🔹 Key Features:
Unordered (Python 3.6+: ordered by insertion)

Mutable (can be changed)

Uses {} curly braces

Keys must be unique and immutable (like strings or numbers)

✅ Syntax:
dictionary_name = {key1: value1, key2: value2, ...}

✅ Example:
student = {
"name": "Rahul",
"age": 20,
"marks": 85
}

print(student["name"]) # Output: Rahul

🔸 Common Dictionary Methods:


Method​ Description

get(key)​ Returns the value for a key


keys()​ Returns a list of all keys
values()​ Returns a list of all values
items()​Returns a list of key-value pairs
update()​ Updates with another dictionary
pop(key)​ Removes the item with the given key
✅ Example with Methods:
print([Link]("age")) # Output: 20
student["marks"] = 90 # Update value
student["grade"] = "A" # Add new key-value pair
print(student)

📌 Conclusion:
A dictionary is a powerful data type in Python for storing and accessing related information
using keys. It is widely used in data structures and real-world applications.

✅ Conditional Statements in Python


2)

Conditional statements are used to make decisions in a program. They execute certain blocks
of code only when specific conditions are true.

🔹 Types of Conditional Statements:


Statement​ Description

if​ Executes code if condition is true


if-else​ Executes one block if true, another if false
elif​ Checks multiple conditions

✅ 1. if Statement
Executes a block only when the condition is true.

Syntax:

if condition:
# code block

Example:

x=5
if x > 0:
print("Positive number")

✅ 2. if-else Statement
Executes one block if true, another if false.

Example:

x = -3
if x >= 0:
print("Positive")
else:
print("Negative")

✅ 3. if-elif-else Statement
Used for checking multiple conditions.

Example:

marks = 85
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
else:
print("Grade C")

🔸 Nested if
An if inside another if.

Example:

x = 10
if x > 0:
if x < 20:
print("Between 1 and 19")

📌 Why Use Conditional Statements?


To make decisions in programs
To control the flow of execution

To perform different actions based on different inputs

✅ Conclusion:
Conditional statements allow a program to choose different paths based on conditions. It makes
programs dynamic, interactive, and intelligent.

3)

✅ What is a Loop in Python?


A loop in Python is used to execute a block of code repeatedly as long as a condition is true or
for each item in a sequence.

Loops help in reducing code repetition and automating repetitive tasks.

🔹 Types of Loops in Python:


1. for loop

Used to iterate over a sequence like list, tuple, string, or range.

Syntax:

for item in sequence:


# code block

Example:

for i in range(3):
print(i)
# Output: 0 1 2

2. while loop

Repeats as long as a given condition is true.


Syntax:

while condition:
# code block

Example:

x=1
while x <= 3:
print(x)
x += 1
# Output: 1 2 3

🔸 Loop Control Statements:


Statement​ Use

break​ Exits the loop immediately


continue​ Skips the current iteration
pass​ Does nothing (placeholder statement)

Example (break):

for i in range(5):
if i == 3:
break
print(i)
# Output: 0 1 2

✅ Why Use Loops?


To avoid repetition of code

To iterate through sequences (like lists or strings)

To perform repetitive tasks like printing, calculations, searching, etc.

📌 Conclusion:
A loop is a core programming concept in Python used to repeat a set of instructions. It makes
the code more efficient, readable, and reduces redundancy.
✅ List in Python
4)

A list is a built-in data structure in Python used to store multiple items in a single variable.
Lists are ordered, changeable (mutable), and allow duplicate values.

🔹 Key Features of List:


Ordered (items have a defined index)

Mutable (can be modified)

Allows duplicates

Uses square brackets []

✅ Syntax:
list_name = [item1, item2, item3, ...]

✅ Example:
fruits = ["apple", "banana", "cherry"]
print(fruits[1]) # Output: banana

🔸 Common List Operations:


✅ Example with Methods:
numbers = [10, 5, 3]
[Link](7)
[Link]()
print(numbers) # Output: [3, 5, 7, 10]
📌 Conclusion:
A list is a flexible and powerful way to store and manage collections of data in Python. It is one
of the most commonly used data types.

You might also like