Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

ECCU Generation AI & Python - Code Your First Python Script

Blah blah blah

Uploaded by

sycharaubert
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

ECCU Generation AI & Python - Code Your First Python Script

Blah blah blah

Uploaded by

sycharaubert
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Code Your First Python Script & Lab 1:

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.

Let's Chat with the Computer: Two-Way Communication 19


Deep Dive: Getting the Right Data Type 20
Time to Practice! Let's get into the Lab. 20
Exercise 2: 20
Talking Back to Python: Getting Information from Your Users 21
Key Takeaways: 22
Python Basics Glossary and Cheat Sheet 22
You Are Now Chatbot-Ready! 23
Assignment 24
GitHub Codespaces Cheat Sheet : 25
Google Colab vs GitHub Codespaces: Syntax and Variables 26

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.

Why Learn Python?


■ Versatile: Python can be used for a wide range of applications including web
development, data analysis, automation, machine learning, and more.
■ Easy to Read: The syntax of Python is designed to be easily readable which makes it
accessible for beginners.
■ Large Community: There is a vast community of developers who contribute libraries
and resources that make coding in Python even easier.
■ Career Opportunities: Learning Python opens up opportunities in high-demand
fields like data science and software development.

3
What You Will Learn
By the end of this workshop, you will learn:

1. How to set up your development environment


2. Basic syntax rules in Python
3. Variables and data types

Python is an excellent language for beginners due to its simplicity yet powerful capabilities.
Let's dive into our first script!

Introduction to Python: Setting Up Python Environment


Overview
In this tutorial, we will explore the basics of the Python programming language and set up the
environment needed to write and execute Python scripts. Python is a versatile and
beginner-friendly language widely used in various fields such as web development, data science,
artificial intelligence, and more.

Steps to Setting Up Your Python Environment on the Cloud


Hey everyone! Welcome to the world of coding and AI!

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.

Part 1: Google Colab - Your Interactive Playground

Your Coding Notebook: Google Colab

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!

○ Perfect for learning because you see results right away.

○ Great for collaboration, as you can share your "notebooks" with others.

● Why we're using it first:


○ Easy to get started, no complicated setup.

○ Has all the tools we need for the first part of the camp already built-in.

Setting Up Google Colab

Here's how you can set up Google Colab:

● 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

Your Future Workspace: GitHub Codespaces

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.

Think of it like this:

● 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!

Using GitHub Codespaces


● What it is:
○ A more powerful environment where you build full-fledged projects. GitHub
Codespaces is a cloud-based development environment that allows you to
write, run, and debug your code directly in your browser.
○ Like having your own computer in the cloud, but accessible from anywhere.
○ Uses the popular code editor, Visual Studio Code, which many pro developers
use.

Why we'll switch later in the camp:

● 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.

Setting Up GitHub Codespaces

Here's how you can set up GitHub Codespaces :

1. Create a GitHub Account: If you don't have one already, sign up for a free account on
GitHub.

2. Enable Codespaces: Go to the "Codespaces" tab in your GitHub account settings


and enable Codespaces.

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!

Getting Started with Python


Both Colab and Github Codespaces are referred to as Integrated Development Environment
(IDE). An IDE provides tools for writing, testing, and debugging code efficiently. What we have
shown you are some popular cloud/browser based IDE’s but there are some other popular and
free IDEs for Python which include but not limited to the following;

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.

Tutorial: Write Your First Script


Today's Warm-Up:

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.

Your First Words & the "Print" Statement

Let's start with a simple greeting.

"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.

Writing "Hello World!" in Google Colab

Step-by-Step Guide

1. Open Google Colab

1. Go to Google Colab: Open your web browser and navigate to Google Colab.

2. Create a New Notebook

1. Create Notebook:
● Click on "File" > "New notebook".

9
3. Write Your Script

1. Add Code Cell:


● By default, a new code cell is already present when you create a new notebook.

2. Write Your Code:


First we use the Print Statement,

The print() function is used to display output on the screen.

So you write print("Hello, World!")

4. Run 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.

See that message below?

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.

In Python, a variable can be thought of as a container that holds a value.

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.

Fig 1. Variables as Containers

What Can We Store? Python Data Types

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!

The main data types you'll use in Python are:

A. Strings: Text, like your name or a message. We put them in quotes.


1. String: A string is a sequence of characters enclosed in single or double quotes.

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:

● Floats: Numbers with decimal points, like the price of something:

Let's do a deep dive on these two data structures;

2. Integer: An integer is a whole number without any decimal points.

For example:

■ age = 16
■ count = 100

3. Float: A float is a number that has decimal points.

For example:

■ pi = 3.14
■ temperature = 98.6

C. Booleans: Represent True or False values, like whether something is on or off:


4. Boolean: A boolean variable can only have two values: True or False.

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

Why Use True/False?

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]

So how do you create a Variable in Python?

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:

Let's create a string variable called name to store your name:

name = "Your Name"

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.

Here is another example:

age = 15

