Raspberry Pi Teaching Introduction for Teachers

From Sensors in Schools
Revision as of 18:21, 17 October 2023 by EdmondLascaris (talk | contribs) (Created page with " = Hello World! = <syntaxhighlight lang="python"> print("Hello, World!") </syntaxhighlight> = Variables and Printing = <syntaxhighlight lang="python"> name = "Alice" age = 30 print(f"My name is {name} and I'm {age} years old.") </syntaxhighlight> = Input from User = <syntaxhighlight lang="python"> name = input("Enter your name: ") print(f"Hello, {name}!") </syntaxhighlight> = Basic Math = <syntaxhighlight lang="python"> num1 = 5 num2 = 3 result = num1 + num2 print(...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


Hello World!

print("Hello, World!")

Variables and Printing

name = "Alice"
age = 30
print(f"My name is {name} and I'm {age} years old.")

Input from User

name = input("Enter your name: ")
print(f"Hello, {name}!")

Basic Math

num1 = 5
num2 = 3
result = num1 + num2
print(f"{num1} + {num2} = {result}")

Conditional Statements

age = 18
if age >= 18:
    print("You're an adult.")
else:
    print("You're a minor.")


Loops (For and While)

for i in range(5):
    print(i)

count = 0
while count < 5:
    print(count)
    count += 1


Lists (Arrays)

fruits = ["apple", "banana", "cherry"]
print(fruits[0])

Functions

def greet(name):
    print(f"Hello, {name}!")

greet("Bob")

Dictionaries

student = {"name": "Alice", "age": 16, "grade": "A"}
print(student["name"])

File Handling

file = open("sample.txt", "w")
file.write("Hello, File!")
file.close()

Error handling

try:
    result = 10 / 0
except ZeroDivisionError:
    print("You can't divide by zero.")

Random Number Generation

import random
number = random.randint(1, 10)
print(f"Random number: {number}")

Basic Turtle Graphics

import turtle
window = turtle.Screen()
alex = turtle.Turtle()
alex.forward(100)

Using Modules

import math
radius = 5
area = math.pi * (radius ** 2)
print(f"Area of a circle with radius {radius} is {area}")