# Print Statement print("Hello, World!")# Variables x=5y="Hello"# Data Types int_var=10# Integer float_var=10.5# Float str_var="Hello"# String bool_var=True# Boolean list_var=[1,2,3]# List tuple_var=(1,2,3)# Tuple set_var={1,2,3}# Set dict_var={"key":"value"}# Dictionary
Control Structures
# If-Else ifx>0:print("Positive")elifx==0:print("Zero")else:print("Negative")# For Loop foriinrange(5):print(i)# While Loop count=0whilecount<5:print(count)count+=1
try:result=10/0exceptZeroDivisionError:print("Cannot divide by zero!")finally:print("Execution complete.")
File Operations
# Read from a file withopen('file.txt','r')asfile:content=file.read()print(content)# Write to a file withopen('file.txt','w')asfile:file.write("Hello, World!")
List Comprehensions
# Basic List Comprehension squares=[x**2forxinrange(10)]print(squares)# Conditional List Comprehension evens=[xforxinrange(10)ifx%2==0]print(evens)
Lambda Functions
# Lambda Function add=lambdaa,b:a+bprint(add(5,3))
Top comments (0)