Important Python Concepts & Examples
Print Function
print ("Hello, World!")
Strings
These are sequences of letters and numbers, or in other words, chunks of text. They are surrounded by quotes for protection.
"Press the ENTER key to continue."
"4 + 7 is my favorite math problem"
(Example of strings)
Variables
Variables are a way of naming information for later use.
myLuckyNumber = 13 (Example of a variable)
Make sure your define your variable before you try to run it.
print (myFavoriteMovie) (You have not created a variable that defines your movie yet.)
myFavoriteMovie = ("Monty Python's Holy Grail")
print (myFavoriteMovie) (Now this will print, because you have defined the variable myFavoriteMovie.)
Integers
These are whole numbers such as 55, -20, and 3 used in programming.
Floats
These are numbers with decimal points such as 58.95, -22.45, and 16.81.
Booleans
This represents either a true or false statement in programming.
x=input()
If x <= 0: (true/false statement #1)
print ("I have no money.")
If x > 0: (true/false statement #2)
print ("I have money.")
Comparators
== (equals) != (does not equal) > (greater than) >= (greater or equal to) < (less than) <= (less than or equal to)
Order of Operations
- parentheses, exponents, multiplication, division, addition, & subtraction (Please excuse my dear aunt sally.)
Max & Min Functions
Max will give you the maximum amout of any agrument and Min will give you the minimum amount of any argument. See the two examples below.
print (max (150, 50, 300)) (When we run this, it will print/return the number 300.)
print (min (20, 45, 60)) (When we run this, it will print/return the number 20)
Comments
Use the # symbol for creating lines of comment. Lines of comment are never figured into the programming, unless they are placed inside of quotation marks.
# Don't forget to check the equation below. (Successful comment)
myString = "Press the # key to begin." (Because the # symbol is inside quotation marks, it is a string and not a comment.)
Three Types of errors:
1. Syntax Error: This type of programming error happens when the computer can't understand what you are saying. It would be like using the following sentence in English, "Take candy no elephant dog." (This sentence makes no sense.)
- priintt (x (Print is mispelled & there needs to be a parentheses after x).
2. Runtime Error: This type of programming error happens when the computer understands what you are saying, but runs into trouble when following your instructions. It would be like using the following sentence in English, "Please eat the tree." (The sentence itself makes sense, but you would have trouble following these directions.)
- myList - [1,2]
print myList[5] (the punctuation, brackets, and spelling are correct, but the computer can't follow these instructions.
3. Logic Error: This type of programming error happens when everything runs without crashing, but since you designed the program incorrectly, the computer gives you an incorrect output or answer.
studentAge = input()
if studentAge < 18:
print ("You are an adult.")
if studentAge > 18:
print ("You are not an adult.")
(The program makes sense and the computer can follow the instructions, but the output yields an incorrect result.)
Print Function
print ("Hello, World!")
Strings
These are sequences of letters and numbers, or in other words, chunks of text. They are surrounded by quotes for protection.
"Press the ENTER key to continue."
"4 + 7 is my favorite math problem"
(Example of strings)
Variables
Variables are a way of naming information for later use.
myLuckyNumber = 13 (Example of a variable)
Make sure your define your variable before you try to run it.
print (myFavoriteMovie) (You have not created a variable that defines your movie yet.)
myFavoriteMovie = ("Monty Python's Holy Grail")
print (myFavoriteMovie) (Now this will print, because you have defined the variable myFavoriteMovie.)
Integers
These are whole numbers such as 55, -20, and 3 used in programming.
Floats
These are numbers with decimal points such as 58.95, -22.45, and 16.81.
Booleans
This represents either a true or false statement in programming.
x=input()
If x <= 0: (true/false statement #1)
print ("I have no money.")
If x > 0: (true/false statement #2)
print ("I have money.")
Comparators
== (equals) != (does not equal) > (greater than) >= (greater or equal to) < (less than) <= (less than or equal to)
Order of Operations
- parentheses, exponents, multiplication, division, addition, & subtraction (Please excuse my dear aunt sally.)
Max & Min Functions
Max will give you the maximum amout of any agrument and Min will give you the minimum amount of any argument. See the two examples below.
print (max (150, 50, 300)) (When we run this, it will print/return the number 300.)
print (min (20, 45, 60)) (When we run this, it will print/return the number 20)
Comments
Use the # symbol for creating lines of comment. Lines of comment are never figured into the programming, unless they are placed inside of quotation marks.
# Don't forget to check the equation below. (Successful comment)
myString = "Press the # key to begin." (Because the # symbol is inside quotation marks, it is a string and not a comment.)
Three Types of errors:
1. Syntax Error: This type of programming error happens when the computer can't understand what you are saying. It would be like using the following sentence in English, "Take candy no elephant dog." (This sentence makes no sense.)
- priintt (x (Print is mispelled & there needs to be a parentheses after x).
2. Runtime Error: This type of programming error happens when the computer understands what you are saying, but runs into trouble when following your instructions. It would be like using the following sentence in English, "Please eat the tree." (The sentence itself makes sense, but you would have trouble following these directions.)
- myList - [1,2]
print myList[5] (the punctuation, brackets, and spelling are correct, but the computer can't follow these instructions.
3. Logic Error: This type of programming error happens when everything runs without crashing, but since you designed the program incorrectly, the computer gives you an incorrect output or answer.
studentAge = input()
if studentAge < 18:
print ("You are an adult.")
if studentAge > 18:
print ("You are not an adult.")
(The program makes sense and the computer can follow the instructions, but the output yields an incorrect result.)