Python For Absolute Beginners
Python For Absolute Beginners
Python For Absolute Beginners
Run
Save
Fresh URL
Open Local
Reset
Additional Resources
Docs (documentation)
Demos
Viz Mode
Let’s see what this thing can do…
Recap:
Operators:
add: +
subtract: -
More operators:
divide: /
multiply: *
>>> print 3 * 12
>>> print 12 / 3
>>> print 11 // 3
>>> print 12.0 / 3.0
>>> print 11.0 / 3.0
>>> print 11.0 // 3.0
Math
Comparison operators:
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
Math
Practice:
String operators:
concatenation: +
multiplication: *
>>> type(Beatles)
>>> type(grades)
Lists
List: a sequence of objects
>>> Beatles = [“John”, “Paul”, “George”, “Ringo”]
>>> grades = [82, 93, 67, 99, 100]
>>> type(Beatles)
<type ‘list’>
>>> type(grades)
<type ‘list’>
Lists
Index: Where an item is in the list
>>> Beatles = [“John”, “Paul”, “George”, “Ringo”]
>>> Beatles[0]
‘John‘
TX
if Statements
Adding many choices: “If you like Frisbee, let’s play! Or else we
can play rugby. Or else we can play
Bioshock, or Half-Life, or Portal…”
The for keyword is used to create this kind of loop, so it is usually just called a
for loop.
Loops
Conditional loops repeat until something happens
(or as long as some condition is True).
>>> count = 0
>>> while (count < 4):
print 'The count is:', count
count = count + 1
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The while keyword is used to create this kind of loop, so it is usually just called a
while loop.
Algorithms
Algorithms
Secret: computers
aren’t very smart.
How would I make a pot of coffee?
1. Get a flavor of ground coffee.
2. Get a coffee maker.
3. Get filter paper.
4. Get a pot of water.
5. Make sure the coffee maker is plugged in…
>>> beverage()
Have you had a cup of coffee today?
Functions
But what if not everyone wants a cup of coffee?