0% found this document useful (0 votes)
4 views12 pages

Python Code Examples for Beginners

The document provides a series of Python code examples covering basic programming concepts such as printing, variables, arithmetic operations, lists, dictionaries, conditional statements, loops, functions, classes, file reading, and exception handling. Each example includes code snippets along with their expected output. It serves as a practical guide for beginners to understand fundamental Python programming techniques.

Uploaded by

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

Python Code Examples for Beginners

The document provides a series of Python code examples covering basic programming concepts such as printing, variables, arithmetic operations, lists, dictionaries, conditional statements, loops, functions, classes, file reading, and exception handling. Each example includes code snippets along with their expected output. It serves as a practical guide for beginners to understand fundamental Python programming techniques.

Uploaded by

zendenilam
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Code Examples

Example 1: Hello World

The classic first program:

print("Hello, World!")

Output: Hello, World!

Page 1
Python Code Examples

Example 2: Variables

Assigning and using variables:

name = "Alice"

age = 25

print(f"My name is {name} and I am {age} years old.")

Output: My name is Alice and I am 25 years old.

Page 2
Python Code Examples

Example 3: Arithmetic Operations

Basic math:

a = 10

b = 3

print(a + b) # 13

print(a * b) # 30

print(a / b) # 3.333...

Page 3
Python Code Examples

Example 4: Lists

Working with lists:

fruits = ["apple", "banana", "cherry"]

[Link]("orange")

print(fruits)

print(len(fruits))

Output: ["apple", "banana", "cherry", "orange"]

Page 4
Python Code Examples

Example 5: Dictionaries

Using dictionaries:

person = {"name": "Bob", "age": 30, "city": "New York"}

print(person["name"])

person["job"] = "Engineer"

print(person)

Page 5
Python Code Examples

Example 6: If-Else Statement

Conditional logic:

x = 10

if x > 5:

print("x is greater than 5")

else:

print("x is not greater than 5")

Output: x is greater than 5

Page 6
Python Code Examples

Example 7: For Loop

Iterating with for loop:

for i in range(5):

print(i)

Output: 0 1 2 3 4

Page 7
Python Code Examples

Example 8: While Loop

While loop example:

i = 0

while i < 3:

print(f"Count: {i}")

i += 1

Output: Count: 0

Count: 1

Count: 2

Page 8
Python Code Examples

Example 9: Functions

Defining and calling functions:

def square(x):

return x * x

result = square(4)

print(result)

Output: 16

Page 9
Python Code Examples

Example 10: Classes

Object-oriented programming:

class Dog:

def __init__(self, name):

[Link] = name

def bark(self):

return f"{[Link]} says woof!"

dog = Dog("Buddy")

print([Link]())

Output: Buddy says woof!

Page 10
Python Code Examples

Example 11: File Reading

Reading from a file:

with open("[Link]", "r") as file:

content = [Link]()

print(content)

Page 11
Python Code Examples

Example 12: Exception Handling

Handling errors:

try:

result = 10 / 0

except ZeroDivisionError:

print("Cannot divide by zero")

finally:

print("Done")

Output: Cannot divide by zero

Done

Page 12

You might also like