instructor: chris trenkov hands-on course python for absolute beginners (spring 2015) class #005...

29
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)

Upload: sheena-walters

Post on 26-Dec-2015

231 views

Category:

Documents


2 download

TRANSCRIPT

Instructor: Chris Trenkov

Hands-on CoursePython for Absolute Beginners (Spring 2015)

Class #005 (April somthin, 2015)

- 2 -- 2 -

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

- 3 -- 3 -

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.

- 4 -- 4 -

Incrementing with out increment operations

• How would you increase the values of a variable by 10 when the variable equals 3.

- 5 -- 5 -

Here

• This is normally the way to do it. It is ugly and has no mother.

- 6 -- 6 -

Basic increments

• The first type of increment is the += you use the plus equals to increment the values by the operator next to it.

- 7 -- 7 -

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.

- 8 -- 8 -

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

- 9 -- 9 -

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)

- 10 -- 10 -

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.

- 11 -- 11 -

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.

- 12 -- 12 -

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.

- 13 -- 13 -

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.

- 14 -- 14 -

Example

• This is my example, guess what's going to happen

- 15 -- 15 -

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

- 16 -- 16 -

- 17 -- 17 -

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.

- 18 -- 18 -

Write your own

• Write your own infinite loop and do something creative with it. If it works don’t do anything. Let it be.

- 19 -- 19 -

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.

- 20 -- 20 -

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.

- 21 -- 21 -

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,

- 22 -- 22 -

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.

- 23 -- 23 -

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.

- 24 -- 24 -

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.

- 25 -- 25 -

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.

- 26 -- 26 -

Fibonacci sequence.

• Example use of for loop

- 27 -- 27 -

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

- 28 -- 28 -

Questions & Answers

© 2014 Chris Trenkov