Raspberry Pi Teaching Introduction for Teachers: Difference between revisions
Jump to navigation
Jump to search
(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(...") |
No edit summary |
||
| (6 intermediate revisions by the same user not shown) | |||
| Line 39: | Line 39: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= Loops (For and While) = | = Loops (For and While) = | ||
| Line 52: | Line 51: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= Lists (Arrays) = | = Lists (Arrays) = | ||
| Line 120: | Line 118: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= Create a Class (Object Oriented Programming) = | |||
<syntaxhighlight lang="python"> | |||
class Student: | |||
def __init__(self, name, age): | |||
self.name = name | |||
self.age = age | |||
student1 = Student("Alice", 20) | |||
print(student1.name, student1.age) | |||
</syntaxhighlight> | |||
= Fibonacci Series = | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
def fibonacci(n): | |||
a, b = 0, 1 | |||
while a < n: | |||
print(a, end=' ') | |||
a, b = b, a + b | |||
fibonacci(1000) | |||
</syntaxhighlight> | |||
= Simple Pygame Setup = | |||
<syntaxhighlight lang="python"> | |||
import pygame | |||
import random | |||
pygame.init() | |||
screen = pygame.display.set_mode((400, 300)) | |||
running = True | |||
player_x = 200 | |||
player_y = 150 | |||
target_x = random.randint(0, 400) | |||
target_y = random.randint(0, 300) | |||
while running: | |||
for event in pygame.event.get(): | |||
if event.type == pygame.QUIT: | |||
running = False | |||
# Clear the screen | |||
screen.fill((0, 0, 0)) | |||
# Draw the player as a red rectangle | |||
pygame.draw.rect(screen, (255, 0, 0), (player_x, player_y, 20, 20)) | |||
# Draw the target as a green rectangle | |||
pygame.draw.rect(screen, (0, 255, 0), (target_x, target_y, 20, 20)) | |||
pygame.display.flip() | |||
pygame.quit() | |||
</syntaxhighlight> | </syntaxhighlight> | ||
= Additional Turtle Instructions = | |||
[[File:Screenshot 2023-10-18 at 5.41.46 am.png | 900px]] | |||
== Change the Turtle's Color == | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
turtle.color("red") | |||
</syntaxhighlight> | |||
== Change the Turtle's Fill Color == | |||
<syntaxhighlight lang="python"> | |||
turtle.fillcolor("blue") | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== Change the Turtle's Pen Size == | |||
<syntaxhighlight lang="python"> | |||
turtle.pensize(3) | |||
</syntaxhighlight> | |||
== Draw a Circle== | |||
<syntaxhighlight lang="python"> | |||
turtle.circle(50) | |||
</syntaxhighlight> | |||
== Draw a Square == | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
for _ in range(4): | |||
turtle.forward(100) | |||
turtle.right(90) | |||
</syntaxhighlight> | |||
== Draw a Triangle == | |||
<syntaxhighlight lang="python"> | |||
for _ in range(3): | |||
turtle.forward(100) | |||
turtle.left(120) | |||
</syntaxhighlight> | |||
== Draw a Star == | |||
<syntaxhighlight lang="python"> | |||
for _ in range(5): | |||
turtle.forward(100) | |||
turtle.right(144) | |||
</syntaxhighlight> | |||
== Change Turtle's Speed == | |||
<syntaxhighlight lang="python"> | |||
turtle.speed(1) # Set speed from 0 (fastest) to 10 (slowest) | |||
</syntaxhighlight> | |||
== Hide the Turtle == | |||
<syntaxhighlight lang="python"> | |||
turtle.hideturtle() | |||
</syntaxhighlight> | |||
== Stamp the Turtle == | |||
<syntaxhighlight lang="python"> | |||
turtle.stamp() | |||
</syntaxhighlight> | |||
== Write Text == | |||
<syntaxhighlight lang="python"> | |||
turtle.penup() | |||
turtle.goto(0, -50) | |||
turtle.pendown() | |||
turtle.write("Hello, Turtle!", align="center", font=("Arial", 12, "normal")) | |||
</syntaxhighlight> | |||
== Clear the Drawing == | |||
<syntaxhighlight lang="python"> | |||
turtle.clear() | |||
</syntaxhighlight> | |||
== Reset the Turtle == | |||
<syntaxhighlight lang="python"> | |||
turtle.reset() | |||
</syntaxhighlight> | |||
== Exit the Turtle Graphics Window == | |||
<syntaxhighlight lang="python"> | |||
turtle.bye() | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 18:48, 17 October 2023
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}")
Create a Class (Object Oriented Programming)
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
student1 = Student("Alice", 20)
print(student1.name, student1.age)
Fibonacci Series
def fibonacci(n):
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a + b
fibonacci(1000)
Simple Pygame Setup
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((400, 300))
running = True
player_x = 200
player_y = 150
target_x = random.randint(0, 400)
target_y = random.randint(0, 300)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Clear the screen
screen.fill((0, 0, 0))
# Draw the player as a red rectangle
pygame.draw.rect(screen, (255, 0, 0), (player_x, player_y, 20, 20))
# Draw the target as a green rectangle
pygame.draw.rect(screen, (0, 255, 0), (target_x, target_y, 20, 20))
pygame.display.flip()
pygame.quit()
Additional Turtle Instructions
Change the Turtle's Color
turtle.color("red")
Change the Turtle's Fill Color
turtle.fillcolor("blue")
Change the Turtle's Pen Size
turtle.pensize(3)
Draw a Circle
turtle.circle(50)
Draw a Square
for _ in range(4):
turtle.forward(100)
turtle.right(90)
Draw a Triangle
for _ in range(3):
turtle.forward(100)
turtle.left(120)
Draw a Star
for _ in range(5):
turtle.forward(100)
turtle.right(144)
Change Turtle's Speed
turtle.speed(1) # Set speed from 0 (fastest) to 10 (slowest)
Hide the Turtle
turtle.hideturtle()
Stamp the Turtle
turtle.stamp()
Write Text
turtle.penup()
turtle.goto(0, -50)
turtle.pendown()
turtle.write("Hello, Turtle!", align="center", font=("Arial", 12, "normal"))
Clear the Drawing
turtle.clear()
Reset the Turtle
turtle.reset()
Exit the Turtle Graphics Window
turtle.bye()