Welcome!

Let's introduce a few folks!

Bonner Springs City Library

Thanks to our sponsors!

Several folks donated time and resources to make this effort possible!


Members of KC Tech Group!
Your City Library Staff!

Why Code?

Broad Applicability

Work in Any Industry!

🚜 🛩 🏭 👩‍🎨

Be in Demand!

Work Anywhere!

Possibly even from home!

Solve Problems!

Work in teams!

Create Value!

Iterate and Improve!

Enjoyment!

Coding can be fun!

It can be rewarding!

Agenda

Getting Started 5:30
Programming Concepts Intro 5:40
Worksheet I 6:00
Functions 6:30
Worksheet II 6:45
Wrap-up 7:15

Technologies

What are we going to be using today?

The Hardware

Raspberry Pi

Raspberry Pi Logo

The Language

Python Logo

The Fun

Let's Get Started

We've got to roll through a few basics first.

Some of what you'll see

Running Python

The Python REPL - Read, Eval, Print, and Loop


						>>>  This is the REPL prompt
						...  This is what the REPL prompt looks like when it
						...    expects something based on the previous line.
					
You can type commands directly and have them processed immediately in Python!

Comments

Describes your code, but doesn't "run"


						# You can comment above a section a code
						money = 500
						for i in range(5):
							money = money * 1.03  # ... and also on the same line
					

Errors

All of my code starts off perfect.

-- No programmer ever.

						>>> pi = 3.14
						>>> a = pi * (r**2)
						Traceback (most recent call last):
						File "<stdin>", line 1, in <module>
						NameError: name 'r' is not defined
					

Error output includes line number, type, and description of error.

Data Types

In coding, we deal with many different types of data.

Text: string

Whole numbers like 1, 2, 3, etc: integer

Numbers like 3.14 or 1.2: float

On or Off: boolean

These are some of the basic built-in data types to get us going!

String

Used for text data like words and sentences.


						>>> message1 = 'I am a sentence'   # you can use single
						>>> message2 = " from two parts."  # ... or double quotes
						>>> message1 + message2            # and glue them together
						 'I am a sentence from two parts.'
					

Integer

Used for round/whole numbers.


						>>> a = 101
						>>> b = 50
						>>> a + b
						151
					

Float

Used for decimal numbers


						>>> a = 5.5
						>>> b = 2.0
						>>> a / b
						2.75
					

Boolean

True or False conditions


						>>> a = 5
						>>> b = 4
						>>> a > b
						True
						>>> b > a
						False
					

Variables

Used to store information in programs.


						# Store some numbers in named variables
						r = 4
						pi = 3.14
						coding_is_fun = True
						# Keep the string called Kevin in a variable my_name
						my_name = 'Kevin'
					

The assignment operator (a single =) is used to store information into a variable.

Operators

Used to perform, you guessed it, operations!


										# Arithmetic Operators
										>>> 4 + 5      # 4 plus 5
										9
										>>> 8 / 4      # 8 divided by 4
										2
									

										# Assignment Operators
										>>> a = 4      # assign a the value of 4
										>>> a += 1     # same as: a = a + 1
										>>> a
										5
									

										# Comparison Operators (returns Boolean result)
										>>> a > 3      # check if a is greater than 3
										True
										>>> a == 5     # check if a is equal to 5
										True
										>>> a != 5     # check if a is not equal to 5
										False
									

Expressions

Compute things and produce a result!


						# Math Operations
						4 + 5                              # 9
						pi * (r ** 2)                      # 50.24
						# Combining strings
						'my name is ' + my_name            # my name is Kevin
					

Variables + Expressions

You can store the result of an expression in a variable


						circumference = pi * (r ** 2)      # circumference is 50.24
					

Control Structures

Allow you to make decisions in your code.

  • Make choices with if statements
  • Repeat code sections with while loops
  • Iterate through things with for loops

if Statements

if elif else


						>>> if circumference > 50:     # check if greater than 50
						...     circle_desc = "Big"    # if so, describe as Big!
						... elif circumference >= 30:  # otherwise, check btwn 30 and 50
						...     circle_desc = "Medium" # if so, describe as Medium!
						... else:                      # otherwise,
						...     circle_desc = "Small"  # describe as Small!

					

Indentation is important in Python!

while Loops


						>>> a = 1
						>>> b = 1
						>>> count = 0
						>>> while a < 5:
						...     count += 1
						...     a = b + 1
						...     b = b + a
						...
						>>> print("a: {}, b: {}, count: {}".format(a, b, count))
						a: 8, b: 15, count: 3
					

for Loops


						>>> # Begin a loop between 0 and 5 and print the
						>>> # value of i at each step
						>>> for i in range(5):            
						...     print(i)
						...
						0
						1
						2
						3
						4
					

Worksheet Time!

Time to apply some of what we've learned!

Functions

Expressions let us hold onto a computed result.

Functions help us hold onto a set of instructions.


						# Declare your function
						def hello():
						    # Then put statements in your function
						    print("Hello there!")
					
You can use the def keyword to "define" a function.

Calling Functions

Once defined, you can run (aka call) a function by its name.


						>>> hello()
						Hello there!
					
The () after the name tells Python to run the function.

Arguments

Not what you have with your siblings. 😆


							def hello_there(name):
							print("Hello " + name)
						
Now we can use name inside our function!

						>>> hello_there('Kevin')
						Hello Kevin
					

Notice that the name we supplied to the function was used in the print function.

Returning information

Functions can also return information to whatever called them.


						# Add two numbers and return the result
						def add(x, y):
						    return x + y
					


						>>> add(1, 4)
						5
					
Very powerful!

Worksheet Time!

Let's clean up our code with Functions!

Thank you!