- What Python scripts are ā understanding scripts vs programs
- Your first script ā "Hello, World!" and beyond
- Scripts with variables ā storing and manipulating data
- Scripts with input/output ā interacting with users
- Scripts with conditionals ā making decisions
- Scripts with loops ā repeating actions
- Scripts with functions ā organizing code
- Practical examples ā real-world scripts
- Hands-on practice with the interactive editor
What are Python Scripts?
A Python script is a file containing Python code that can be executed to perform a specific task. Unlike a full-fledged program, a script is typically smaller, focused on a single purpose, and designed to be run from the command line or an editor.
Think of scripts as recipes ā they contain step-by-step instructions that Python follows to produce a result. Writing scripts is the best way to learn Python because you can start small and gradually build more complex programs.
# A simple Python script
print("Hello, Python!")
1. Your First Python Script
Every Python journey starts with the classic "Hello, World!" script. It's simple but teaches you the essential structure of a Python program.
# Hello, World! - Your First Python Script
print("Hello, World!")
# Output:
# Hello, World!
# A slightly more interesting version
name = "Python"
print(f"Hello, {name}!")
# Output:
# Hello, Python!
2. Scripts with Variables
Variables store data that can be used and manipulated throughout your script. They are the building blocks of any Python program.
# Script with variables
name = "Alice"
age = 25
height = 5.6
is_student = True
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Height: {height} feet")
print(f"Is student? {is_student}")
# Output:
# Name: Alice
# Age: 25
# Height: 5.6 feet
# Is student? True
3. Scripts with Input/Output
Interactive scripts use input() to get data from users and print() to display results.
# Interactive script
name = input("What's your name? ")
age = int(input("How old are you? "))
print(f"Hello, {name}!")
print(f"You will be {age + 1} years old next year!")
# Output:
# What's your name? (user types: Bob)
# How old are you? (user types: 30)
# Hello, Bob!
# You will be 31 years old next year!
4. Scripts with Conditionals
Conditionals (if, elif, else) allow scripts to make decisions based on conditions.
# Script with conditionals
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult.")
elif age >= 13:
print("You are a teenager.")
else:
print("You are a child.")
# Output:
# Enter your age: 16
# You are a teenager.
5. Scripts with Loops
Loops (for and while) allow scripts to repeat actions multiple times.
# Script with for loop
print("Counting to 5:")
for i in range(1, 6):
print(i)
# Script with while loop
count = 0
print("Counting down from 3:")
while count < 3:
print(3 - count)
count += 1
# Output:
# Counting to 5:
# 1
# 2
# 3
# 4
# 5
# Counting down from 3:
# 3
# 2
# 1
6. Scripts with Functions
Functions help organize code into reusable blocks. They make scripts more readable and maintainable.
# Script with functions
def greet(name):
return f"Hello, {name}!"
def calculate_area(length, width):
return length * width
# Main script
person = "Charlie"
print(greet(person))
length = 5
width = 3
area = calculate_area(length, width)
print(f"Area of rectangle: {area}")
# Output:
# Hello, Charlie!
# Area of rectangle: 15
7. Practical Script Examples
Here are some practical scripts you can create to solve everyday problems.
a. Temperature Converter
# Temperature Converter
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}°C is equal to {fahrenheit}°F")
b. Simple Calculator
# Simple Calculator
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
operator = input("Enter operator (+, -, *, /): ")
if operator == '+':
result = a + b
elif operator == '-':
result = a - b
elif operator == '*':
result = a * b
elif operator == '/':
result = a / b
else:
result = "Invalid operator"
print(f"Result: {result}")
c. Guess the Number Game
# Guess the Number Game
import random
secret = random.randint(1, 10)
print("Guess a number between 1 and 10")
while True:
guess = int(input("Your guess: "))
if guess == secret:
print("Correct! You win!")
break
elif guess < secret:
print("Too low!")
else:
print("Too high!")
Try It Yourself!
Experiment with Python scripts directly in your browser. Modify the code and see the results in real time.
WELCOME TO PYTHON SCRIPTS
========================================
1. HELLO WORLD
Hello, Python!
2. VARIABLES
Name: Alice, Age: 25
3. INPUT/OUTPUT
Try changing the code below!
4. CONDITIONALS
7 is odd
5. LOOPS
Counting to 5:
1 2 3 4 5
6. FUNCTIONS
Hello, Python Learner!
ā You're writing Python scripts!
š You've Mastered Python Simple Scripts!
You understand variables, input/output, conditionals, loops, functions, and practical script examples. You're now ready to write your own Python scripts!
Quick Quiz ā Test Your Knowledge
Let's see what you've learned about Python simple scripts:
for i in range(3): print(i)?Frequently Asked Questions
š¤ What's the difference between a script and a program? ā¼
š§ How do I run a Python script? ā¼
python script_name.py or from an IDE like VS Code, PyCharm, or Jupyter Notebook.
š What is the if __name__ == "__main__" block?
ā¼
š What's the best way to learn Python scripting? ā¼
š Where to Go From Here
Now that you understand Python simple scripts, here are some related topics to explore:
š Variables & Identifiers
Learn about variable naming rules
š„ Input/Output
Master input() and print()
šÆ Decision Making
Learn if-else statements