ECCU Generation AI & Python - Code Your First Python Script
ECCU Generation AI & Python - Code Your First Python Script
Python Playground
Introduction 2
Agenda: 2
Prerequisites: 2
Materials Needed: 2
Let's Get Started! 3
Introduction to Python 3
Why Learn Python? 3
What You Will Learn 4
Introduction to Python: Setting Up Python Environment 4
Overview 4
Steps to Setting Up Your Python Environment on the Cloud 4
Part 1: Google Colab - Your Interactive Playground 4
Using Google Colab 5
Using GitHub Codespaces 6
Getting Started with Python 7
Steps to Set Up Python Environment On Your Computer: 8
Python Basics: 9
So how do you create a Variable in Python? 13
Putting it Together: A Mini-Challenge 18
One last lesson before we practice.
1
Introduction
Welcome to the STEAM Summer Camp workshop on coding your first Python script! In this
session, we will introduce you to the world of programming using one of the most popular and
versatile languages, Python. By the end of this 45-minute workshop, you will have written your
very own Python script and gained a foundational understanding of programming concepts.
Agenda:
1. Introduction to Python
■ What is Python?
■ Why learn Python?
■ Setting up an environment
2. Your First Python Script
■ Writing a simple program step by step
■ Running and testing your script
3. Basic Syntax
■ Variables
■ Data types
■ Print statement
4. Control Structures
■ Conditional statements (if-else)
5. Functions
■ Defining functions
■ Parameters and return values
6. Q&A Session
Prerequisites:
■ No prior programming experience required.
■ Please make sure you have a laptop or computer with internet access.
Materials Needed:
■ Laptop or computer
■ Internet access
■ Code editor installed (optional)
2
Let's Get Started!
We are excited to embark on this coding journey with you! Throughout this workshop, feel free to
ask questions, experiment with code samples, and most importantly, have fun while learning
how to code in Python. Let's dive into the wonderful world of programming together!
Introduction to Python
So, What's Python?
Imagine you have a super-smart friend who's really good at following instructions. You can ask
them to do calculations, organize stuff, or even create art. That's kind of like Python!
Python is a language that lets you write step-by-step instructions (called code) for computers to
understand and execute.
The language is a very flexible, beginner-friendly programming language that is widely used in
various fields. Python emphasizes readability and simplicity, making it an excellent choice for
those new to programming.
People use Python for all sorts of things, from building websites and analyzing data to creating
AI-powered robots and art! To name a few.
Fun Fact
Python was created by Guido van Rossum and first released in 1991.
In this tutorial, we will cover the basics of writing your first Python script. By the end of this
workshop, you will have a solid understanding of fundamental programming concepts and be
able to create simple programs using Python.
3
What You Will Learn
By the end of this workshop, you will learn:
Python is an excellent language for beginners due to its simplicity yet powerful capabilities.
Let's dive into our first script!
This week, we're going to be learning how to "talk" to computers and get them to do some cool
things. Just like you use apps on your phone or websites every day, we're going to build similar
stuff, but we'll be the ones behind the scenes making it work.
Imagine you have a notebook, but instead of just writing words, you can write instructions for the
computer to follow. That's what Google Colab is! It's like a super-powered notebook where
you can write code, run it, and see the results instantly.
The best part? You don't even need to install anything on your laptop! All you need is a web
browser and an internet connection.
Colab is also great because it's easy to share your work with your classmates and teachers, so
we can all learn together.
4
Using Google Colab
● What it is:
○ Google Colab is a free cloud-based Jupyter notebook environment (A web
browser-based coding tool) that allows you to write and execute Python code.–
All you need is internet access!
○ Great for collaboration, as you can share your "notebooks" with others.
○ Has all the tools we need for the first part of the camp already built-in.
● Go to Google Colab: Open your web browser and go to the Google Colab website.
● Create a New Notebook: Click on "File" > "New Python 3 Notebook" to create a new
Python notebook.
● Write Your Code: You can start writing your Python code in the cells provided.
● Run Your Code: Click on the play button next to a cell to run the code in that cell.
5
Part 2: GitHub Codespaces - Your Professional Workshop
As we get better at coding this week, you'll start building more complex things. That's where
GitHub Codespaces comes in. It's like a professional workshop where you can build bigger
projects, like the awesome chatbots we'll be creating later.
Codespaces is still in your web browser, but it's more like a full-powered coding environment.
You can store multiple files, use powerful tools, and even collaborate with your friends on
projects.
● Colab is our practice space, where we doodle and sketch out ideas.
● GitHub Codespaces is our full art studio, where we bring those ideas to life and
create a masterpiece!
● Colab is great for learning, but Codespaces lets us build bigger and more complex AI
applications.
● It also teaches you tools (like version control with Git) that are used in real-world jobs.
1. Create a GitHub Account: If you don't have one already, sign up for a free account on
GitHub.
6
3. Create a New Codespace: Click on the "New Codespace" button to create a new
development environment.
4. Choose Your Repository: Select the repository where you want to write your Python
script.
5. Start Coding: You can now write and run Python code directly in the GitHub
Codespaces environment.
We'll dive deeper into Codespaces later in the week, but just keep in mind that it's the tool you'll
use to really unleash your creativity with Python and AI.
So, are you ready to start coding? Let's dive in and have some fun!
A. PyCharm
B. Visual Studio Code
C. Jupyter Notebook
D. Datawars for Education
Some of these can also be installed on your own computer without having to go online. The
following section provides the steps of setting up the Python Environment on your Computer.
7
Steps to Set Up Python Environment On Your Computer:
I. Install Python:
A. Visit the official Python website at python.org.
B. Click on the "Downloads" tab.
C. Choose the appropriate version for your operating system (Windows,
macOS, or Linux) and download the installer.
II. Run Installer:
A. Once the installer is downloaded, run it.
B. Make sure to check the box that says "Add Python to PATH" during
installation.
III. Verify Installation:
A. Open a command prompt (Windows) or terminal (macOS/Linux).
B. Type python --version or python3 --version and press Enter.
C. You should see the installed version of Python displayed.
IV. Install an Integrated Development Environment (IDE):
A. PyCharm
B. Visual Studio Code (VsCode)
C. Jupyter Notebook
Now that you have set up your Python environment, let's dive into writing your first Python script.
In the next section, we will cover basic Python syntax and common programming constructs.
8
Python Basics:
Talking in Code: Python Syntax
Congratulations on setting up your Python environment and getting ready to code your first
Python script! Before we jump into writing any scripts, we will explore some basic Python
concepts.
Just like any language, Python has its own grammar and vocabulary. We call this the
syntax.
Learning Python is like learning the rules of this special language, so you can communicate
effectively with the computer.
We're going to jump right into Colab and write a super short program to make the computer say
hello! Think of it like your first "conversation" with the computer, this is called a script.
So we will explore basic Python concepts and write some simple scripts in Python.
"Hello World!"
Let's head over to your Colab notebook. We'll go through it step-by-step, so don't worry if you've
never done this before.
Step-by-Step Guide
1. Go to Google Colab: Open your web browser and navigate to Google Colab.
1. Create Notebook:
● Click on "File" > "New notebook".
9
3. Write Your Script
1. Run Cell:
● Click on the play button next to the code cell or press Shift + Enter.
2. You should see “Hello, World!” printed below the code cell.
You just told the computer to say "Hello, world!" That's Python's way of saying hi back!
Congratulations! 🎉
You've successfully written and executed your first Python script using both Google Colab!
This platform provides convenient ways for you to write, run, and share your Python code without
needing any local setup.
So now you know how to get your computer to talk using a simple script, now lets cover some more
Python Basics so you can better understand the language.
10
The Building Blocks: Variables (and the Data They Hold)
Storage Location: In Python, we use variables to store information, kind of like containers with labels.
Just like a box that can store different items, a variable can store different types of data such as
numbers, text, or other values.
When you create a variable in Python, you are essentially giving a name to a piece of memory where you
can store information that can be used later in your code.
Think about all the different kinds of information you encounter every day: your name, your age, the price
of your favorite snack.
Python needs ways to store and work with different types of data too!
We still use variables as containers, but those containers need to hold the right kind of stuff!
For example:
○ name = "Alice"
○ firstname_lastname = “Beenie Man”
○ message = 'Hello, World!'
○ message = "Welcome to the Generative AI Summer Camp!"
11
B. Numbers:
● Integers: Whole numbers (no decimals), like your age:
For example:
■ age = 16
■ count = 100
For example:
■ pi = 3.14
■ temperature = 98.6
For example:
■ is_student = True
■ is_hungry = False
N.B.
These are case-sensitive keywords, so true or TRUE would not be recognized as boolean values.
Only True
The built-in boolean values True and False are essential for several reasons:
● Consistency: It standardizes how logic and conditions are evaluated across Python programs.
● Efficiency: Boolean values are optimized for logical operations, making comparisons and
decisions faster.
● Integration with Libraries: Many Python libraries expect boolean values as input or return them
as results.
12
D. Lists: Ordered collections of items (which can be different data types!). We use square
brackets:
■ Lists: A list is a collection of items enclosed in square brackets [ ].
For example:
■ fruits = ['apple', 'banana', 'orange']
■ favorite_foods = ["roti", "pelau", "callaloo soup"]
■ numbers = [1, 2, 3, 4, 5]
In Python, the single equals sign (=) is the assignment operator. It's used to assign a value to a
variable.
To create a variable in Python, you simply need to assign a value to a name using the assignment
operator =.
Here's an example:
In this line of code, the string "Your Name" is the value, and name is the variable (storage container).
The equal sign takes the value on the right and puts it into the container (variable) on the left.
age = 15
Key Points:
● Direction: The assignment happens from right to left. Think of it as an arrow pointing from the
value to the variable.
● Not Comparison: The single = is NOT used for checking if things are equal. For that, Python
uses a double equals sign (==).
Awesome! Let's do a quick recap of the Python concepts we've covered so far:
13
Comments: Notes for Yourself and Others
So this is like your basic A, B, C, D’s… and 1, 2, 3 , how about trying to explain your code or write some
notes about what your code does, because documentation is important!!!!
So it's not about writing #thisisgreat and #Icancode!!! The purpose of the hash symbol (#) in Python is
not for tagging or creating hashtags like you might see on social media.
Comments are notes or explanations within your code that are ignored (not executed) by the
Python interpreter. This means, anything you type after a hash symbol on a line is ignored by
Python.
It's just for you to read and other programmers who might read your code!
● Clarity: Comments make your code easier to understand, both for yourself when you revisit it
later and for others who might work on the same code.
● Explanation: They can describe the purpose of a code block, clarify the logic behind a particular
section, or explain why certain choices were made.
● Debugging: Comments can be used to temporarily disable lines of code for testing or
troubleshooting purposes.
Types of Comments
1. Single-Line Comments
2. Multiline Comments
1. Single-Line Comments:
○ Start the line with a hash symbol (#).
○ Anything following the # on that line is considered a comment.
14
2. Multiline Comments:
● Enclose the comment text within triple quotes (""" or ''').
● This is often used for longer explanations or descriptions at the beginning of a file or before a
function.
""" This is a multiline comment that can span multiple lines """
print("Hello, world!")
In this example, the comments clearly explain the purpose of each line or block of code, making it much
easier to understand.
Important Note: While the hash symbol in Python is not used for hashtags like on social media, it's still
a powerful tool for making your code more readable, maintainable, and collaborative!
● You already know the plus sign (+) from math, where it's used for addition. In Python, when we
use it with text (strings), it acts like glue, sticking pieces of text together.
● Let's try it out:
15
Print Statement: print(full_message)
● If we want to include numbers in our messages, we need to convert them into text (strings) first.
We do this with a special function called str().
Combining text (what we just did) is going to be super useful when we start building chatbots. We'll use
it to put together different pieces of text to make the chatbot's responses feel more personal for each
customer.
For example:
bot_name = "IslandHelperBot"
user_name = "Alex"
bot_response = "Hi, " + user_name + "! I'm " + bot_name + ", how can I help you today?"
print(bot_response)
Code Output
16
○ This is your way to make the computer display messages on the screen.
○ It's like your personal megaphone to share information or results.
○ Example: print("Hello, future AI experts!")
17
Putting it Together: A Mini-Challenge
Can you write a short Python script in Colab that does the following:
Lab Task
1. Create a variable called island and store the name of your respective Eastern Caribbean island
in it.
2. Create another variable called population and store a rough estimate of the population. (Don't
worry about being exact!)
3. Print a message that says: "[Your Island] has a population of approximately [Population] people."
Be sure to add comments so your fellow peers can understand what you have written.
_____________________________________________________________________________________________
Answer
Output: St. Kitts and Nevis has a population of approximately 55000 people.
Explanation:
1. Variables:
○ island: We create a variable to store the island name. Since it's text, we use double
quotes (it's a string data type).
○ population: This stores the estimated number of people. We use an integer here since
it's a whole number.
2. String Concatenation:
18
3. print() Function:
Output:
When you run this code in Colab, it will print the following message:
_____________________________________________________________________________________________
Key Points for Students:
Alright, Python experts! We've learned how to tell the computer to do things, like print messages and
store information. But what if we want the computer to ask us questions and get information back from
us?
This is where the input() function comes in handy. It's like having a built-in microphone for your
Python program, allowing it to "listen" to what you type on your keyboard. Here's how it works:
Breaking it Down:
1. input("What's your name? "): The computer displays the message "What's your
name?" and waits for you to type something.
2. You Type: You type your name (e.g., "Maria") and press Enter.
3. Input is Stored: Python takes the text you entered ("Maria") and stores it in the variable
name.
4. print(): The program then uses the name variable in a personalized greeting.
19
● No matter what you type into the input() function, it always treats it as a string (text).
● So, even if you type in the number 15, Python sees it as "15".
● Let's say you want to store your age in a variable, which is a number. Here's how to adjust the
code:
We use the int() function (short for "integer") to turn the user's input (a string) into a number that we
can do calculations with later.
Exercise 2:
1. Scenario: Imagine you are interacting with a customer. You want to create a personalized
message based on their name and age.
2. Task:
■ Create a Python script that asks the customer for their name and age.
■ Respond with a customized message based on their input.
3. Steps:
■ Open a text editor and create a new Python script file (e.g.,
personalized_message.py).
■ Define variables to store the customer's name and age.
■ Use input() function to prompt the user to enter their name and age.
■ Use string concatenation to create a personalized message.
■ Print the personalized message to the console.
4. Execution:
■ Save the script file.
20
■ Open a terminal or command prompt.
■ Navigate to the directory where the script is saved.
■ Run the script using the command python personalized_message.py.
■ Enter your name and age as prompted.
■ Observe the personalized message displayed on the console.
Example Script:
# Create a personalized message based on user input
message = "Hello, " + name + "! You are " + age + " years old. Thank you for visiting us today."
print(message)
Congratulations! You have successfully created your third Python script that generates a personalized
message based on user input. These exercises have introduced you to variables, data types, and string
concatenation in Python. Keep practicing and exploring more possibilities with Python programming.
Stay tuned as we dive deeper into more exciting coding exercises in our next session!
Today, you learned how to make Python programs more interactive by getting information directly from
the user. This is like having a two-way conversation with the computer!
● input(): Think of this as the microphone for your Python program. It lets the computer "listen" to
what you type on your keyboard.
● How it works:
1. You see a message (a question, a prompt, etc.).
2. You type your response and press Enter.
3. Python takes what you typed and stores it in a variable for later use.
● Everything you type into the input() function is automatically treated as a string (text).
● Even if you type in a number, Python thinks it's a word – that's the "String Trap"!
21
int(): The Costume Change for Numbers
● If we want to use numbers for calculations or other operations, we need to change their costume
from a string into an integer.
● That's where the int() function comes in. It's like changing a word into a number.
Key Takeaways:
Pro Tip: In the future, you'll encounter another function called float(). This is used when the user might
input a decimal number (e.g., their height or weight).
2. GitHub Codespaces: Your online coding workspace - like having a powerful computer in the
cloud!
3. Print Statement: How you make Python show messages Example: print("Hello, World!")
5. Data Types:
6. Comments: Notes in your code (Python ignores these) Example: # This is a comment
7. Input: Getting information from the user Example: favorite_color = input("What's your favorite
color? ")
8. If Statements: Making decisions in your code Example: if age >= 13: print("You can use
Instagram!") else: print("Sorry, you're too young for Instagram.")
22
You Are Now Chatbot-Ready!
With this knowledge, you have the skills to start building basic chatbots that can ask questions and
respond based on the user's input. Get ready to put this into practice as we move on to more advanced
Python concepts and AI techniques!
23
Assignment
2. Set Up Codespace
2. Open Codespace:
● Click on the green "Code" button.
● Select "Open with Codespaces".
● Click on "New codespace".
1. Open Terminal:
● In the bottom panel, click on the terminal tab or open a new terminal by clicking on the
"+" icon and selecting "New Terminal".
24
Celebrate Your Success! 🎉 You have successfully set up your Python environment and written
your first script!
● To add a new code cell: Click the "+ Code" button in the top menu
● To add a text cell: Click the "+ Markdown" button in the top menu
● To change cell type: Use the dropdown in the top menu to switch between Code and
Markdown
Now you are ready to dive deeper into learning Python programming by exploring its syntax,
data types, control structures, functions, classes, and much more.
Happy Coding! 🐍🚀
25
Google Colab vs GitHub Codespaces: Syntax and Variables
What this means: In Codespaces, you have more options for how to write your code.
2. Running Code:
○ Colab: Click play buttons (Run cells using the play button) or use a keyboard
shortcut (Shift+Enter)
○ Codespaces:
■ For .ipynb: Same as Colab
■ For .py: Run in terminal with python filename.py
What this means: In Codespaces, you have more options for how to write your code.
What this means: Colab is easier to start with, but Codespaces teaches you how to set
up your own coding environment.
What this means: Codespaces is more like what professional coders use.
26
with open('myfile.txt', 'r') as file:
content = file.read()
What this means: Codespaces keeps everything in one place, which can be easier to
manage.
What this means: In Codespaces, your work is saved more reliably between coding
sessions, but it's not permanent storage. This means that Codespaces is like a game
that remembers where you left off, but might reset if you update the game.
7. Code Suggestions:
○ Colab: Gives basic suggestions
○ Codespaces: Gives more detailed suggestions
What this means: Codespaces can help you write code faster once you get used to it.
Remember, the basic Python code (like variables, print statements, and input functions) works
the same in both. The main differences are in how you set up and manage your coding
environment.
27
28