Get Your Pyth-On

Start Here

1. What is This Course?

2. What Resources Do I Need?

Follow along and learn while typing! We will be using a free online compiler called Programiz for all of our programs. Link: https://www.programiz.com/python-programming/online-compiler/

Handy Dandy Programiz Guide:

3. How Can I Make the Most of It?

4. Closing Notes

Variables and Printing

Choosing a Variable Name

Legal Variable Name Examples:

Illegal Variable Name Examples:

Variable Declaration

Python has no command for declaring a variable so datatype are automatically assigned

type() function

To check the data type assigned to your variable, use the type() function

Casting

To specify the data type of a variable, use casting

Printing

print() function allows you to output variables
Components: print(object(s), sep=separator, end=end, file=file, flush=flush)

Utilizing print systems:
Automatically assumed to use “ “ as separator

Use a different separator(“—“)

Can separate variables using “+” as well

Carefully add separators when using “+”

Arrays

An array is a special variable that can hold more than one value at a time.

Using an Array

When you have a list of items instead of making multiple variables like this:
  • name1 = “Fred”
  • name1 = “Fred”
  • name2 = “George”
  • name3 = “Sally”
  • Solution is to use an array
  • names = [“Fred”, “George”, “Sally”]
  • Accessing Array Elements

    To access element in array:
  • names[0] -> this accesses element “Fred”
  • To change element in array:
  • names[0] = “Bob” -> this allows the first element to now be “Bob” instead of “Fred”
  • Conditions

    What are conditions? and why?

    Let's Check Common Conditional Code in Python!

    Types of Conditions

    Check out the following example below:

    Those conditions can help you get true or false base on different situation, so that the code can react differently to that true or false and has a different behavior. However, just having these conditions is not enough, let's learn another important python statement in next section if/else statements

    If, else and elif?

    Check out this example!

    Loops

    What Are Loops?

  • Loops are a sequence of instructions that are continually repeated until a certain condition is reached. We can use loops to repeat an instruction multiple times!
  • Types of Loops

  • For Loop
  • While Loop
  • Let's Make a For Loop!

    Let's Make a While Loop!

    Useful feature of a for loop in Python!

    A useful feature of for loops in python is that you are able to iterate through a list/array without having to index the list.

    Instead of doing this:

    we can do this:

    A for loop allows you to be able to access every item in a list/array even if you dont know how many items are in the list/array. This is very useful for things such as reading from files.

    Loop Dangers!

    Infinite Loop

    An Infinite Loop is a continuous repetitive conditional loop that gets executed forever. Now an infinite loop will not necessarily run forever, it will eventually end when the computer runs out of memory. However, this is something you should avoid becuase your program will hang.

    You can end up in an infinite loop if you don't have a condition that is met in your loop.

    For Example:

    In the above image you can see the loop will never exit because x will never be less than -2

    Functions

    What Are Functions?

  • Functions are structures that allow us to complete a series of actions in our program. We use them to perform tasks!
  • Example (read the comments!):
  • Types of Functions

    1. Built-in functions: functions that Python provides for us like print() and input()!
    2. User defined functions: functions that programmers create to accomplish specific tasks in their program.

    Function Structure:

    Let's analyze our "do_something()" function from before:

    do_something function labeled
    1. Every function in Python starts with def, followed by the function name, and ():
    2. Every function also contains a body.
    3. Calling a function causes the function body to execute. To call a function, simply write the function name followed by parentheses. If the function takes parameters, put them inside the parentheses.

    Let's take a look at another example (with parameters!):

    This add function takes in two parameters (num1, and num2).

  • num1 and num2 reserve spots for numbers or variables that contain numbers!
  • This function returns the sum of num1 and num2.
  • Notice that unlike do_something(), add(num1, num2) prints nothing to the console!

  • This is because the do_something() function prints within the function.
  • In order to see if our add function is working properly, we can call the function within a print statement!
  • User Input!:

    When programs need the user to provide information, it takes in user input. Here, we will learn how to use the built-in Python function input() to store what the user provides for us.

    Below is the definition of a "greet" function that asks the user for their name and says "Hello user name , how are you?":

    Inside of input(), we pass a prompt. This prompt shown to the user before they provide their input. We typically choose prompts that tell the user what they are supposed to do. This is what you should see when your program runs:

    The computer is asking for your name! Do you see the cursor on the console? Write your name and hit enter!

    What really happens here when you hit enter is whatever you typed to the console is then stored in variable, name. Then, our function prints the greeting statement with whatever you entered to the console (see image below). Pretty cool right?

    Challenge Problem: Cats or Dogs

    Define a function named cats_or_dogs that takes no parameters. Ask the user "Which is better? cats or dogs: ". If the user inputs "cats", the console should respond with "Meow!". If the user responds with "dogs", the console should respond with "Bark!" If the user inputs something other that "cats" or "dogs", the console should tell them "You can only answer cats or dogs. Try again!", then the prompt appears again.

    Hints:

  • Use conditionals since we are checking for different inputs. If this happens -> then do this
  • We use == to determine if two values are the same. Strings are values!
  • Solution

    Don't be intimidated by the code. Read it line by line and try to understand what it's doing!

    Summary:

    Where Can I Use These Skills?

    So you've acquired a slew of new skills after this course, where can you use them? It is important to realize that no matter where you work or what the situation is, having a toolbox of skills will always be helpful, even if you don't directly apply the skills to that specific job.

    Analytical Thinking/ Logic

    Breaking the Problem Down

    CS-Related Jobs

    Outside of CS

    Rock, Paper, Scissors!

    Apply the concepts you've learned in modules 1-5 to make a program that plays rock, paper, scissors with the user!

    Think about what pieces you'll need from what you've just learned?

  • Variables to store information like player names?
  • User Input to take in what your response will be?
  • Conditional statements to see who wins?
  • Maybe a loop to play the game over and over?
  • Step 1

  • "import random" into your program. This will allow us to genereate random choices from the computer's side. (We'll see how to use it later)
  • Step 2

  • We need to know what the user chose, and what the computer chose. Do not fear, we will show you how to know what the computer "chose".
  • First, store what the user chose. We learned how to do this with user input. We'll accept the inputs: "rock", "paper", or "scissors".
  • Next, we're going to use our import of "random" to store the computer's choice. Like so:
  • As you can see, we've made an array of possible actions, and we make the computer randomly choose among those actions.

    Step 3

  • The last thing we need to do is compare the user action and computer action and display a tie, win, or lose message.
  • Think of all the possible cases. What structures do we use when there are multiple cases?
  • Cases: