18
Aug

Lesson 5 – Python Programming (Automate the Boring Stuff with Python)


Welcome to lesson 5, which roughly
covers pages 38 to 45 of the “Automate the Boring Stuff with
Python” textbook. Now that we know about boolean values, comparison operators, and boolean operators we can start looking at flow control statements. The simplest flow control statements are
“if” and “else”. Open up a new file editor window by
clicking on File>New File and enter the following code: name=’Alice’
if name==’Alice’: print(‘Hi Alice’)
…and then go back to the original indentation and type print(‘Done’)
I’m going to save this as if_example.py and press F5 to run it. You can see that this code prints out ‘Hi
Alice’ and ‘Done’. Let’s take a look at the if
statement first. Here’s a flow chart diagram for it. The expression in the if statement is
called a condition. “Condition” is just a name for
an expression in a flow control statement But technically a condition and
expression are the same thing. If the condition in an if statement
evaluates to True, the execution enters the indented code
that follows. If the condition is False, the execution
skips the indented code. There’s a great visualization tool at pythontutor.com that can show us what’s happening in
this program. I’m just going to but technically a condition and
expression are the same thing “Visualize Execution”. We can click forward to step through the program one line at a time. So first Alice is
assigned to the name variable. Then the condition is checked, and it does
evaluate to True so the execution enters this indented code and prints ‘Hi
Alice’. And then finally it leaves the block of
this indented line of code… and print… and executes print(‘Done’) Let’s go back and change this slightly.
We’ll change this so that ‘Bob’ is assigned to name. You can see that
this condition evaluates to False now, so the latest
code after it is skipped and only ‘Done’ then the condition is checked, and it does
evaluate to True, so the execution enters this indented code and prints out ‘Hi Alice’. The indentation is how Python tells
what part is inside the if statement’s block, and what
isn’t. A block begins when the indentation increases and ends when the indentation returns to its previous level. Blank lines are ignored for the sake of
looking at what is in or not in the block. So this is how you can tell that print(‘Hi Alice’) is in a block because its indentation is
increased compared to the previous line. Here’s a small game program that I wrote. You don’t have to pay attention to the code, just look at the indentation and you
could tell where blocks begin and end. So you can tell, here, the indentation is increased so this is starting a new block. And then the indentation returns back to
normal here, so that’s the end of the block. This is the complete block. Right here you can tell a new block has started and then the
indentation is increased again on this line. So this is a block inside of this block. And then the indentation returns to its
previous level so this continues on with the previous block. And then the
indentation returns back to zero here. That’s the end of this block. This is
all one block, it just contains another block inside of
itself. And then looking on you can see that this is also a block, it just contains
this blog- this one-line block here and this one-line
block here. And then another block begins right here. Blocks are also sometimes called clauses.
But just like how “condition” is just another name for an “expression”,
they’re the same thing. There’s a nice reminder: New blocks begin only after statements
that end with a colon, like in the if statement in our previous program. You can also check it out right here
here’s a colon and it begins a new block here. Here’s another call in this statement
begins a new block. This colon and so this is a block inside 0 this block on the indentation returns to its
previous level so this continues on with the previous blog and then the
indentation returns back to zero here let’s see and I’ll this block this is
all one block it just contains another block inside
itself been looking on in see that this is also a block it just contains
this blog this one-line block here in this one-line
block here and then another block begins right here blocks are also sometimes called clauses
but just like hell condition is just another name for an expression
they’re the same thing there’s a nice reminder new books begin only after statements
that end with the colon look in the if statement in our previous program you know so check it out right here
here’s a call and it begins a new blog here here’s another call in this statement
begins a new block this call and shows that a new block is starting in our name equals equals als example
the condition was false nothing happened but we can use in ill statement so that could run
specifically when a condition is false screwed in new program and take in the
following code password equals swordfish if password equals equals swordfish Colin print access granted and hit backspace to get rid their
indentation tight else Colin print wrong password then save this as def health example in plain English
this reader’s if password is equal to swordfish print axis created or else print wrong
password what’s copy and paste this into the
online play fun to turtle if the condition is true than the if
block is executed and the else plot is skit but if we set it so that the condition
would be false than the if block is skipped in the else block is
executed one and only one of the blocks will be
executed the flow chart for this program would
look something like this: you can see that any path from start to
end will go through one and only one of the blocks so with the Italian statements wanna two
blocks is executed but you might have a case we want one of
many possible blocks to execute the belet statement is in else if
statement it lets you provide as many additional conditions to check
as you need open a new file editor window and enter the following code: in me was bom H equals 3,000 if name equals equals Palace the print hi alice bill if pages less than 12 print they were not Alice kiddo them bill if the page is greater than 2000 print unlike you else’s not him on dead immortal vampire they live be discreet than a hundred print they
were not Alice grainy at save the says yet Ellis example in let’s run this
program we can copy and paste this into the
online play fun to Turtle to see what exactly is happening here the string bob is assigned to that name
the integer 3,000 as a side aged it’s an hour at the if statement
this condition is checked and it’s false so it’s gets that block
then this elliff statement has its condition
checked it’s also falls we skip that in here this is the first
true condition that we found to the execution
enters inside this block prince that out and then it
just gets all the other conditions you can have as
many lol statements following if statement as you need but the order of the old statements does
matter the execution enters the first block that has a true
condition the rest to the conditions won’t even be checked you can also add Intel statement to the
end of the chain Ellis statements the L statements block
will execute if all the previous conditions have been
false worst thing about flow control
statements sometimes you might see code like this this condition is kinda weird the name
variable is set to whatever the users take did but input will be returning a string
value not a bully in true or false value the reason this code works the
conditions can use truth he and full C values for Strings the blank string is a full C
value condition evaluates the blank string
it’s considered to be the same as the false boolean values all the other non play extreme values
are true the values was for running this code entry name again taken Ellis this is
thank you for entering a name sent a non-playing string like alice is
truth the then this was considered to be a true
condition so it entered this block of code but if I run this
again and entered nothing the blank string is
considered a faulty value so this condition is considered to be
false and so this block code was executed will this code is works and it’s on a
shortcut in general it’s better to be more explicit you should change this
line to be something like give name does not
equal the blank string for integers the value 0 is the full see value and every other
integer value is true fee and the 0.0 floating point value is
false EE and everything else is true T you can use the ball function to return adequately boo in value of
water-filled value pass at the screen like the string
and into functions that we’ve seen before so if we pass 02 ball been see that’s a
full CW ball returns falls if we pass any other
injure returns true you can do this with string
values as well hello is the truth the value where’s the
black string is a full CW to recap if statement can be used to
conditionally execute code depending on whether or not the if
statement condition is true or false in Ellis that is in else its statement
can follow in if statement and its block executes if the condition is true in all the
previous conditions have been falls Intel statement comes at
the end its block is executed all the previous
conditions have been false and finally the values 0 0.0 and the empty string are considered to
be full C values when used in conditions they’re
considered to be the same as the falls Windows he can only see which for
yourself which values are true tearful see bypassing them to the pool function