Here, the number 15 is assigned to the variable age.

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!!!!

This is where the hash symbol (#) comes in.

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.

The Hash Symbol in Python: Comments

In Python, the hash symbol is used to create comments.

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!

Why Use Comments?

● 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

There are two types of comments.

1. Single-Line Comments
2. Multiline Comments

How to Write Comments in Python

1. Single-Line Comments:
○ Start the line with a hash symbol (#).
○ Anything following the # on that line is considered a comment.

# This is a single-line 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 """

Example of Using Comments in Your Tutorial

# This line of code prints a greeting message

print("Hello, world!")

# Create a variable called 'name' and store your name in it (string)

name = "Your Name"

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!

Making Words Play Nice: String Concatenation in Python


So let's now try and string two words together to make sense, just like in English. So far, we've seen
how to store different types of data in variables and display simple messages using the print()
function. But what if we want to combine different pieces of information together, like adding someone's
name to a greeting? In Python, we do this by combining text, or strings as we call them. The word for
this is “String Concatenation”,

Concatenate means to combine.

The "+" Operator: Text Glue

● 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:

String Variable: greeting = "Good morning, "

String Variable: customer_name = "Maya"

String Concatenation: full_message = greeting + customer_name + "!"

15
Print Statement: print(full_message)

Output: Good morning, Maya!

Numbers Need to Dress Up


● Remember, Python is very specific about types of data. We can't directly combine a word
(string) and a number using the +.

● 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().

Let's try it out:

Variable and Integer : order_number = 12345

String Variable: "Your order number is: "

String Concatenation: message = "Your order number is: " + str(order_number)

Print Statement: print(message)

Output: Your order number is: 12345

Making Chatbots Friendly

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

Hi, Alex! I'm IslandHelperBot, how can I help you today?

Your Python Toolkit (So Far)

1. The Messenger: The print() Statement

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!")

2. Storage Containers: Variables


○ Think of them like labeled boxes. You give them a name, and they hold the data you put
inside.
○ They're essential for working with information in your programs.
○ Example: customer_name = "Sophia"

3. What's Inside? Data Types


○ Just like you can put different things in different boxes, Python has types for different
kinds of data:
■ Strings: Text (words, phrases, sentences) in quotes
■ Integers: Whole numbers (no decimals)
■ Floats: Numbers with decimals
■ Booleans: Represent True or False (like a yes/no answer)
■ Lists: Ordered collections of items (can hold any data type!)
○ Choosing the right data type is important for how Python handles the information.

4. The Arrow of Assignment: The = Operator


○ This isn't about checking equality, it's about putting things into those variable
containers.
○ It works like this: variable_name = value
○ Think of it as an arrow pointing from the value (on the right) to the variable (on the left).

5. The Note-Taker: Comments with the # Symbol


○ Use the # symbol to add notes and explanations to your code.
○ Python ignores comments, but they help you understand what the code is doing.
○ Example: # This line calculates the total price

6. The Text Combiner: The "+" Operator for String Concatenation


○ When used with strings (text), the + sign sticks them together.
○ Example: "Hello" + "World!" becomes "HelloWorld!"

7. The Costume Change: str() Function


○ Converts numbers into strings so you can combine them with text.
○ Example: print("You have " + str(5) + " apples.")

Before we add more concepts, let's put these to work.

17
Putting it Together: A Mini-Challenge

“Your ________(Island) has a population of approximately ____ people.”

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

# Create a variable to store the island's name (String)


island = "St. Kitts and Nevis"

# Create a variable to store the approximate population (Integer)


population = 55000 # This is just a rough estimate!

# Print a message using the variables and string concatenation


print(island + " has a population of approximately " + str(population) + " people.")

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:

○ The + operator combines text together.


○ Note that we use str(population) to convert the number into a string so it can be
combined with the other text.

18
3. print() Function:

○ This displays the final combined message on the Colab output.

Output:

When you run this code in Colab, it will print the following message:

St. Kitts and Nevis has a population of approximately 55.000 people.

_____________________________________________________________________________________________
Key Points for Students:

● The island variable is a string, so it's enclosed in quotes.


● The population variable is an integer, so it doesn't have quotes.
● We convert the population variable to a string using the str() function so we can combine it
with the other strings using the + operator.
● The print() function displays the message on the console.

One last lesson before we practice.

Let's Chat with the Computer: Two-Way Communication

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?

Introducing the input() Function

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:

name = input("What's your name? ")


print("Nice to meet you, " + name + "!")

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.

Important Note: The String Trap!

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".

Deep Dive: Getting the Right Data Type

● Let's say you want to store your age in a variable, which is a number. Here's how to adjust the
code:

age_as_string = input("What's your age? ")

age = int(age_as_string) # Convert the string into a number

print("Wow, you're", age, "years old! That's awesome!")

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.

Time to Practice! Let's get into the Lab.

Exercise 1: Hands-On Challenge: Your Own Interactive Script

Now it's your turn! In Colab, create a Python script that:

1. Asks the user for their favorite Caribbean dish.


2. Asks the user how many times they've eaten it this week. (Remember to convert to an integer!)
3. Prints a personalized message like, "You love [dish] and have eaten it [number] times this week.
That's impressive!"

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

# Ask for user input

name = input("Enter your name: ")

age = input("Enter your age: ")

# Create a personalized message

message = "Hello, " + name + "! You are " + age + " years old. Thank you for visiting us today."

# Print the personalized message

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!

Talking Back to Python: Getting Information from Your Users

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!

The Input Function: Your Program's Ears

● 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.

Important Note: The String Trap!

● 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:

1. input() allows your program to get information from the user.


2. All input is treated as a string, even numbers.
3. Use int() to convert a string representing a whole number into an integer.

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).

Python Basics Glossary and Cheat Sheet


1. Python: A friendly programming language that helps you talk to computers.

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!")

4. Variables: Containers for storing information Example: my_age = 15

5. Data Types:

○ Strings: Text (words, sentences) in quotes Example: name = "Alex"

○ Integers: Whole numbers Example: age = 16

○ Floats: Numbers with decimal points Example: height = 5.9

○ Booleans: True or False values Example: is_student = True

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.")

9. Functions: Reusable blocks of code Example: def greet(name): print(f"Hello, {name}!")


greet("Sarah") # Prints: Hello, Sarah!

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

Writing "Hello World!" in GitHub Codespaces


Step-by-Step Guide
1. Create a New Repository

1. Log in to GitHub: Go to GitHub and log in.


2. Create a New Repository:
● Click on the "+" icon at the top right corner and select "New repository".
● Name your repository (e.g., hello-world-python).
● Choose whether you want it to be public or private.
● Click on "Create repository".

2. Set Up Codespace

1. Navigate to Your Repository: Go to your newly created repository.

2. Open Codespace:
● Click on the green "Code" button.
● Select "Open with Codespaces".
● Click on "New codespace".

3. Write Your Script

1. Create a New File:


● In the left sidebar, click on the "+" icon next to your repository name.
● Name your file hello_world.py

2. Write Your Code:


print("Hello, World!")

3. Save your File:


Save your file with a .py extension (e.g., hello_world.py).

4. Run Your Script

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".

2. Execute Your Script:


Run your script by clicking on the "Run" button or using keyboard shortcuts. - python
hello_world.py

3. You should see Hello, World! printed in the terminal.

24
Celebrate Your Success! 🎉 You have successfully set up your Python environment and written
your first script!

GitHub Codespaces Cheat Sheet :

1. Start a new Codespace:


○ Go to your GitHub repository
○ Click the green "Code" button
○ Choose "Open with Codespaces"
2. Create a new Jupyter Notebook:
○ Click the "New File" icon in the left sidebar
○ Name it with a .ipynb extension (e.g., my_notebook.ipynb)
3. Run your Python code in a Jupyter Notebook:
○ Click the "Run" button next to each cell, or use Shift+Enter
○ To run the entire notebook, go to the "Run" menu and select "Run All Cells"
4. Save your work:
○ Jupyter Notebooks auto-save, but you can also click "File" > "Save" or use
Ctrl+S (Cmd+S on Mac)
5. Close your Codespace:
○ Click on your account icon in the bottom left
○ Choose "Close Remote Connection"

Additional Jupyter Notebook Tips:

● 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

1. Where do you Write your Code (File Extensions):


○ Colab: Uses .ipynb (Jupyter Notebook) files (like digital science journals)
○ Codespaces: Can use both
.py (These are standard Python script files) and
.ipynb (these are Jupyter Notebook files ) (Can use both notebook files and
regular Python files- which allow for interactive coding and documentation.)

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.

3. Importing Libraries (Installing and Using Extra Tools):


○ Colab: Many libraries pre-installed
import numpy as np # Often works without installation
○ Codespaces: May need to install libraries
!pip install numpy # Install before importing
import numpy as np

What this means: Colab is easier to start with, but Codespaces teaches you how to set
up your own coding environment.

4. Talking to Your Computer (System Commands):


○ Colab: Use a special symbol (!) ! before system commands
!ls # List files
Codespaces: Use terminal for system commands, or ! in Jupyter notebooks.

What this means: Codespaces is more like what professional coders use.

5. Accessing, Working and Storing Files:


○ Colab: Often uses Google Drive to save files
from google.colab import drive
drive.mount('/content/drive')
○ Codespaces: Direct access to repository files i.e Your files are right there in your
coding space and saves directly in your GitHub project.

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.

6. Remembering your Variables (Persistent Storage):


○ Colab: Variables and data are usually lost when you close the session or if it
times out. (Colab is like a game that doesn't always save your progress when
you quit.)
○ Codespaces: Data persists within a session and between sessions, but resets if
you rebuild the environment.

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.

What This Means for You:

● Both are great for learning Python!


● Colab is super easy to start with and is more focused on quick, shareable data science
work.
● Codespaces is more like what professional coders use and offers a more complete
development environment,

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

You might also like