Commands from "Python Hello"

In this section, I will put the commands found in "Python Hello" in terms that are easy for someone who has been coding for, say, one week to understand.

The "print" command can be used to output static text.

print("This is the static text produced.")
print("It can be found within the quotation marks in the parentheses.")
This is the static text produced.
It can be found within the quotation marks in the parentheses.

You can also add variables to the printed static text with the "+" symbol.

x = 5 + 3
print("The sum of 5 and 3 is " + str(x) + ".")
The sum of 5 and 3 is 8.

This is just scratching the surface.

"if" and "else"

The "if" and "else" commands can make certain things happen under certain conditions.

Instead of giving a copius amount of examples, just know that anything you can make happen unconditionally with Python can be restricted to occur under only certain situations with "if." How to handle the remaining cases is determined by "else," which doesn't necessarily have to be present.

x = 4 * 3

# Remember to use a colon at the end of the "if" statement to activiate the condition.
if x == 12:
    print("Four times three is 12.")
else:
    print("Something went wrong.")
# We use "else" above for when x is equal to something different.
Four times three is 12.

But why does it matter that we can do that? How can 'x' be equal to anything other than 12 with the set factors of 3 and 4?

Input

Input allows the person interacting with the code to type in a custom response to a prompt.

from tkinter import Y


x = input("What is your favorite number?")
# Fastpages does not allow one to input a response in the actual page.
# For the sake of demonstration, 'x' has been set to equal 5.

# The syntax looks like this if you want a separately printed question and response.
print("What is your other favorite number?")
y = input()
# The variable 'y' has been set to equal 3.

# Now the "if" command will vary based on what we said.
z = int(x) * int(y)
if z == 12:
    print("The product of your favorite numbers is 12.")
else:
    print("The product of your favorite numbers is not 12.")
What is your other favorite number?
The product of your favorite numbers is not 12.

Extra Learning

Here are some extra commands and functions I found out about on my own. They can be very helpful in tandem with the rest of these functions.

"elif"

This adds an additional condition to an "if" command. It goes between "if" and "else" vertically.

x = input("What is your favorite number?")
y = input("What is your second favorite number?")
z = int(x) * int(y)
if z == 12:
    print("The product of your favorite numbers is 12.")
elif z == 15:
    print("The product of your favorite numbers is 15.")
else:
    print("The product of your favorite numbers is not 12 or 15.")
The product of your favorite numbers is 15.

Algebra

All the algebra stuff above was not taught in the lesson. I figured it out on my own.

  • "+" = Addition
  • "-" = Subtraction
  • "*" = Multiplication
  • "/" = Division
  • ">" or "<" = Less Than or Greater Than (often for 'if' conditions)
  • "=" = Equating a value/string