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

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

Dec 26, 2015

Download

Documents

Sheena Walters
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)

Instructor: Chris Trenkov

Hands-on CoursePython for Absolute Beginners (Spring 2015)

Class #005 (April somthin, 2015)

Page 2: Instructor: Chris Trenkov Hands-on Course Python 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

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

- 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.

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

- 4 -- 4 -

Incrementing with out increment operations

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

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

- 5 -- 5 -

Here

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

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

- 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.

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

- 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.

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

- 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

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

- 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)

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

- 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.

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

- 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.

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

- 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.

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

- 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.

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

- 14 -- 14 -

Example

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

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

- 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

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

- 16 -- 16 -

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

- 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.

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

- 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.

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

- 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.

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

- 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.

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

- 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,

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

- 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.

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

- 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.

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

- 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.

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

- 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.

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

- 26 -- 26 -

Fibonacci sequence.

• Example use of for loop

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

- 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

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

- 28 -- 28 -

Questions & Answers

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

© 2014 Chris Trenkov