Tags: , , , , ,

67 Comments

  • Joe says:

    Hi! What program do you use to make your flow charts?

  • Michael Veach says:

    Hi Al.

    Just got your book and really loving it so far. Thanks for taking the time to make these videos as well; they are the perfect recap.

  • tyjani123 says:

    Hey Blank Edito thanks for the assistance I was pretty stuck on this section in the book! Btw, you should totally share your game code you referenced in this video I would love to see how it runs!

  • TakeRenaHome says:

    Thanks! Made it easier to learn python!

  • Primož Kramberger says:

    total noob here, this is amazingly fun, thank you!

  • Sona Buvelle says:

    hi guys can u help me ?
    phython itself is a calculator but i want ween i put a Problem like 5 + 5 * 1 / 9
    = 5.555555555555555 but with a dialog like this

    5.555555555555555 ('This is the answer')
    i want this ('This is the answer') ween a problem is solve

    5 + 5 * 1 / 9
    This is the answer – 5.555555555555555
    i want put this but i dont know how to do it

    # Test

    print('hello user')

    print('What is your name?') # ask for there name
    myName = input()

    print('It is good to meet you Im A Calculator, ' + myName)

    print('Calculator help people Find answer Quickly in Math')
    print('hit me with any Problem')
    p

  • Pagrindiniai Kanalai says:

    Please help i get unexpected intend where is the problem?

    print ('Hello player')
    print ('What is your name')
    Vardas = input()
    print ('ok ' + Vardas)
    print ('lets begin the game')
    Atsakymas1 = 25
    print ('5+20')
    atsakymas = input()
    if atsakymas == Atsakymas1
    print ('Good job')
    else
    print ('Fail')

  • Eva Fang says:

    I really enjoy your classes. Great videos!

  • monhuntui says:

    Oh gosh it took me a while to wrap my head around how the != condition worked in this example, but I got there in he end.

  • Aleksandar Zdravkovic says:

    I NEED TO WATCH THIS TODAY!

  • Irfan Ullah says:

    Dude, you are the best

  • d0mbee87 says:

    Uplink player 🙂

  • David M says:

    thank you for these fucking videos, very helpful

  • Night Shade says:

    @Blank Editor if i wanted to make A bot on git hub how would i do that?

  • Coco B says:

    this is so boring i dont want to learn about this i go to school

  • Coco B says:

    this is so boring i dont want to learn about this i go to school

  • Infinity & Beyond says:

    you lost me at else if

  • Kenneth says:

    Ooooo, I really would like to try that game :3

  • Sedric Nicole Urbano says:

    Blank Editor > MyProfessors
    True

  • Jett Cloud says:

    I'm a little confused about the segment on truthy and falsey values, why is it better to say
    if name != ' '?
    Like in the book to explain it the sample code used says:
    name = ' '
    while not name:
    print('Enter your name:')

  • SAMHARA VADIVEL PERIANNAN says:

    but if name did not work in pyton 2.7.13 new

  • Jo says:

    How do you indent on the shell?

  • Péter Janó says:

    The subtitles not long after the video starts is a bit off, but later comes back to point.. maybe you could check on that, or you cannot change subs after uploading? Thank you for your videos, love them!

  • Bashu Biswas says:

    I'm just starting to learn python, I wrote this program below, but after putting my age, it shows error. Please help.

    print('What is your name?')
    myName=input()
    print('How old are you '+myName +'?')
    myAge=input()
    if myAge >= 11:
    print('Welcomr Back Sir!')
    elif myAge < 10:
    print('Which class are you studying in?')

  • Retard Devil says:

    When i tried to run the file it just said invalid syntax …… i have saved it with .py and still it says so
    what shall I do now?

  • Howlie says:

    Thank you for taking your time. Till now I had no idea how to code but I really wanted to get into it so I watched a couple of your videos and this is my first creating (which im proud of) :

    print ('Choose password.')
    password = input()
    print ('Do you choose to save the password ' + password)
    Anwser = input()

    if Anwser == 'yes':
    print ('Okay, welcome sir.')

    else:
    print ('Choose new password.')
    newPassword = input()
    print ('Do you choose to save the password ' + newPassword)
    newAnwser = input()
    if newAnwser == 'yes':
    print ('Welcome.')

    print ('Enter password here.')
    Person = input()

    if Person == password or newAnwser:
    print ('Access granted.')

    else:
    print ('Wrong password, try again.')

  • Jasmin Khangura says:

    After watching some videos on python and taking a class, your course is the most well-organized and practical. And it's free! Thank you so much for taking the time to put this course together and make these easy to understand videos!

  • iRate says:

    Look, What I made.

    print('Please Create a password.')
    name = input()
    if name != '':
    print('Thank you very much.')
    else:
    print('You did not enter a password')

  • amin mohamed says:

    great videos, just started to learn python
    cant wait till i get to the other videos, here is a little thing i created that resembles a website log in or sign up

    print('please enter your name')
    myname = input()
    print('Hello '+ myname)

    print(' is it your first time on this site')
    answer = input( )

    if answer == 'yes':
    print('Please write your full name')
    myname = input()
    print('email:')
    myemail = input()
    print('write your password:')
    password = input()

    if len(password) < 7:
    print('your password is weak, please rewrite')
    password = input()
    if len(password) < 7:
    print('your password is still weak please rewrite with more than 7 characters')
    password = input()
    if len(password) < 7:
    print('password is still weak, please restart the process')
    else:
    print('new password accepted')
    print('your password is good')
    print('your full name is ' + myname )
    print('your email is ' + myemail )
    print('your password is ' + password )

    elif len(password) >7 :
    print('your password is accepted')
    print('your full name is ' + myname )
    print('your email is ' + myemail )
    print('your password is ' + password )
    else:
    print('your password is strong')

    else:
    print('please log in ')
    print('wite your email address: ')
    email = input()
    print('write your password: ')
    password = input()

    if len(password) > 7:
    print('access granted')
    else:
    print('access denied')

  • chirag rahul rayala says:

    name == 'RAHUL'
    age == 19
    if name == 'RAHUL':
    print('Hi RAHUL')
    elif age < 12:
    print('RAHUL is not 12.')
    elif age > 19:
    print('RAHUL is not more then that.')
    elif age > 1000:
    print('RAHUL is not an immortal.')

    CAN YOU SPOT MY ERROR….!!!!!!!!

  • chirag rahul rayala says:

    can you spot my error.

  • Guffey's Games says:

    hey to what extent can you automate every day tasks. If seen several vids like this but never have the spoken about everything that can be done.

  • Dilip kumar Sharma says:

    Thanks Al, Just bought the course on Udemy.
    Just 1 thing , Udemy charged me in $ instead of INR so i have to FX also. Please see if you can do any thing. It should charge in local currency.

  • DJARO says:

    I made a little program that lets you encrypt a message with a password, and the only way to see the password again if you enter the right password

    information = input('what mesasge do you want to encrypt n')
    password = input('what shpuld the password be? n')

    unlocked = 0
    while unlocked == 0:
    givenpassword = input('type password to see the message n')
    if givenpassword == password:
    print("the message is: "" + information + """)
    unlocked = 1
    input()
    else:
    print('wrong password')

  • Khan Aamir says:

    It showa error, every time.

  • Khan Aamir says:

    Your teaching does not work on python 3.6

  • Suspicious says:

    Does his book deliver to Canada 🇨🇦

  • HARRY POTTAH says:

    Dunno why our school taught us C++ (Which was hard for most to understand).

    But still, am thankful for the knowledge given to me and if i were to choose between C++ and Python, i'd choose this lol.

  • Prince Joe says:

    What does elif means?

  • Christian de MERONA says:

    Ha ! Password 'swordfish' ! Love the Marx brothers reference XD https://youtu.be/ySqec8WrEQQ?t=8s

  • MisterBiter says:

    Best Teacher="Al"

    if Best Teacher=="Al":
    print ("You are correct, kiddo.")

  • praso kaarshi says:

    That's wrong, or just check me
    if a variable is created under 'if' statement,
    it may be called outside a function, i.e. python doesn't ignore if 'if'-statement is false,

    i don't know what it does, may be variable class is just independent from execution of statement
    if you didn't get this after this video, please delete your system32 folder from C:\Windows

  • Pixleat says:

    print("Can you solve this?")
    print("(50 * 3) + (77 – 43) + 88 / 2 + (28 * 5)")
    solves = input()
    if solves == "368":
    print("Correct")
    elif solves <= "368":
    print("Incorrect")
    elif solves >= "368":
    print("Incorrect")

  • Xin Zhao says:

    Which video will end all these boring basices and starts to do something as the title montioned?

  • Andoxico says:

    5:24 Did anyone else catch the Citizen Kane reference?

  • Crewz says:

    Sup everyone,

    I'm new to programming.

    This simple thing I made today!
    I am proud of it xD Profesional programmers will laugh now xDDD

    print("Hi there, what is your name?")
    myName = input()

    if myName == 'Joey':
    print('Hey Joey! Welcome back!')
    else:
    print('Nice to meet you ' + myName)

  • Monkey Madness says:

    5:55 How to like stop the thing when wrong password?

  • zeldaclassicidiot says:

    I was the 1000th like 😀

  • Nick Fitzpatrick says:

    #NBA Player Comparison. My 1st Ever Program

    print('Welcome to NBA Player Comparison.')
    print('We compare you to a player based on your best skill.')
    print('Choose one: score, pass, dribble, dunk, shoot, block')

    password = input()

    if password == 'score':
    print('Since you can score really well, you play most like Kobe Bryant.')
    if password == 'pass':
    print('Since you can pass really well, you play you most like Steve Nash.')
    if password == 'dribble':
    print('Since you can dribble really well, you play most like Kyrie Irving.')
    if password == 'dunk':
    print('Since you can dunk really well, you play most like Shaq.')
    if password == 'shoot':
    print('Since you can shoot really well, you play most like Steph Curry.')
    if password == 'block':
    print('Since you can block really well, you play most like Ben Wallace.')

  • aimbot robin says:

    caves with treasures? you gonna link that game, sir?

  • JuggleBoy says:

    I just came over a discord video that told me how to make a bot and he said i needed some python experience and reccomended me this and than i started watching and got really interested so now i just f*** it on the discord tutorial and im gonna learn alot more about python 😛

  • payne max says:

    my god u are a really good teacher!best i see on YouTube so far thanks for the videos

  • Ian Sullivan says:

    SPEAK LOUDER BILLY, I CAN'T HEAR YOU OVER THE SOUND OF YOUR INSANELY LOUD TYPING. What did that keyboard do to you?

  • Israphial says:

    Oooh, new mic in this one!

  • AJAY SINGH SHAKYA says:

    >>> bool(' ')

    True

    >>> bool(' ')

    True
    spaces and tabs gave true in bool() why

  • Active bestway says:

    SyntaxError: multiple statements found while compiling a single statement

  • Active bestway says:

    that website isnt working recheck it please

  • Donnie Darko says:

    I havent seen all of the videos, but just wanted to know if i just buy his course on udemy, if the complete course is not on youtube

  • Aegean Kosyn says:

    Dude Your sponsor on your website which is called 'trinket' doesn't run well on Mac! From that 2nd Chapter it really took all of my inclination to learn programming! Randomly it loads and takes you to another part of the page! Can you please fix that !!!?

  • Aegean Kosyn says:

    Seriously if you decided to make it free than just make it really free, not pay a little to get more than the free version ! Please !!!

  • Richard Külling says:

    The subtitles seem to be not in sync with the video

  • MAYURI TIWARY says:

    Great class .I m all new in programming.But I dared to take this just becoz of you.Hoping to do well in python.Thanks a lot for such a wonderful class.You are a great teacherrrrr. Lots of love from INDIA

  • Qadehar Potter says:

    Hello . I typed " if number <= 20 or >= 30:" but all I got is SynthaxError Message

  • Tùng Nguyễn says:

    My ver is 3.7. After having tested, I realized that:
    – 'space' or 'tab' were considered as values of a var -> 'if name:' would output True value even if I inputted 'space' or 'tab'.
    – 'space' or 'tab' would be falsey in bool()

  • real name says:

    I was chasing a dancing Al Sweigart through the depths of a dark Dantesque forest… when I fell through an unseen rabbithole… down, down I fell into the underworld of the code…

  • Twitch Clips says:

    print("Thank you so much Al Sweigart! Can you teach us more?")

    answer=input()

    if answer == yes
    print("<3")

    else:

    print("</3")

  • Michael Schöne says:

    Hey man,
    very nice tutorials and a very nice book.

    Great job!

Leave a Reply

Your email address will not be published. Required fields are marked *