Join this channel to learn data science, artificial intelligence and machine learning with funny quizzes, interesting projects and amazing resources for free
For collaborations: @love_data
Post #4458
2.9K
- ๐ 2
- โค 1
DA @datasciencefun
Showing posts older than #4459 ยท Back to latest
student = {
"name": "Rahul",
"age": 23
}
print(student.keys())
print(student.values())
print(student.items())students = [
{"name": "Amit", "marks": 90},
{"name": "Sara", "marks": 85}
]
for student in students:
print(student["name"], student["marks"])
Amit 90
Sara 85
colors = ("Red", "Green")
colors[0] = "Blue"TypeError because tuples are immutable.student = {"name": "John"}
print(student["age"])KeyError.print(student.get("age"))fruits = ["Apple", "Banana", "Mango"]
print(fruits)
['Apple', 'Banana', 'Mango']
print(fruits[0])
print(fruits[2])
Apple
Mango
fruits[1] = "Orange"
print(fruits)
['Apple', 'Orange', 'Mango']
fruits.append("Grapes")
print(fruits)fruits.remove("Orange")
print(fruits)colors = ("Red", "Green", "Blue")
print(colors)print(colors[1])
Green
numbers = {1, 2, 2, 3, 4, 4, 5}
print(numbers){1, 2, 3, 4, 5}numbers.add(6)
numbers.remove(3)
student = {
"name": "Deepak",
"age": 24,
"course": "Data Science"
}
print(student)print(student["name"])
Deepak
student["city"] = "Mumbai"
student["age"] = 25
del student["course"]
numbers = [10, 20, 30]
numbers.append(40)
numbers.insert(1, 15)
numbers.remove(20)
numbers.sort()
print(numbers)
def greet():
print("Welcome to Data Science!")
greet()
Welcome to Data Science!
def function_name():
# Function body
def welcome():
print("Hello, World!")
welcome()
def greet(name):
print("Hello", name)
greet("Deepak")
Hello Deepak
def square(number):
print(number * number)
square(5)
25
def add(a, b):
return a + b
result = add(10, 20)
print(result)
30
def greet(name="Guest"):
print("Hello", name)
greet()
greet("Rahul")
Hello Guest
Hello Rahul
def calculate(a, b):
return a + b, a * b
sum_value, product = calculate(4, 5)
print(sum_value)
print(product)
9
20
def demo():
message = "Inside Function"
print(message)
demo()
message
numbers = [5, 2, 8, 1]
print(len(numbers))
print(max(numbers))
print(min(numbers))
print(sum(numbers))
4
8
1
16
def average(marks):
return sum(marks) / len(marks)
scores = [80, 75, 92, 88]
print(average(scores))
83.75
def greet():
print("Hello")
# Nothing happens because the function isn't called.
greet()
def add(a, b):
a + b
# Correct:
def add(a, b):
return a + b
sales = [1000, 2000, 1500, 3000]
total = 0
for amount in sales:
total += amount
print(total)
7500count = 1
while count <= 5:
print(count)
count never changes.count = 1
while count <= 5:
print(count)
count += 1
print("Hello")
print("Hello")
print("Hello")
print("Hello")
print("Hello")for i in range(5):
print("Hello")
for variable in sequence:
# Code to execute
for i in range(5):
print(i)
0
1
2
3
4
range(5) generates numbers from 0 to 4.range() function generates a sequence of numbers.for i in range(5):
print(i)
0 1 2 3 4for i in range(1, 6):
print(i)
1 2 3 4 5for i in range(2, 11, 2):
print(i)
2 4 6 8 10 fruits = ["Apple", "Banana", "Mango"]
for fruit in fruits:
print(fruit)
Apple
Banana
Mango
while condition:
# Code
count = 1
while count <= 5:
print(count)
count += 1
1 2 3 4 5while True:
print("Hello")
for i in range(10):
if i == 5:
break
print(i)
0 1 2 3 4for i in range(5):
if i == 2:
continue
print(i)
0 1 3 4for i in range(5):
pass
for i in range(3):
for j in range(2):
print(i, j)
0 0
0 1
1 0
1 1
2 0
2 1

if age >= 18:
print("Eligible")
if age >= 18:
print("Eligible")
if age >= 18:
print("Eligible")
prediction = 0.82
if prediction >= 0.5:
print("Spam Email")
else:
print("Not Spam")
if condition:
# Code to execute
age = 20
if age >= 18:
print("Eligible to vote")
Eligible to vote
age = 16
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
Not eligible to vote
marks = 85
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 60:
print("Grade C")
else:
print("Grade D")
Grade B
age = 25
citizen = True
if age >= 18:
if citizen:
print("Eligible to vote")
Eligible to vote
age = 25
if age >= 18 and age <= 60:
print("Working Age")
marks = 35
if marks >= 40 or marks == 35:
print("Eligible for Grace Marks")
is_holiday = False
if not is_holiday:
print("Go to Office")
salary = 60000
experience = 4
if salary > 50000 and experience >= 3:
print("Eligible for Promotion")
else:
print("Not Eligible")
value_if_true if condition else value_if_false
age = 20
status = "Adult" if age >= 18 else "Minor"
print(status)
Adult
temperature = 38
if temperature > 35:
print("It's a hot day.")
elif temperature >= 20:
print("Weather is pleasant.")
else:
print("It's cold.")
if age >= 18
print("Eligible")
SyntaxError.