Python Tutorial
Python Tutorial
Comments
Remember the “#” in the first paragraph? The #
means its a comment. They are ignored by the
computer.
Type “# print(“Hello World”)” then Enter (or
return).
Variables
Variables are things that give a name to a thing. To
create one, type the following:
myVariable = 1.2
>>> print(myVariable)
1.2
Conditions
Maybe in Python you want to ask something like
“Do I have enough memory for my calendar?” or
“Is my answer correct?” You actually can! You just
use if! Example:
if this happens:
then do this
Another example:
Found Hello
== — Equal To
!= — Not Equal To
> — Greater Than
< — Less Than
>= — Greater Than or Equal To
<= — Less Than or Equal To
Try it yourself.
We can use it in a program:
answer = input(“4+5: ”)
if answer == “9”:
print(“Correct Answer!”)
if this happens:
do this
if something else happens (else):
then do this
answer = input(“4+5: ”)
if answer == “9”:
print(“Correct Answer”)
else:
print(“Oops... Wrong Answer”)
answer = input(“4+5: ”)
if answer == “9”:
print(“Correct Answer!”)
elif “9” in answer:
print(“Close, but still wrong.”)
else:
print(“Wrong”)
for counterVariable in
range(howManyTimesRepeat):
print(“Hey”)
Hello
Hello
Hello
Hello
Hello
for i in [1,2,3,4,5]:
print(“Hello”)
while this_condition_is_true:
print(“condition is true”)
i = 9
while i < 13:
import time
time.sleep(1)
i = i + 1
Answer:
def functionName():
print(“Stuff here”)
def greeting():
print(“hey”)
greeting()
hey
def printMyName(yourName):
print(yourName)
printMyName(“Johnny Plum”)
Johnny Plum