Gather Workshops Logo

Python Taster

What is this thing anyway?

tiny.cc/pythontaster

What is Python?

  • It is a programming language
  • Used to build both apps and websites
  • Many diverse use cases

Gather Workshops Logo

Where Python Fits

Command-Line Programs

Data-crunching Power

Hardware

Takes the magic of Python beyond the screen

Many large companies use Python for

Web Development

Python teaches us many

Common Concepts

Python - It's for everybody

A Valuable Skill In Any Career

it's not just for full-time programmers!

Workshop Mission:

Fun with Python

Gather Workshops Logo

Komodo Edit

Get up and coding quickly

Komodo Edit Logo

Komodo Edit

Free, cross-platform code editor

Download Here

Python Files

are just plain-text files

Running Programs

can be done directly from Komodo

Gather Workshops Logo

Python Basics

Let's get coding!

Workshop Goals

  • Output
  • Variables
  • Inputs
  • Loops
  • Conditionals

Output

print("Hello CS4HS!")

We make text show up on the screen by using the print function.

Output Challenge

See if you can make your program create the following output

Hello CS4HS!
Today is Thursday.
I'm learning Python!

Variables

message = "Hello CS4HS"
print(message)

Variables allow us to store changeable information
under a consistent name.

Variables Challenge

Using the following variables

greeting = "Hello!"
introduction = "My name is"
name = "Alice"
luckyNumberText = "My lucky number is"
luckyNumber = 13

See if you can write a program which produces

Hello!
My name is Alice
My lucky number is 13

Input

message = input("What is your message?")
print(message)

Receiving user input makes our program interactive.

We can store input in a variable.

Input Challenge

Using the following starter code

greeting = "Hello!"
introduction = "My name is"
luckyNumberText = "My lucky number is"

# missing code goes here

print(greeting)
print(introduction, name)
print(luckyNumberText, luckyNumber)

See if you can modify the code to:

  • ask you for your name and lucky number
  • use those values in the message

Conditionals

password = input("Please enter your password: ")

if password == "bunnies":
    print("Success!")
else:
    print("Access Denied")

If-else statements allow us to choose which piece of code to run, based on a value.

This is "Boolean logic" where a result is always either true or false.

Conditionals Challenge

Using the following starter code:

chosenNumber = input("Please enter a number between 1 and 10: ")
chosenNumber = int(chosenNumber)

# if less than one
print("Too low!")

# else if more than 10
print("Too high!")

# else
print("Perfect! Thank you.")

Change the code to output the correct response,
based on the user's input.

Make sure to check the boundary values (0, 1, 10, and 11)

Loops

for number in range(1, 6):
    print("Number is", number)

Using a loop allows us to run a piece of code many times.

We can also keep track of how many times we have looped.

Loops Challenge

Use loops to create a star sail like this:

*
**
***
****
*****
******
*******

Hint: You can put a loop inside another loop!

Bonus: Can you make your loop draw a tree instead of a sail?

Gather Workshops Logo

Calculator Program

Bringing it all together

Project Brief

This is a calculator, but maybe not the kind of calculator you were expecting.

Your challenge is to create a Love Calculator

Your program must do the following:

  • Ask for two names
  • Calculate their suitability
  • Output their result as a percentage

Example Output

What is your name? Mickey Mouse
What is the name of your crush? Minnie Mouse
You have a 75% match!

Your final running program might look something like this.

"Mickey Mouse" and "Minnie Mouse" are input provided by the user.

Collecting Input

You will need to collect at least two pieces of information:

  • The user's name
  • Their crush's name

Make sure to store those values in variables.

Example of Collecting Input

If you're having trouble collecting input, try this:

# ask for input
firstPerson = input("What is your name? ")
secondPerson = input("What is your crush's name? ")

Calculation Algorithm

You will need to process the two names in some way. You could:

  • Compare the number of letters
  • Count how many letters they have in common
  • Convert the names to numbers some other way

Make sure that the resulting number is a percentage.

Example of a Calculation Algorithm

If you're having trouble writing an algorithm, try this:

# count total number of letters
totalLetters = len(firstPerson) + len(secondPerson)

# count number of letters that are the same
matches = 0;

for letter in firstPerson:
    if letter in secondPerson:
        matches += 1

for letter in secondPerson: 
    if letter in firstPerson:
        matches += 1

Printing Output

You will need to tell the user their result:

You are a 55% match!

Remember the result should be a "percentage match".

Printing Output

If you're having trouble printing the output, try this:

# output
percentage = int(matches / totalLetters * 100);        
print(str(percentage) + "%" )

Loading...