Here - Chris Trenkov`s Programming Projects

Download Report

Transcript Here - Chris Trenkov`s Programming Projects

Python for Absolute Beginners (Spring 2015)
Hands-on Course
Class #005 (April somthin, 2015)
Instructor: Chris Trenkov
Objectives
Some review
We will be going into increment operators
We will also do some loopage
Then we will make some awsome programs
Questions & Answers
-2-
Incrementing numbers
• In most programming languages there is a way to
increment variables. This mean the variable can equal
one more than it previously did. For example if I want to
increase var, which equals 10, by 2, var will equal 12.
That is pretty much it.
-3-
Incrementing with out increment operations
• How would you increase the values of a variable by 10
when the variable equals 3.
-4-
Here
• This is normally the way to do it. It is ugly and has no
mother.
-5-
Basic increments
• The first type of increment is the += you use the plus
equals to increment the values by the operator next to it.
-6-
Decrement
• Lets say you want to decement the variable, make
it one less, you use the -=, less than equals. It
does the same thing except it minuses the current
values of the variable by the number next to it.
-7-
More types in increments
• You can increment the values using any operation
like
• Var += 2: adding
• Var -= 2: subtracting
• Var *= 2: multiplying
• Var /= 2: dividing
• Var //= 2: divide rounding
• Var **= 2: taking it to the exponent, play around
with these
-8-
Fun exarcises
• Here are some intuitive conversions
•
•
•
•
•
•
•
•
•
•
•
str(12)
int(7.5)
float(7)
int(“-12”)
Here are some not so intuitive, try to guess them
str(False)
Int(True)
float(false)
bool(0)
bool(1)
bool(1337)
-9-
Loops
• In every single useful programming language there
are things called loops. A loop is a thing, like an
instatement where as if the conditions are true it
loops a block of code until they arn't true.
- 10 -
Loops the types
• In python there are 3 different types of loops, the
while loop, the do while loop, and the for loop. For
today were are only going to learn the while and
the for loop. They are effectively the but are used
for different things.
- 11 -
While loop
• A while loop it the most basic type of loop. It takes
in a boolean value, if its true it loops, if it isn't it
stops looping. That is as basic as it gets.
- 12 -
How to write a while loop
• Three steps.
• Step one: type while
• Step two: put the buns and the sesame seeds,
while():
• Step three: fill the buns with a boolean.
- 13 -
Example
• This is my example, guess what's going to happen
- 14 -
Example 2
• If you guessed nothing, you would be right. Because the
boolean is false it dosn’t loop or let anything in.
• Guess what is going to be the output of this
- 15 -
- 16 -
What happend
• Because the while loop is true it looped the print statement
over and over again, and because the True statement will
never become false it will loop for ever. Because it loops for
ever it will never reach to that print statement down they’re.
- 17 -
Write your own
• Write your own infinite loop and do
something creative with it. If it works don’t do
anything. Let it be.
- 18 -
Using the increment operator with it.
• Lets combined with what we have learned and make a
infinite loop with a variable incrementing by one every loop.
Then print it out. You can see the speed of the computer.
But this is not its fastes speed possible, the print statement
takes python a couple of milliseconds to perform so
obviousy this is not the true speed of the computer.
- 19 -
Useful while loops
• Lets write a useful while loop. I’m going to make mine
increment x to a million then exit the loop and print out x.
you can see the logic, as x is less than one million it will
loop again, until x is equal to or greater than 1000000 it will
exit the loop and print x, guess how long this operation will
take.
- 20 -
For loops
• A for loop is a more useful loop with using numbers instead
of booleans. You basically declare a variable and give it a
range to loop by,
- 21 -
Writing For loops
• This is a five step process, so be carful. There are more
complicated types of for loops used for different things like
functions, lists, tuples, and dictionaries. But this is what well
use.
- 22 -
For loops
• Basically what this does is as long as x is in range of the
range(), it will continue to loop and increments x by one.
You can see this behavior when you print x inside the loop.
You can type the same.
- 23 -
For loops
• Type your own for loop. Make your variable increment by
the for loop x every loop then print it out. Try doing this
between 0, and one million.
- 24 -
Fibonacci sequence.
• What is the Fibonacci sequence, It is basically a sequence
that starts with 0, and 1, it then adds the two numbers
together making 1, it then adds the sum to the previous
number, 1, making 2, it then adds 2 to the pervious number.
It does this.
- 25 -
Fibonacci sequence.
• Example use of for loop
- 26 -
Seriously????!!?!?!?
• Are there any questions, or concerns, would you like me too
explain something again, This is the most fundamental part
of programming, and is what allows you to do some really
awesome things later down the line
- 27 -
Questions & Answers
- 28 -
© 2014 Chris Trenkov