0% found this document useful (0 votes)
9 views3 pages

Code Ref

Uploaded by

mvjkumar1978
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Code Ref

Uploaded by

mvjkumar1978
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

# Sample dictionary

sample_dict = {

"name": "Alice",

"age": 25,

"city": "New York",

"is_student": False

# 1. Loop using key,value of dict

print("Dictionary Iteration:")

for key, value in sample_dict.items():

if isinstance(value, int) and value > 20: # Conditional logic

print(f"{key} -> {value}")

# 2. Loop using only key or value

for key in sample_dict:

print(f"Key: {key}")

#3. embeded loop and write to new dict

sample_dict_new = {key: value**2 for key, value in sample_dict.items()}

#4. Loop for and if elif and else

for key, value in sample_dict.items():

if key == "age" and value > 30:

print(f"{key}: {value} (Age is above 30)")

elif key == "is_employed" and value:

print(f"{key}: {value} (Person is employed)")

else:

print(f"{key}: {value}")

#--------------------------------------------

my_list = [1, 2, 3, 4, 5]

# Loop through elements

for element in my_list:


print(f"Element: {element}")

# Loop through indices

for i in range(len(my_list)):

print(f"Index: {i}, Element: {my_list[i]}")

# Enumerate index and element

for index, element in enumerate(my_list):

print(f"Index: {index}, Element: {element}")

# Iterate through outer and inner lists

for sublist in my_list:

for item in sublist:

print(item)

# Iterate through sorted list

for element in sorted(my_list):

print(element)

#--------------------------------------------

import pandas as pd

# Sample DataFrame

data = {'Name': ['Alice', 'Bob', 'Charlie'],

'Age': [30, 25, 35],

'Department': ['HR', 'IT', 'Finance']}

df = [Link](data)

# Iterate through rows

for index, row in [Link]():

print(f"Index: {index}, Name: {row['Name']}, Age: {row['Age']}, Department:


{row['Department']}")

Index: 0, Name: Alice, Age: 30, Department: HR

for row in [Link]():

print(f"Index: {[Link]}, Name: {[Link]}, Age: {[Link]}, Department:


{[Link]}")
# Iterate through each element

for i in range(len(df)):

for j in range(len([Link])):

print(f"Row {i}, Column {j}: {[Link][i, j]}")

df['Info'] = df['Name'] + " (" + df['Department'] + ", Age: " + df['Age'].astype(str) + ")"

print(df)

#---------

add = lambda x, y: x + y

# Use the lambda function

result = add(5, 3)

You might also like