Python Basic Syntax
Python Basic Syntax
Printing Output
(Prints text to the console)
# Print "Hello, World!" to the screen
print("Hello, World!")
Output:
Hello, World!
age = 25 # Integer
# Printing variables
Output:
Alice 25 5.6 True
Example Input:
Enter your name: Bob
Output:
Hello, Bob
num = -5
if num > 0:
print("Positive")
print("Negative")
else:
print("Zero")
Output:
Negative
print(i)
count = 0
print(count)
count += 1
Output:
0
6. Functions
(Reusable blocks of code that perform a specific task)
# Defining a function that takes a name and returns a greeting
def greet(name):
print(greet("Alice"))
Output:
Hello, Alice
7. Nested Functions
(A function inside another function)
# Function with a nested function
def outer_function(name):
def inner_function():
outer_function("Bob")
Output:
Outer function running...
8. Lists (Arrays)
(Used to store multiple values in a single variable)
# Creating a list of fruits
fruits.append("Orange")
print(fruits)
Output:
Banana
print(person["name"])
print(person["age"])
print(person)
Output:
Alice
25
Writing to a File
# Open a file in write mode and add text
content = file.read()
print(content)
x = 10 / 0
except ZeroDivisionError:
Output:
Cannot divide by zero!