Python Basics Tutorial
Learn Python programming basics including variables, data types, conditional statements, and loops. These are essential fundamentals for playing The Farmer Was Replaced programming game.
1. Variables
Variables are containers for storing data. In Python, you can assign values directly without declaring types.
# Variable assignment examples
name = "John"
age = 25
height = 1.75
Python automatically identifies data types: string, integer, float, etc.
2. Data Types
Python has the following basic data types:
# Data type examples
text = "Hello World" # String
number = 42 # Integer
decimal = 3.14 # Float
is_true = True # Boolean
items = [1, 2, 3] # List
3. Basic Operations
Python supports various mathematical and logical operations:
# Arithmetic operations
a = 10 + 5 # Addition
b = 10 - 3 # Subtraction
c = 10 * 2 # Multiplication
d = 10 / 3 # Division
# Comparison operations
x = 5 > 3 # True
y = 5 < 3 # False
z = 5 == 5 # True
4. Conditional Statements
Use if, elif, else to execute different code blocks based on conditions:
age = 18
if age < 18:
print("Underage")
elif age == 18:
print("Just turned adult")
else:
print("Adult")
5. Lists
Lists are ordered collections that store multiple items:
# Create a list
fruits = ["Apple", "Banana", "Orange"]
# Access element
first_fruit = fruits[0] # "Apple"
# Add element
fruits.append("Grape")
# Iterate over list
for fruit in fruits:
print(fruit)
6. For Loops
For loops are used to iterate over each element in a sequence:
# Iterate over list
animals = ["Dog", "Cat", "Bird"]
for animal in animals:
print("I like", animal)
# Numeric loop
for i in range(5):
print("Number:", i)
7. While Loops
While loops repeat code blocks while the condition is true. This is the most important loop structure in The Farmer Was Replaced:
# Basic while loop
count = 0
while count < 5:
print("Count:", count)
count = count + 1
# Infinite loop (common in games)
while True:
# Get the next action
action = get_next_action()
if action == "Stop":
break
execute_action(action)
8. Functions
Functions are reusable code blocks that perform specific tasks:
# Define function
def greet(name):
return "Hello, " + name + "!"
# Call function
message = greet("Xiao Ming")
print(message)
# Function with multiple parameters
def calculate_area(length, width):
return length * width
area = calculate_area(5, 3)
print("Area:", area)
Game Requirements
To start playing The Farmer Was Replaced, you need to master these core concepts:
- Creating and using variables
- Basic data types (strings, numbers, booleans)
- Conditional statements (if statements)
- Basic list operations
- Using while loops
- Defining and calling functions