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
Review, review, review, review, review, review; REVIEW
Review on world politics, and the effects on the French
economy after world war 2
Review on everything we have learned
There will be more review after the review
Questions & Answers
-2-
IDLE
• IDLE, is what’s used to code python, its called IDLE just
incase you seemed to of forgotten, It is better than the
command line in every way, this is what were using.
IDLE. IDLE has 2 modes, one mode is the command line
mode. It works just the same as the command line, but is
white and has color; the other mode is for writing multi
lined programs and saving them. Please never make me
have to open IDLE for you again.
-3-
Everything we know so far
• These are the things we all should know by now, but we
will be going over them with small practices, and less
images
• Data types
• Arithmetic stuff
• Variables
• Boolean type operators
• If statements
• Else statements
• Functions
-4-
Data types
• Data type is anything that is represented as a
values in Python
• 10 – int
• 10.1 – float / double (not really though
)
lesson)
• “I am word” – string
• True - boolean
-5-
(but that is for a nother
Small exercise
• Can you name these data types
• “10.1”
• 22.0
• True
• “False”
• -123
-6-
Converting data types
• In python you can convert data types to each other
here are the example conversion types
• str() – converts anything to a string
• int() – converts anything to an integer
• float() – converts anything to a float
• bool() – converts anything to a boolean
-7-
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)
-8-
Arithmetic
• This is a simple one
•
•
•
•
•
•
+ adds two numbers/strings
- subtracts two numbers
* multiplies two numbers
/ divides two numbers and will give decamle
** is an exponent like 2 ^ 2 but with two stars like 2 ** 2
// is divide also but it gives back an integer
-9-
Comparative type operators
• Comparative types operators are just like
Arithmetic type operators but with booleans
• == equals - if both values are the same it returns
true
• <= less than or equal to – compares if the integer
in front is less than the second one
• >= greater than or equal to – compares if the
integer in front is more than the second one;
- 10 -
Comparative type operators 2
• Comparative types operators are just like
Arithmetic type operators but with booleans
• and And: if both booleans are true it returns true
• Or Or – if at least one of the booleans are true it
returns True, other wise its false
- 11 -
If statements
• If statements allow for logic In your code, it takes three
steps to write an if statement
• 1 type if
• 2 put parentheses after the if like if()
• 3 put a colon after it if():
• That’s all it takes to write an if statement, now all you have
to do is fill it up with a boolean in the middle
- 12 -
Example if
• All of these ifs have a boolean inside of it
• Tell them to type it in their IDLE
- 13 -
Write your own if
• Write an if that compares if 10 is less than 10.1 and if 1
equals to 1
•
<img src=“traceback error cannot find image(s) src” />
- 14 -
Ifs
• I hope yall are good with if statements now, Any questions,
if no one says anything and say your good, you better never
ask me a question again, and go to MIT to graduate right
now with a comp sci degree in Python analitics(I am just
kidding of Course)
- 15 -
One Input for functions
• When you define a function, inside the brackets, you can
tell python you want an input by giving it a new variable to
use. like
- 16 -
When you define an input
• As you can see, at the top of the function, specified to
python that this function requires one input, (name). This
variable is only visible inside the
function.
- 17 -
Defining multiple inputs
• Obviously in python you can define multiple inputs, but to
do that all you have to do is create more variables and
separate them with commas, and when you use them you
call them you give the required inputs to run the function
- 18 -
Lets make an adding function
• Even though this function will be useless in real world
applications, it is still good practice. First we define our
function and call it like addSum() or something, then we
define 2 inputs, them inside the function, we add them
together and store it into a variable, then we print the
variable!
- 19 -
Out puts
• Lets say you want your function to give you a number as an
output, instead of printing every thing.
• All you have to do is type return and give it a variable to
return like
- 20 -
Did it do anything?
• If you typed that out and compiled it, you'll see that nothing
“appeared” to of happened. Well something did happen you
just can’t see it. When you called the function it returned a
number for your to use, all you have to do to get that
number is to set it equal to a variable, as you can see it is
now treated as a number
- 21 -
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
- 22 -
Questions & Answers
- 23 -
© 2014 Chris Trenkov