Python Day 1 Training Notes
Python Day 1 Training Notes
# This is a comment
print("Hello, Python!")
# Java style:
# if (condition) {
# System.out.println("True");
# }
# Python style:
if condition:
print("True")
# Dynamic typing
x = 10 # int
x = "hello" # now a string
x = 3.14 # now a float
# List (mutable)
my_list = [1, 2, 3, "four", 5.0]
my_list.append(6)
print(my_list[0]) # Access by index: 1
# Tuple (immutable)
my_tuple = (1, 2, "three")
age = 18
if age < 18:
print("Minor")
elif age == 18:
print("Just turned adult")
else:
print("Adult")
Loops
for loops with in keyword (different from Java's C-style for)
while loops
List comprehensions (unique to Python)
python
# While loop
count = 0
while count < 5:
print(count)
count += 1
Default parameters
Variable arguments with *args and **kwargs
python
# Basic function
def greet(name):
return f"Hello, {name}!"
# Variable arguments
def sum_all(*numbers):
return sum(numbers)
print(sum_all(1, 2, 3, 4)) # 10
# Keyword arguments
def build_profile(**user_info):
return user_info
def count_even_odd(numbers):
result = {"even": 0, "odd": 0}
return result