Your cart is currently empty!
Writing Your First Python Program
Back to: Coding With AI
Writing Your First Python Program
Welcome to your first hands-on experience with Python, a powerful and beginner-friendly programming language. This lesson is all about setting up your coding environment, understanding basic concepts, and writing your first Python script. Python will be the foundation for many exciting projects, including coding with artificial intelligence!
What is Python?
Python is one of the most popular and versatile programming languages in the world. It is used in a wide variety of fields, including web development, data analysis, artificial intelligence, machine learning, and even creative fields like game and animation development. Python is known for its simplicity, readability, and extensive library support, making it an excellent choice for beginners and experts alike.
Step 1: Setting Up Your Coding Environment
Before we can start coding, we need to ensure Python is installed on your computer and that you have the right tools to write and execute code.
- Install Python:
- Visit the official Python website.
- Download the latest version of Python for your operating system (Windows, macOS, or Linux).
- Follow the step-by-step installation guide provided on the website. During installation, check the box that says “Add Python to PATH” to ensure easy access from your terminal or command prompt.
- Install an IDE (Integrated Development Environment):
- An IDE provides a user-friendly platform to write, edit, and run your code. For beginners, we recommend:
- Thonny: A simple IDE specifically designed for Python beginners.
- Visual Studio Code: A versatile and widely used editor with excellent Python support.
- Download and install your preferred IDE, then open it to familiarize yourself with its interface.
- Verify Installation:
- Open your terminal or command prompt.
- Type
python --version
orpython3 --version
to confirm Python is installed correctly. - If the command outputs the version number, you’re ready to go!
- Set Up a Practice Folder:
- Create a folder on your computer named “Python Practice” to store all your scripts and projects. This will help you stay organized as you progress.
Step 2: Understanding Python Basics
Learning Python basics is crucial before you can dive into building programs. Let’s break it down in a simple and detailed way:
1. What Are Variables and Data Types?
- Variables: Think of variables as containers for storing information. When you write:
name = "Alice"
You are telling Python to remember the name “Alice.” You can use name
anywhere in your code to refer to “Alice.”
- Data Types: Variables can store different kinds of data. For example:
age = 12 # Integer (whole number)
pi = 3.14 # Float (decimal number)
is_student = True # Boolean (True or False)
These types help Python decide how to work with the stored data.
2. How to Print Information to the Screen
- Printing is like giving your program a voice. Use the
print()
function to display messages or results:
print("Hello, World!")
- You can also use it to show the contents of variables:
name = "Alice"
print("Your name is " + name)
3. How to Take Input from Users
input()
lets your program ask users for information:
name = input("What is your name? ")
print("Hello, " + name + "!")
- Here’s how it works:
- Python shows the question.
- The user types an answer.
- The answer is stored in the
name
variable.
4. What Are Comments and Why Use Them?
- Comments are notes for you and others who read your code. Python ignores them when running the program.
# This is a comment explaining the next line
print("This line will execute.")
- Use comments to make your code easier to understand.
Step 3: Writing Your First Python Program
Let’s bring everything together to create your first Python program!
1. Create a New File:
- Open your IDE and start a new file.
- Save it as
first_program.py
to give it a meaningful name.
2. Write Your First Script:
Copy and paste the following code into your file:
# This is your first Python program!
print("Welcome to Python!")
# Ask the user for their name
name = input("What is your name? ")
# Greet the user
print("Hello, " + name + "! Let's start learning Python together!")
3. Run Your Program:
- Save your file by clicking File > Save or pressing Ctrl+S (Command+S on Mac).
- If you’re using an IDE, look for a “Run” button. In Thonny, for example, it looks like a green triangle.
- Alternatively, use the terminal:
- Navigate to the folder where you saved your file (e.g., “Python Practice”).
- Type the following command:
python first_program.py
- Watch your program come to life!
4. Interact with Your Program:
- Follow the prompts, type your name, and see your personalized greeting.
- Congratulations! You’ve just run your first Python script.
Step 4: Experimenting with Your Program
Now that you’ve built a basic script, let’s take it up a notch. Here are some ways to make it more fun:
1. Add More Questions
Make the program ask the user for their age:
age = int(input("How old are you? "))
print("Wow, you’re " + str(age) + " years old!")
2. Perform Simple Calculations
Use Python to calculate the user’s age in 10 years:
future_age = age + 10
print("In 10 years, you’ll be " + str(future_age) + " years old!")
3. Introduce Simple Math Operations
Build a program that performs basic math tasks like addition and multiplication:
num1 = int(input("Enter a number: "))
num2 = int(input("Enter another number: "))
print("The sum is: " + str(num1 + num2))
print("The product is: " + str(num1 * num2))
4. Add a Loop
Use a loop to repeat questions until the user types “exit”:
while True:
response = input("Type 'exit' to quit or 'continue' to keep going: ")
if response.lower() == 'exit':
break
else:
print("Great, let’s continue!")
Copyright 2024 MAIS Solutions, LLC All Rights Reserved