Python Basics

Your First Program

Open IDLE, start a new program, or if you have one opened use that one, and then type: print("Hello, World!") Now hit Fn + F5 and you will see that the console prints: Hello, World! Like this:

Variables!

Now to do something a little harder, I will teach you how to store a variable. To make a variable, you type variablename = value. The value can be a string("Hello"), an integer(1), a float(7.7), or a boolean(booleans are either True or False).
A variable could be: name = 'Daniel'.
Now you can do print(name) with NO quotation marks, and it will print: Daniel.

What if you wanted the variable and other words? Then you would do print("Hello, " + name + '!').

Another variable trick is input. To do input you can do:
name = input("What is your name: ")
print(name)
Then the program will ask you for your name and print it like this:

If Statements

If statements are a simple, but important skill to know in Python. If you know any other programming languages, then this will look familiar. Just on tip for python if statements: Identization MATTERS. Here is an example of a program with an if statement: name = input("Give me your name: ")

print("Hello, " + name + '!')

if name == 'Daniel':

print("That is my name too!!")

On this example, you cannot see identations, because HTML does not indent it, but here is a picture example:

If statements can also be used with number. If you have a variable that is
i = 12
and another a = 13
then you could do
if a > i:
print("i is larger than a ")
This leads us to else and elif statements. If you have a variable that is
i = 12
and another a = 13
then you could do
if a > i:
print("a is larger than i ") then you could also do
else: print("i is larger than a")
If you made i = 14 then the else would run. Here is a video to recap that, because it may have been confusing.

Functions

This is the python tutorial for functions! To define a function, you use the keyword def and then the function name(identation matters) like this:
def hello():
print("Hello")

Now, to run the function we put the function name and parenthesis. Here is how to run the example function hello() Here is a picture:

You can also make functions with variables built in like:
def add(a,b)
return a + b

print(add(1,2))
Now this might be confusing, but what the program is doing is taking what the user wants (in this case 1 and 2) making them a and b, then adding them. We use the print, because if we don't the program will just store a + b instead of printing it. Here is a picture:

There is one more thing that I will teach you about with functions. That is how to get a user input with functions. To do that you will do:
def add(a,b):
return a + b
num = int(input("Give me a number: "))
num2 = int(input("Give me another number: "))
add(num, num2)

This makes the variable num a and the varible num2 b. Then it adds a and b together. Now the reason I put int before the input is because when your user will be putting in numbers, you need to add the int. Here is what the program code is and how runs:

Lists

If you know any other programming langauges, then this should be easy. Making a list is like making a variable you do
aList = [1,2,3,4,5,6,7,8,9,10]
print(aList)

This should print [1,2,3,4,5,6,7,8,9,10]. To acces one number in the list you could do
print(aList[0])
Notice that ZERO prints the first index. To make a list go backwards, you would do aList[::-1]. To select certain index from the list use this: The first number in the [] is what index to start at. The second is the one to end at. The third is the stride (if you did aList[::2] then it would print only odd numbers). Just remember, that the numbers are seperated by colons(:). If you want index to start at the beginning and end at the end, just use a colon. For example: print(aList[::]) would do the same as print(aList).
LISTS CAN ALSO BE STRINGS

Loops

There are two types of loops in Python: For loops and While loops. I will first teach you, for loops. Here is an example of a for loop that I will explain:

Here is what it prints:

This is saying, for i in friends
print (i)
but it means for the elements(the names) in friends, print the elements. I will go more into detail in my video, and my advanced tutorial.

Now, for while loops. Here is an example of a while loop, and what it runs:

This one, may be confusing, so I will break it down for you. i = 99 creates a variable with the value of 99. The while, is saying while i(99) is less than 100 then print(i). It also decreases i so it print down from 100. The if is saying when i is less than 0, then break (end) the loop.

Quiz

Congratulations!

Now, you know the basics of Python, and can either review with my video tutorials, do my easy challenges, or learn advanced Python! You have come so far and I am sure that you will get the hang of Python(if you don't already have it)!!


Congratulations!!
Listen to me saying congratulations:

*please note that videos were removed from this website because the files were too big to upload*






Help

Who:

What:

What comment, do you want to put a solution to (numbers and struggler is the 1st one and if you want to change the user then type:user1, then user 2, etc.):

What do you want the solution to be:

From What is the problem? Solution
PythonCoder I need help with the while loops. Solution: Check out my video, or just use for loops.
struggler I cannot get the hang of Python
harrypotter How do you do functions?