0% found this document useful (0 votes)
1 views3 pages

Python Code Examples

The document provides simple and intermediate Python code examples, including basic operations like printing, arithmetic, and control structures. It covers topics such as checking even or odd numbers, calculating factorials, generating Fibonacci sequences, and working with lists and dictionaries. Additionally, it demonstrates error handling using try-except blocks.
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)
1 views3 pages

Python Code Examples

The document provides simple and intermediate Python code examples, including basic operations like printing, arithmetic, and control structures. It covers topics such as checking even or odd numbers, calculating factorials, generating Fibonacci sequences, and working with lists and dictionaries. Additionally, it demonstrates error handling using try-except blocks.
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
You are on page 1/ 3

Simple and Intermediate Python Code Examples

1. Print Hello World

print("Hello, World!")

Explanation: This is the most basic Python program that prints text to the screen.

2. Sum of Two Numbers

a = 5

b = 3

sum = a + b

print("Sum:", sum)

Explanation: This code adds two numbers and prints the result.

3. Check Even or Odd

num = 4

if num % 2 == 0:

print("Even")

else:

print("Odd")

Explanation: This checks if a number is even or odd using the modulus operator.

4. Factorial Using Loop

n = 5

factorial = 1

for i in range(1, n+1):

factorial *= i

print("Factorial:", factorial)

Explanation: This calculates the factorial of a number using a for loop.

5. Fibonacci Sequence

a, b = 0, 1

for _ in range(10):

print(a, end=" ")


a, b = b, a + b

Explanation: This prints the first 10 numbers in the Fibonacci sequence.

6. Check Prime Number

num = 7

is_prime = True

for i in range(2, int(num ** 0.5) + 1):

if num % i == 0:

is_prime = False

break

print("Prime" if is_prime else "Not Prime")

Explanation: This checks if a number is prime by testing divisibility.

7. List Comprehension

squares = [x**2 for x in range(10)]

print(squares)

Explanation: This creates a list of squares using list comprehension.

8. Function with Return Value

def greet(name):

return "Hello, " + name

print(greet("Alice"))

Explanation: This defines a function that returns a greeting message.

9. Dictionary Operations

student = {"name": "John", "age": 20}

print(student["name"])

student["grade"] = "A"

print(student)

Explanation: This demonstrates basic dictionary operations like access and update.

10. Using Try-Except

try:

num = int("abc")
except ValueError:

print("Invalid input")

Explanation: This handles exceptions using try-except blocks.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy