#
# Python Review
#
#
# Comments
#
# Anything after a # symbol is ignored
# by the Python interpreter
#
# Use comments to make your code more readable
# You can also use them as a design tool;
# they can be used as place holders for detail
# to put in later
#
# Generally, put comments before code blocks,
# such as before loops, ifs, elifs, elses,
# and function definitions. Describe in
# plain English what the block is doing
# rather than specifically what each line does.
#
# Data types and variables
#
# Create a variable with a specific data type by initializing it to a value
# Here is an example of creating an integer variable.
integer_variable = 1
# Here is an example of creating a float variable (number with decimals)
float_variable = 1.0
# Here is an example of creating a string variable (character list)
string1_variable = "Hello World"
string2_variable = 'Hello World' # you can use single quotes, too
# Here are examples of booleans - these variables can only be True or False
# (and case matters = both True and False must have the first letter uppercase)
boolean1_variable = True
boolean2_variable = False
# You MUST create the variable before you use it!
x = 0
x += 1 # Add one to what is in x. Will not work if you don't set x to something first!
#
# Console input and output
#
# Use the input function to get data the user types into the console
# It returns a string variable
user_supplied_value = input ("What is your name?")
# The print function will put data on the console
# It takes a string as an input
print (user_supplied_value)
# You can format strings using the + operator to join strings together
print ("You typed '" + user_supplied_value + "' into the console")
#
# Casting
#
# Casting lets you convert a variable's data type to another data type
#
# An example might be if you want to create a string with a number in it
# You would use the str() function to convert the number to a string
# (See what happens if you DON'T use the str() function in the statement below)
int_var = 5
print ("The value of int_var is " + str(int_var))
# Example of converting a string to an integer using the int() function
# This is helpful if you ask a user to input a number with the input function;
# input will return a string, but we want it to be a number
input_number = input("Enter a number (for real - don't try to trick it!)")
x = int(input_number) + 4 # Take away the int() call to see what happens!!
print (str(x))
#
# Conditionals
#
# Conditionals allow you to run different lines of code based on a
# condition being True or False
# If statement - if the condition (the expression after the IF) is True
# the indented block under the IF will run. Otherwise it will not.
# You must have a colon (:) after the IF statement and all lines
# underneath are indented 4 spaces
# In the example below, the if condition will always be true
if True:
pass #pass doesn't do anything, but if you have an IF you must have at least
# one statement after it; use pass if you don't know what to put yet
# but you still want your code to be able to run
# in this example, the IF will execute if the variable has the indicated value
x = int(input("Enter a number (please make sure it's a number!)"))
if x == 1: # Notice the : and the 2 equal signs
print ("The code here is run")
# You can also have an ELSE, which provides code to run if the condition is False
x = int(input("Enter a number (please make sure it's a number!)"))
if x == 1: # Notice the : and the 2 equal signs
print ("The code here is run when the condition is TRUE")
else: # Notice the :
print ("The code here is run when the condition is FALSE")
# You can use ELIF you have more than just 2 possibilities
x = int(input("Enter a number (please make sure it's a number!)"))
if x == 1: # Notice the : and the 2 equal signs
print ("The code here is run when 1 is entered")
elif x == 2:
print ("The code here is run when 2 is entered")
else: # Notice the :
print ("The code here is run when anything other than 1 or 2 entered")
#
# Functions
#
# Functions allow you to create executable modules that can be "called"
# by your code
#
# Like variables, you have to define functions before you can use them
# You may optionally pass arguments (values) into functions and
# optionally return values
#
# Below is an example of a simple function definition and call
# Use keyword def in front of a function definition
def add_numbers (a, b): # Notice colon (:) after definition statement
# Always put a comment describing what the function does
# This function takes two numbers as inputs and returns their sum
x = a + b
return x
# Call the function (notice that the call is NOT indented, which means
# it is outside the "scope" of the function. You must define the function
# first before calling it
sum = add_numbers (2, 5)
print ("The sum is " + str(sum))
#
# Loops
#
# Loops allow you to repeat code in a circular way
# There are several different kinds of loops
# We've looked at the just a few of them
# The loop with a range loops through a specified number of times
# The range number can be a constant or a variable set with an integer value
# The loop counter variable keeps track of the iteration number, that is,
# it goes up by one each time the loop repeats.
# The tricky thing here is that the counter starts at 0.
#
# In this example the loop will repeat 5 times; the counter values are 0, 1, 2, 3, 4
# each time through the loop
for counter in range(5):
# print out the counter value each time through the loop
print (counter)
-
Run Code
-
-
Stop Running Code
-
Show Chart
-
Show Console
-
Codesters How To (opens in a new tab)