0% found this document useful (0 votes)
2K views

Programming Foundations Fundamentals

This document provides an overview and introduction to the fundamentals of programming. It discusses what programming is, explaining that programming is the process of converting ideas into instructions that a computer can understand and execute. These instructions must be specific and sequential. It then discusses what a programming language is, noting that there are many programming languages with their own syntax and semantics. It provides examples of the "Hello World" program in different languages like C++, JavaScript, and Python to illustrate differences in syntax between languages. Finally, it discusses writing source code, noting that code is written in plain text using a text editor.

Uploaded by

Nemanja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Programming Foundations Fundamentals

This document provides an overview and introduction to the fundamentals of programming. It discusses what programming is, explaining that programming is the process of converting ideas into instructions that a computer can understand and execute. These instructions must be specific and sequential. It then discusses what a programming language is, noting that there are many programming languages with their own syntax and semantics. It provides examples of the "Hello World" program in different languages like C++, JavaScript, and Python to illustrate differences in syntax between languages. Finally, it discusses writing source code, noting that code is written in plain text using a text editor.

Uploaded by

Nemanja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 67

The fundamentals of programming

Selecting transcript lines in this section will navigate to timestamp in the video
- Now more than ever, you hear about the importance of STEM education and the
need for qualified computer programmers. Maybe you even know someone who's
started learning to code. Or perhaps you've heard about how programming can be a
rewarding, lucrative career. Whatever the reason, this course is designed for
you, someone who's curious to pull back the curtains and explore the world of
computer programming. I'll walk you through your first line of code, explain the core
concepts needed to write useful programs, and take a peek at several programming
languages along the way. I'm Annyce Davis, and I've worked in the software
industry for over a decade, and programming is one of my favorite things to do. I also
love to share what I know with others, so I'm really excited to help you master the
fundamentals of programming here, on LinkedIn Learning.

Following along with the course


Selecting transcript lines in this section will navigate to timestamp in the video
- Throughout the course I'll be using a Mac computer  but it's not a requirement that
you do the same. You can follow along using Windows or Linux as well. Just make
sure you feel comfortable downloading software and working with the file
system. But don't worry, I'll walk you through every step. This course comes with
exercise files. You can download them and store it on your Desktop, like you see
here. The Exercise Files folder is broken into various chapters. Each chapter
corresponds to a chapter of the course. Inside each directory you will find files that
map to the video. For example, if you wanted to look for the third video from chapter
four, you'd come to the chapter four folder and look for 04_03. If you see files that
end in _begin, this is the state of the file at the beginning of the video, whereas if you
see _end, this is the state of the file at the end of the video. But notice, 04_02 and
04_06, they don't have any _begin or _end. That's because the code contained in
these files are just for reference. We're not going to make any changes to those. This
makes it easier for you to compare the beginning and ending states of our file
changes. Keep in mind though, not all videos will have exercise files. But don't worry,
I'll make sure to call out which file we're working with as we go throughout the
course.

What is programming?
Selecting transcript lines in this section will navigate to timestamp in the video
- What is programming? Well, when you pick up your phone and check the
time, respond to a message, or comment on a friend's picture, you're experiencing
the results of what someone programmed your phone to do. Programming is the
process of converting ideas into instructions that a computer can understand and
execute. These instructions are specific and sequential. You can think of it a bit like a
recipe. Let's say you wanted to bake a cake. You would first need a list of
ingredients and then the set of instructions. For our ingredients, we have flour, sugar,
eggs, milk, and so forth. If you've ever baked a cake, you know that the amount of
each ingredient can dramatically affect the outcome. If you accidentally use a full cup
of milk instead of 1/2 a cup, then you'd have runny mess on your hands. Or what if
you swapped the baking soda for salt? The cake would never rise. In a similar way, the
instructions we provide to computers need to be specific so that we can get our
desired result. Computers are very literal. They try to execute our commands
exactly. When we give them bad instructions, we might introduce bugs or even make
the computer crash. In programming, a bug is when something unexpected
happens, whereas a crash is when your program stops early or freezes, just like if you
use the wrong ingredients in your cake. Instead of a fluffy, moist cake, you could have
a flat, runny disaster. The other characteristic of computer instructions is that they're
sequential, just like the steps in a recipe. The order of the steps will impact your final
product. If we mix the baking soda in before the eggs, we can cause our cake to be
dense, or adding the milk with the butter may cause the dry ingredients to clump and
leave us with a lumpy cake. Order matters. The same thing can be said about
programming. It's all about giving computers the right instructions and the correct
sequence of steps to produce the desired result. This is important because
programming is just as much about finding errors and preventing crashes as it is
about writing the instructions or code that the computer understands. So, to
recap, programming is how we communicate with the digital world. It's characterized
by having instructions that are specific and sequential. And when we get our code
right, just like in a recipe, the end result is a program that helps us to enjoy a bit more
of our world.

What is a programming language?


Selecting transcript lines in this section will navigate to timestamp in the video
- When you want to provide instructions to a computer,  you use what's called a
programming language. There are hundreds, if not thousands, of programming
languages available, each made up of its own syntax, or rules, and semantics, or
meaning. Let me give you an analogy. In English, we would write, Welcome, and use
on exclamation mark, but in Spanish, we would write, Bienvenido. Semantically, they
mean the same thing, but Spanish, unlike English, requires an exclamation mark at
the beginning and end. This is one rule, or a syntax, of the Spanish
language. Likewise, programming languages also have unique syntax rules. Let's look
at a few examples of the Hello, world program. Maybe you've already heard of it
before. It's the most basic program used for decades to get started with a new
programming language. It helps to highlight the differences in syntax between
languages. First we'll look at a language called C++. This is how we would get the
program to display Hello, world when run. Now, it may look strange to you, but that's
okay. Soon you'll be able to understand exactly what's happening. For now, I want
you to just notice the syntax used in the language. This consists of double quotes
around the words Hello, world, the use of a semicolon at the end of statements, and
there's a return keyword right before a closing curly brace. All of these things are part
of the syntax, or rules, of the C++ programming language. Let's look at a few more
examples. This language is called JavaScript. It's a very popular language used
in virtually every website these days. This is how we would display Hello, world using
JavaScript. Notice the use of different syntax. First, the words Hello, world are now
inside of single quotes. We don't have any curly braces. Instead, we see something
about document.write. And finally, here's Hello, world in Python. Similar syntax to
JavaScript, but now we don't see any parentheses, and the use of double quotes has
returned, just like we saw with C++. Now you may be wondering, what's the point of
having so many different languages, especially if they do the same thing? Why not
just have one programming language and we all use it? The main reason why we
have so many languages is that each comes with its own set of strengths and
weaknesses. Some are ideal for programming small devices with limited memory,
whereas other were made to handle complex mathematical computations. Regardless
of the language you choose, ultimately, that language has to be broken down into
the only one that computers understand, machine language. Machine language is
extremely complex for us to write directly because it's mostly just a series of
numbers. Professor Mike DeHaan compared machine code to DNA molecules. Can
you imagine trying to read a DNA sequence atom by atom? That would take you
forever, and this is why we have these other languages that we call high-level
languages. They're closer to human languages, comprised of keywords, structure, and
syntax that's easier for us to learn and understand.

Writing source code


Selecting transcript lines in this section will navigate to timestamp in the video
- Writing source code is what programmers do all day.  It's the instructions we have
for the computer, and it's written in plain text. In other words, we write it without
special formatting, like bold, italic, or different font types. It's mostly just the actual
characters. This means that word processing applications aren't suitable for writing
code, because by default, they insert bits of information in files that prevent them
from being plain text. Instead, you can use a text editor. If you're on a PC, you already
have a text editor available to you. Notepad. And for Mac users, there's TextEdit. Both
applications allow you to write source code. Let's look at an example. I'll be using
TextEdit since I'm on a Mac. We're going to go ahead and open up TextEdit, and the
first thing that you'll notice is this formatting bar. By default, TextEdit stores files in
Rich Text Format. That means you can do things like bold or italics, or even
underline. But this is not suitable for writing source code. To change it, we're going to
go over to TextEdit, choose Preferences, and then we're going to choose plain
text under the Format section. Also, make sure you uncheck all of the options down
below. Great. We can close our Preferences pane, and let's restart TextEdit to make
sure that our changes are still present. Perfect. Now that formatting bar is no longer
here. If you're on a PC and you're using Notepad, you don't have to worry about
this. It uses plain text by default. So let's start off by doing our favorite program, Hello
World. We're going to do the Python version. Let me zoom in a bit so you can see
what I'm doing. I'm going to type print, open parentheses, double quotes, hello
world, exclamation mark, double quotes, and a closing parentheses. We did it. We've
written some source code. Source code can either be one line, like you see here, or
thousands. It just depends on the needs of your program. Let's go ahead and save
this file. By default, TextEdit saves files with a .txt extension. This is because it's plain
text. But it's a best practice to use an extension that corresponds to the programming
language that's inside of your file. Since this code was written in Python, we're going
to use the Python extension, which is .py. So let's save this as 01_03.py. Perfect. Each
programming language has it's own file extensions. For JavaScript, it's .js, Perl,
.pl, whereas a language like Kotlin uses .kt. Now that we know how to write our
code, we need to learn how to translate it into the language that the computer
speaks. Machine language. This is a vital step to get it running on any device.

Running your code


Selecting transcript lines in this section will navigate to timestamp in the video
- If we were to double click our Python file, it would just open in an editor, but it
wouldn't do anything with the code that we put inside. Why is that? Well, it's because
Python is an interpretive language. This means we have to interpret it into machine
code and we haven't run the interpreter yet. There are three main ways to translate
source code into machine code: compile it, interpret it, or an combination of
both. Compile, interpret? Let me give you a real world example. Say you need to write
a letter to someone who speaks Mandarin, but doesn't speak English. What would
you do? You could first write it in English then use a translation service to convert
your letter into Mandarin and mail it. The other person would never see your original
letter, only the one that was in Mandarin. That's basically what compilers do. They
take your high level programming language and turn it into an executable that
contains low level machine code. This way, users can run your code on their
machine without every needing your original source code. Now the second option for
sending out a letter is to send it to them in English, then they could have a friend who
speaks English read each line of the letter first and tell them what it means in
Mandarin. Every time our Chinese friend needs to read the letter, the interpreter will
need to come back over and reinterpret the letter. This is essentially what computer
interpreters do: they process your source code each times its run, line by line, and it's
up to the other user to have the needed interpreter available on their machine. Now
this is of course an over simplification of both compilers and interpreters. They
perform complex functions that help to optimize the size, speed, and security of your
source code, but it helps to convey the general idea of how they differ. Typically,
languages like C, C++, and Objective-C are known as compiled languages, whereas
PHP and JavaScript are known as interpretive languages. And finally, Java, C# and
Python use the combination approach. Nowadays, the line between compiling and
interpreting is a bit blurry, as many languages have some variants of both. The key
thing to keep in mind, is that you may need to perform additional steps to get your
code to run on your, or someone else's machine.

Using an IDE
Selecting transcript lines in this section will navigate to timestamp in the video
- You can write entire programs in simple text editors, but in practice few
programmers do. They use enhanced text editors called integrated development
environments, or IDEs. IDEs provide features that speed up code development. When
you want to write a letter, an essay, or a resume what tool would you use? You'd
probably use a word processor such as Microsoft Word, or Google Docs. They have
special tools that help you check grammar, spelling, and add nice formatting. For
programming we use an IDE in the same way. An IDE is an application that provides
the special tools needed to write, debug, and compile code. Let's take a look at two
popular IDEs to see what they have to offer. Don't worry about downloading anything
right now, we're just going to check them out together. We'll start with Xcode. Xcode
is used to develop applications for Apple products, like the iPhone, iPad, or
Mac. Right away you can see a difference between our simple text editor and a more
sophisticated IDE. We have line numbers on the left side of our source code
editor. This allows us to more quickly reference individual statements in our
code. There's also the use of color, known as syntax highlighting, to aid in pointing
out important keywords and improve the readability of our code. And finally, notice
how you can even preview what your code will look like when compiled and run on a
real device. Definitely a great tool to improve your coding process. Now although
Xcode is really nice, let's look at one more example, Visual Studio Code. Visual Studio
Code, or VS Code for short, is a more lightweight editor than Xcode and it was
initially designed for scripting languages, such as JavaScript and TypeScript. However,
it gradually came to support many more languages through powerful
extensions. You'll find developers using it to develop apps in dozens of
languages. One of its unique features is called IntelliSense. This allows you to get
code suggestions while you're typing. It works a lot like auto-complete, when you're
sending a text or when you search in Google and it guesses the rest of the phrase
you're typing. And unlike a simple text editor, we can actually run our Python code
from within the IDE. It's really cool. And there are many more IDEs out there. My
favorite is Android Studio, as I spend a lots of time creating Android applications. But
when I'm working on an application written in Ruby I turn to RubyMine, as it's
tailored for the Ruby language. Programmers love to argue, I mean talk, about which
IDE is their favorite. If you have an hour or two to spare, just ask any programmer
what IDE they think is best. Then get ready to pull up a chair. IDEs can ease the
burden of software development by providing tools to help you write, debug, and run
your source code more easily. Ultimately, it's up to you to find one that meets your
needs, and you don't mind using it for hours a day.
Question 1 of 14

If you were writing your source code in the JavaScript programming language, what
file extension would you use to save your file?


.script

 .js

Correct


.java

.py
Question 2 of 14

What is the standard extension to use for a Python source code file?


.txt

 .py

Correct


.doc

empty extension
Question 3 of 14

If you were developing an application for an iPhone, which IDE would you use?


Android Studio

RubyMine

 XCode
Correct

Question 4 of 14

Which IDE feature allows you to get code suggestions while you are typing?

 Syntax Highlighting

Incorrect

Syntax Highlighting is used to color code elements but does not offer code
suggestions.


XCode

Device Preview

 IntelliSense

Correct

Question 5 of 14

Which tool is used to write source code?

 a text editor

Correct


a web browser

a spreadsheet application

a word processor
Question 6 of 14

Which language does not use a hybrid compiler/interpreter approach?


C++
 C#

Incorrect

 Python

Incorrect

 Java

Incorrect

Replay

Review this video

Running your code

2m 41s

Question 7 of 14

Why doesn't a Python file execute when you double-click it?


You need to use the .exe file extension.

Python code needs to be compiled to bytecode before it can be executed.

 Double-clicking does not run the Python Interpreter on the source code.

Correct

Question 8 of 14

Which language is considered an interpreted language?


C

 JavaScript

Correct


C++

 Objective-C

Incorrect

Question 9 of 14

How would you write the "Hello, world!" program in Python?

 print("Hello, world!")

Correct


document.write("Hello, world!");

std::cout << "Hello, World!"

put('Hello, world!')
Question 10 of 14

Source code is written in rich text.

 FALSE

Correct


TRUE
Question 11 of 14

Programming can be defined as: converting ideas into _____ that a computer can
understand.

 instructions
Correct


appeals

 recommendations

Incorrect

Question 12 of 14

Why do computer instructions need to be sequential?

 The order in which these instructions are executed is important.

Correct

 Providing the instructions in sequence also makes them exact.

Incorrect


A sequence of instructions results in fewer crashes.

 A sequence of instructions results in fewer bugs.

Incorrect

Bugs could also be present in a sequence of instructions.

Question 13 of 14

In programming, a crash is when your program stops early or freezes because


something unexpected happened.

 FALSE

Incorrect


TRUE
Replay

Review this video

What is a programming language?

3m 36s

Question 14 of 14

Why are there many programming languages?


to support different text entry formats

 to address many different computing needs

Correct


to directly write machine language

 to offer a syntax closer to a human language

Incorrect

Why Python?
Selecting transcript lines in this section will navigate to timestamp in the video

- When choosing the language for this course, I had two main requirements. It needed to be
popular and easy to learn. As far as being popular, right now, the top five programming
languages are Java, Python, JavaScript, C++ and C#. That narrowed down the list
considerably. But when it comes to ease of learning, both Python and JavaScript have fewer
Syntax requirements than the other languages, so let's look at those two. Both Python and
JavaScript tend to be more concise. This means it takes fewer lines of code to get things
done. They're also easier to understand, as both languages use structuring and wording that
are closer to human language. But which one to choose? Well, JavaScript was designed to
work with and manipulate webpages, and it continues to be widely used for that very
reason, although, it can also be used for non-web applications. Python, on the other
hand, was designed to be a general purpose language. You can use Python to create web
apps, internal company tools, perform scientific analysis, as well as create games. The Python
language has been around for decades, and it's available on multiple platforms including
Mac, Windows, Linux and more. It's a great choice for exploring the various concepts that
we're going to cover. So for this course, Python is the winner. We've already seen some
Python code. Remember our simple, "Hello world!" program? That was written in Python. This
is also Python code. Can you guess what it does? If not, that's okay. By the end of this
course, not only will you know what it does, but you'll be able to write some of the programs
yourself.

Installing Python on a Mac


Selecting transcript lines in this section will navigate to timestamp in the video

- If you're using a Mac computer, getting Python setup is very easy. The Mac operating
system ships with the version of Python already installed. If you're using Windows, please
skip to the next video, as I'll walk you through how to get set up. To check what version of
Python is installed, we're going to open a Terminal window. Terminal is an application that
lets you run commands to explore directories, execute scripts, and yes, work with Python. To
open up Terminal, we're going to go to our Finder, then applications. Go to utilities and open
up Terminal. You can also do this with a shortcut by typing command + space, and then
spelling out terminal and hitting Enter. Let's go ahead and close down our Finder
window. From here, we can check what version of Python we have installed. To do that, we're
going to type Python, all in lowercase letters, and then hit Enter. Let me go ahead and zoom
in a bit so you can see what I'm doing. The first thing you notice is that it prints out the
version of Python that we're running on the machine. Now, typically, when we say print out in
programming, we don't mean go grab a piece of paper from the printer. Instead, it's referring
to displaying the result of a command on your screen. Programming languages have version
numbers associated with them, because over time, new features are added. By increasing the
version number, the developers of the language are communicating that they've changed
something and it could impact how you use the language. At the time of this recording, we
have Python two dot seven dot ten installed. Notice how we're inside of the Python
prompt. This is denoted by the three angle brackets. This is letting us know that the
computer is ready for us to send it some Python instructions. But for now, we're just going to
go ahead and exit. We can do this by typing exit, and then a open and closed parentheses
and hitting Enter. If for some reason you don't see the version of Python that's installed, or
you're not sure if you're running the latest version of Python, you can head over to
python.org/downloads. Let's do that now. Here, you can see the latest version of
Python that's available for your operating system. In this case, Python three dot seven dot
three is the latest version. And since this is newer than what we have installed on our Mac,
let's go ahead and download it. Once it's finished downloading, you can go ahead and
double click the file to open it up. This will show you the Python installer. Let's click on
continue to walk our way through the wizard. Now you may have to enter your password to
let your computer know that you're okay installing Python. And that's it, we have Python
installed. Let's go ahead and close all of these windows. Now that we have it downloaded, we
no longer need this window. So we'll close it, and we're back here inside of our Terminal. Just
to make sure we have the latest version available, we're going to restart. So let's quit, we'll
use that shortcut, Command + Space bar, and then terminal. And now let's type the Python
three command, no spaces between. Great! And just like that, we're running the latest version
of Python. If for some reason you're not able to get Python installed, I've added a
troubleshooting guide for you, but otherwise, you should be running the latest and greatest
version of Python.

Installing Python on Windows


Selecting transcript lines in this section will navigate to timestamp in the video

- To install Python on your Windows machine you need to go to python.org, let's do that


now. From here go to the downloads tab and then we're going to choose download python
three dot seven dot three. Programing languages have version numbers associated with
them because over time new features are added. By increasing the version number the
developers of the language are communicating that they've changed something and it could
impact how you use the language. Now that that's downloaded we can click on the
executable in order to launch the installation process. Make sure you choose the add python
three dot seven to path checkbox option. Then you want to click on install now. Okay, now
that the setup was successful let's close this as well as the browser window. To open our
newly installed Python installation we're going to come to the start menu and then we're
going to navigate down to where you see Python. If we expand this folder there's multiple
executables present but we're going to use the one that's Python three dot seven. When we
click this, this will open up the Python prompt. It's at this point that we can start sending
some instructions to the computer. For now we're just going to exit and we'll do that by
typing exit with an opening and closing parentheses and that's it. If for some reason you're
not able to get Python installed, I've included a troubleshooting guide for you but otherwise
you should be running the latest and greatest version of Python.

Running Python on the command line on Mac (Change Title)


Selecting transcript lines in this section will navigate to timestamp in the video

- One way to execute or run your Python code is by using the command line. Let's open up
Terminal and try out a few commands. Let's go ahead and launch the Python
interpreter. We'll use the Python three command. Now we're inside of our Python
prompt. This means the computer's ready to take our instructions. Let's try out a simple math
problem. Two plus two. First, we'll try it with what is two plus two? Uh oh, we get a syntax
error. We call it that syntax refers to the rules of a programming language. Syntax errors
happen when you write code that breaks the expected rules. In this case, we wrote our code
in plain English, but the interpreter doesn't understand English, only Python. Let's try this
again. This time, we'll take two plus two, and just like that, we get four. That's because two
plus two is a valid Python expression. Let's try another one. Two plus, and then enter. Once
again, another syntax error. That's because this is not a complete Python expression. For
example, in English, I could say you're doing a great job with the course so far. That's a
grammatically correct sentence, but if I were to say, you're doing a great job with, that would
be an incomplete sentence. I didn't finish it up. Programming languages work much the same
way. They need you to follow the syntax or rules in order to understand your commands. Let
me show you one more thing. First, we're going to exit our Python prompt. We'll do that by
typing exit or we could also use the shortcut, Control D. So now we're outside of the Python
prompt. Do you remember our 01_03 file? Let me show you how we can run it from the
command line. The first thing we need to do is make sure we're in the same directory as our
file. To do that, we'll use the CD or change directory command, and we'll go to our file which
is located inside of Desktop/ExerciseFiles/Chap01. Now if we hit enter, let's go ahead and
make sure we're in the proper directory. We'll do that using the LS, or short for list
command, which is available on Mac and Linux, and there you have it, we see our Python
file. To run it, we're going to use the Python three command with a space, and then the name
of our file 01_03.py. Great, we were able to get the output of our file here on the command
line. Most of the time, you'll run your code directly in the IDE. Though, you may have
occasions where you want to run some Python code from the command line, and now, you
know how.

Running Python on the command line on Windows


Selecting transcript lines in this section will navigate to timestamp in the video

- [Instructor] One way to execute or run your Python code is by using the command line. I'll
be referring to it as the terminal or command line throughout this course. You can access it
on your Windows machine by going to the Start menu and then navigating to the Python
folder. When you get to the dropdown, choose Python 64 bit. This is going to open up the
Python shell. The Python shell will display the prompt, which lets us know that the interpreter
is ready for our Python instructions. Unlike the Mac, you don't need to type anything else to
start sending Python commands. We'll start out by trying to get the sum of two plus
two. Let's see what happens when we type what is two plus two? And hit Enter. Well, we get a
syntax error from the Python interpreter. We call that syntax refers to the rules of a
programming language. Syntax errors happen when you write code that breaks the expected
rules. In this case, we wrote our code in plain English but the interpreter doesn't understand
English, only Python. Now, let's try that again. But this time, we'll type two space plus two and
then hit Enter. And look, we get back the number four. That's because the expression two
plus two is valid in the Python programming language. But what if we were to just type two
plus and then hit Enter? Well, we get another syntax error because our expression was not
complete. For example, in English, I could say you're doing a great job with the course so
far. That's a grammatically correct sentence but if I were to say you're doing a great job
with, that would be an incomplete sentence. I didn't finish it up. Programming languages
work much the same way. They need you to follow the syntax or rules in order to understand
your commands. For now, let's exit the Python prompt. We can do this by typing exit, open
parentheses, close parentheses and then hitting Enter. Remember our 01_03.py file? Let's see
if we can run the code contained in it. First, we'll need to open up the command prompt. To
do that, we'll come down to search and we'll type Command and we'll choose Prompt. Next,
we need to make sure we're in the same directory as our file. My file is located on my
desktop inside of the exercise files folder. Yours might be in your downloads or another
location. The first thing we're going to do is to type cd or the change directory command and
then we need to find the path of our file. I'm going to open up the File Explorer and then I'll
navigate to where our file is located. First, I said it's on the Desktop. I'll choose the
Programming Foundations folder. Then Exercise Files, and I want the file that's inside of
chapter 01. So I'm going to drag this folder over to my command prompt. Now, when we
close down the File Explorer, notice that the entire file path has been copied for us. It's pretty
sweet. Next, we're going to type enter. We're now inside of the chapter 01 folder. I'll then
type the dir command, which stands for directory to get a list of all the files that are
contained in this directory and I see my 01_03.py file. In order to execute it, I'm going to type
python 01_03.py and then hit Enter and we get our Hello world! Throughout this course, I'll
be using a Mac computer, so I'll often refer to Python 3 instead but for Windows
users, typing strictly Python will work just fine. Although most of the programs that you write
in your IDE, you can run directly from there, you may have occasions where you want to run
some Python code from the command line and now you know how.

Running Python on the command line


Selecting transcript lines in this section will navigate to timestamp in the video

- One way to execute, or run your Python code, is by using the command line. Let's open up
Terminal, and try out a few commands. Let's go ahead and launch the Python
interpreter. We'll use the Python three command. Now we're inside of our Python
prompt. This means the computer's ready to take our instructions. Let's try out a simple math
problem. Two plus two. First we'll try it with what is two plus two? Uh oh, we get a syntax
error. Recall that syntax refers to the rules of a programming language. Syntax errors happen
when you write code that breaks the expected rules. In this case, we wrote our code in plain
English. But the interpreter doesn't understand English. Only Python. Let's try this again. This
time we'll type two plus two. And just like that, we get four. That's because two plus two is a
valid Python expression. Let's try another one. Two plus, and then enter. Once again, another
syntax error. That's because this is not a complete Python expression. For example, in English
I could say, you're doing a great job with the course so far. That's a grammatically correct
sentence. But if I were to say, you're doing a great job with. That would be an incomplete
sentence. I didn't finish it up. Programming languages work much the same way. They need
you to follow the syntax, or rules, in order to understand your commands. Let me show you
one more thing. First, we're going to exit our Python prompt. We'll do that by typing exit. Or
we could also use this shortcut, control d. So now we're outside of the Python prompt. Do
you remember our zero one zero three file? Let me show you how we can run it from the
command line. The first thing we need to do is make sure we're in the same directory as our
file. To do that, we'll use the cd, or change directory command, and we'll go to our file which
is located inside of desktop slash exercise files slash chapter zero one. Now if we hit enter,
let's go ahead and make sure we're in the proper directory. We'll do that using the ls, or short
for list command, which is available on Mac and Linux. And there you have it. We see our
Python file. To run it, we're going to use the Python three command with a space, and then
the name of our file. Zero one zero three dot py. Great. We were able to get the output of
our file here on the command line. Most of time, you'll run your code directly in the
IDE. Though you may have occasions where you want to run some Python code from the
command line. And now, you know how.

Installing Visual Studio Code on a Mac


Selecting transcript lines in this section will navigate to timestamp in the video

- We'll be using Visual Studio Code, or VS Code for short, as our IDE for this course. VS Code
is a great cross-platform editor, as it's fast, easy to customize, and has a friendly user
interface. To get started, we'll open up the VS Code webpage. I'll be showing you the setup
for the Mac operating system in this video. If you're using a Windows machine, please skip to
the next video. So the first thing we're going to do is come to code.visualstudio.com. And
then we're going to choose the Download for Mac button. Once it's finished
downloading, we can double click the zip file to open it up. And in order to use Visual Studio
Code, we're going to copy our file over to the applications folder. Now we can go ahead and
open up Visual Studio Code. Let's close down the browser. The first thing you see when you
come to Visual Studio Code is the welcome screen. From here, you can create a new file,
open up a folder, or customize it with extensions. In order to understand a bit more about
this particular IDE, we're going to go to the Learn section and click on the interface
overview. From here, we can see the various options that are available to us inside VS
Code. First, we can explore the various files, search across files, or we can even use things like
source code management. Further, we can do launching and debugging as well as manage
extensions. For now, this is good enough to get you started. So we're going to hit Exit to
leave this overview. Great. In order to work with Python inside of Visual Studio Code, we need
to use a Python extension. So the first thing we're going to do is go to Tools and
Languages. And then we're going to choose Python. It's the very first option. If we click on
this extension, we get tons of information about the various features that it supports. It has
things like linting, debugging, or code navigation. Don't worry if you don't understand
everything about this right now. It'll become more and more familiar as you continue on your
programming journey. For now, let's go ahead and install it by clicking on the Install
button. And then once that's complete, we can go ahead and restart Visual Studio Code. So
we're going to quit and then open it up again. Now, before we can officially get
started working with Python, we need to choose an interpreter. Remember Python is an
interpreted language. That means it goes through your code line by line in order to give you
some results. To install an interpreter, we can open up the command palette. You can access
the command palette using Command Shift P, and then you want to choose Python, Select
Interpreter. From here, you have various options available to you. We want to choose the
Python three option as this is the very latest version that we've already installed. So I'll just
choose the very last one. And that's it. We now have everything we need in order to write and
run our code inside of an IDE.

Installing Visual Studio Code on Windows


Selecting transcript lines in this section will navigate to timestamp in the video
- To get started, we'll open up the VS Code webpage. Once you're here, you want to choose
download for Windows. Once it's downloaded, let's click on the executable in order to launch
the installation flow. Now that we've launched the installation flow, let me go ahead and
close this browser Window and then we'll walk through the flow. First, we're going to click on
next and make sure you accept the agreement. We'll click next again, choose the default
location for installation. We'll keep everything the same for the defaults, and when it comes
to selecting the additional task, I recommend you choose every option, as this will make it
easier for you to work with the exercise files. Let's click next again, and then finally,
install. Okay, now it's finished. Let's go ahead and launch Visual Studio Code. Now that the
app is launched, we're greeted with the welcome screen. From here, we can create a new
file, open a folder, or add a workspace folder. For now, let's take a quick look at some of the
features of this IDE. We can do that by coming to the learn pane and choosing interface
overview. The key things that you'll want to work with for this course are the file
explorer where you can see all of the files listed, searching across files in case you need to
find one with a specific name, managing extensions, which we're going to talk about
shortly, and then also finding and running all commands. Let's escape it by just clicking on
the overlay. The next thing we want to do to get this ready to work with Python is to go to
the customize pane and choose tools and languages. This is going to open up the extension
pane on the left side, and one of the options is Python. If we click on the Python
extension, we're going to get lots of details about how Python works in Visual Studio
Code. What's nice about VS Code is that for each extension, you have a detail screen which
tells you about the different features that are included. For instance, in the Python
extension, we get linting, debugging, code formatting, and much more. But you don't have to
worry about all of that for this course. We're only going to use very limited features. So make
sure that you install the extension if it's not already available. Remember, Python is an
interpreted language, meaning it processes your source code each time its run, line by
line. So in order to run Python code and get code suggestions, you must tell VS Code which
interpreter to use. Let's do that now. We're going to close down the extension and then we're
going to open up the command palette. To do that, we're going to type Control + Shift +
P, and then start typing Python, select interpreter, and choose it from the dropdown. It's
going to give us the option for Python 3.7.3, which is the one that we had installed
previously. So now we can just select it and we're ready to go. That's it, we are ready to start
writing and running our code in an IDE.

Running Python in an IDE


Selecting transcript lines in this section will navigate to timestamp in the video

- Let's take a look at a few ways we can run our Python programs in VS Code. Let's go ahead
and open up our Exercise Files folder. To do that, we're going to click on Open Folder, and
then we're going to choose Exercise Files. Now the first thing you'll notice over here on the
explorer pane is that we have a list of all of the folders inside of our Exercise Files
directory, but there's this one folder that we didn't create, .vscode. You don't have to worry
about changing anything here for the course. This is just a special folder created by VS
Code in order to store some metadata. Let's go ahead and open up one of the files. We're
going to open the one inside of chapter 02, and we're going to choose 02_07. This looks
really different from what we've seen before in our simple text editor. The first thing you
notice is the use of line numbers over here on the left side. We call this the gutter. Using line
numbers inside of IDEs makes it easier for us to find codes later on. For example, on line
number three, we have the print statement, goodbye world. What's another thing you notice
that's really different? It's the use of color. We call this syntax highlighting. This is how we can
point out special key words or various objects inside of our programs. For example, the print
key word is yellow, whereas the hello world is orange, but how did VS Code know to choose
the colors for the Python programming language? Well it's based on the fact that we're
using the .py file extension. This is the file extension used for Python code. Now let's say we
want to run this code. We can click anywhere inside of our file, right-click, and go to Run
Python File In Terminal. Let's do that now. Notice that we see our output. Hello world,
goodbye world. We've done this using our IDE. Now what if we were interested in running all
of the code in a file? Well we could even use just one portion. For example, let's run line
number three only. We'll come to this line, right-click, and we'll choose Run Selection or Line
in Python Terminal, and now we only get goodbye world. What's really nice is that
this actually watches a real Python shell for us. So if we want, we could even run other Python
commands. For example, four star four. This is how we do multiplication in Python. When we
hit enter, we get 16. Now let's go ahead and exit this the same way we would our normal
terminal from the command line. We're going to type exit, and we're no longer inside of our
Python prompt, and to leave this entire area, we can just click the close button that's in the
right corner. Great. And that's it. We've seen a few different ways that we can run our Python
programs inside of VS Code, and it's nice that we have such helpful features like the line
numbers or the syntax highlighting. I encourage you to explore running some simple
commands on your own so that you can get the hang of it.

Basic statements and expressions


Selecting transcript lines in this section will navigate to timestamp in the video

- You could have all of the ingredients to bake a cake, but if you don't have a recipe, then
there's no cake. It's the same way with statements. Statements are the building blocks of any
program and are the individual actions that you want your program to take. Without
statements, there's no program. Each statement can be made up of keywords, expressions,
and operators. We'll talk more about keywords later. For now, we'll focus on expressions and
operators. Operators are symbols that tell the computer to perform an action with some
input, and the good news is, you're already familiar with many of the operators that are used
in programming from your primary school education. In this table, you can see a partial
listing of some operators that should be familiar to you. The plus symbol used for
addition, minus for subtraction, an asterisk or a star to represent multiplication, and finally, a
forward slash for division. These are known as arithmetic operators, as they take numbers for
their input and perform an arithmetic operation. Flash back to primary school. When we have
three plus two, the plus sign is the operator and three and two are the input, also known as
operands. In programming, the combination of operators and operands that break down to a
single value are called expressions. Let's look at a few examples. If we take the following, 10
plus two, what does that equal? 12, correct. 10 plus two is our expression and the 12 is what
our expression evaluated to or equaled. Let's try another one. 10 divided by two plus
three. What does this expression evaluate to? Yes, it's eight. Now let's look at another
expression where the answer may surprise you. Two plus three times six. What do you think
this expression evaluates to? It's 20. Now if you thought 30, that's understandable. But in
programming, the default order of operations is the same as in mathematics. The plus or
addition operator in our example is evaluated after the asterisk or multiplication
operator. That's why we get 20 instead of 30. If you're from the United States like me, you
may have been taught the phrase, please excuse my dear aunt Sally as a way of remembering
the order of operations or PEMDAS. That stands for P, parentheses first, E, exponents, MD,
multiplication and division from left to right, and finally, AS, addition and subtraction, left to
right. It's important to keep the order of operations in mind as your program could behave
unexpectedly based on how an expression is evaluated. If we go back to our last
expression, it evaluated to 20 because of the order of operations. But what if we, as the
programmer, wanted it to process the addition first? In that case, we would add in the
parentheses around our addition operation and then we would get the result that we wanted,
30. Computer programs are made up of statements, statements that can perform a
mathematical calculation, display something to the screen, or even make a choice between
two code paths. As we continue to explore the fundamentals of programming, you'll see just
how powerful this concept really is.

Troubleshooting issues
Selecting transcript lines in this section will navigate to timestamp in the video

- One of my favorite quotes about programing is from Alan Perlis, who said, "There are two
ways to write error free programs, "only the third one works." This play on words is meant to
highlight the fact that programs tend to have bugs, and you're not doomed as a
programmer if you run into issues when writing and running your code. But when you're first
getting started, what can you do to figure out why your code is acting up? Well, first, you
need to understand what type of bug or error you're dealing with. Errors tend to fall into
three categories. Syntax, you somehow broke the rules of the language. Runtime, the
computer was unable to execute a portion of your code. And finally semantic, the output of
the program is not what you expected. Let's look at an example of each type of error. We'll
go to the command line and start our Python Shell. We'll start out by typing print, and then
hello world. And finally enter. Uh-oh, we didn't get our familiar hello world like we
expected. This is because the quotes were missing, the Python interpreter didn't
understand what we wanted it to do. When you come across these types of errors, it's best to
slowly review each line of code to see where you have missed providing a set of
quotes, parentheses or a colon. The next type of error is a runtime error. This happens when
your program is actually running. Let's look at an example. We're going to create a new
statement, this is going to use the expression 10 times two divided by zero. Now let's hit
enter, and we receive a zero division error. But why? This is because division by zero was
attempted. From mathematics, we know that division by zero is undefined. The Python
interpreter has no idea what to do with that expression, so it just fails. When you come across
runtime errors that you don't understand, the best thing to do is look it up. My preferred
website for doing so is stackoverflow.com. You can copy and paste the error message and
then look for similar questions to what you're experiencing. When you click on the
question, you'll often find a suitable answer. Now, the final type of error is a semantic
one. This is when the output of the program is not what you expected. Let's take a look at an
example. We're going to print out hello Alice. We'll start by creating a variable called
name, and we'll set it equal to Alice. And hit enter. Next, we're going to use our print
statement, and we're going to include hello and then name, and enter. Oh no, look what
happened, we have hello name instead of hello Alice. That's because we didn't use the proper
variation of the print method. Python didn't understand that we wanted it to use the name
variable. Semantic errors are the most challenging to troubleshoot, it's best to make tiny
changes and then run your code. That makes it easier to figure out where you introduced the
error. Also, I find going for a short walk helps me to clear my head and come back to the
problem with fresh ideas. Ultimately, you need to have a good sense of humor. Sometimes
these issues pop up from the smallest of mistakes, just laugh it off and keep moving
forward. It's all part of the process.

Question 1 of 20

You have Python 3 installed. How will you run the Python code in the file "process.py"
on the command-line prompt?


python3 exec process.py

 python3 process.py

Correct


run python3 process

process.py > python3
Question 2 of 20

To get Python code suggestions, you need to tell VS Code which Python interpreter
to use.


FALSE

 TRUE

Correct
Question 3 of 20

Which statement is true regarding Python support in VS Code?

 VS Code support for Python is planned for the near future.

Incorrect


VS Code comes fully ready to support Python.

 Only a specific VS Code version supports Python.

Incorrect

 You need to install a VS Code extension to support Python.

Correct

Question 4 of 20

What does this expression evaluate to?

2*3+2*5

 16

Correct


26

40
Question 5 of 20

Which expression evaluates to 20?


4 * (4 + 4)

5+5*2

11 - 1 * 2

 (2 + 3) * 4
Correct

Question 6 of 20

Which is an example of a syntax error?

 2 (4*2)

Correct


2 + (4/0)

2 + (4*2)
Question 7 of 20

Which of these is NOT a reason why we chose Python as the language for the course?

 highly performant

Correct

 beginner-friendly

Incorrect


relevant

popular
Question 8 of 20

Which concern could be considered a disadvantage of Python under certain


circumstances?


Python has a complex syntax.

Python is not very popular.

 Python cannot build web apps.

Incorrect
 Python is a general-purpose language.

Correct

Question 9 of 20

Why does VS Code need a Python interpreter to be specified?

 to load a Python file

Incorrect

 to install extensions

Incorrect

Extensions in VS Code can be installed regardless of an interpreter specified.

 to add Windows Explorer actions

Incorrect


to provide code suggestions

Replay

Review this video

Installing Visual Studio Code on Windows

3m 40s

Question 10 of 20

When working with the Terminal inside of VSCode, how can you expand the pane?


Click the trash can.

Click the plus button.

 Click the up arrow.

Correct

Question 11 of 20

In the Python Terminal, what is printed when running the following line?

5*2+1

 11

Correct


521

 5*2+1

Incorrect


53
Question 12 of 20

What does this expression evaluate to?

3+2*5

 13

Correct


20

25
Question 13 of 20

The code below, which is supposed to print the value of the variable x, is not working.
Which category does the error fall under?

x = 5 print("x")
 syntax

Incorrect


runtime

stack

 semantic

Correct

Question 14 of 20

Which statement is true regarding Python on a Mac?


It is not possible to upgrade the Python version on a Mac.

 Python is automatically upgraded on a Mac every time the terminal is started.

Incorrect

 There is only one version of Python supported on a Mac.

Incorrect

Macs can actually run different versions on Python.

 A Mac comes with a version of Python already installed.

Correct

Question 15 of 20

Which is an example of a runtime error?


name = "It's me!" print("Hello.", name)

 name = "It's me!" print("Hello.", Name)

Correct
 print("Hello.", "It's me!)

Incorrect

Question 16 of 20

Typing “What is 2 plus 2?” into the Python prompt causes a _____.

 segmentation fault

Incorrect

 computer meltdown

Incorrect


syntax error

Replay

Review this video

Running Python on the command line

3m 8s

Question 17 of 20

Which Mac application allows you to run commands and execute scripts in a
command-line interface?

 Terminal

Correct


Finder

Operating System

Browser
Question 18 of 20

Which command will start the Python prompt on your computer?

 start_python

Incorrect

 open_python

Incorrect


python3

 pypy

Incorrect

Replay

Review this video

Installing Python on Windows

1m 49s

Question 19 of 20

Which command exits from the Python command-line prompt?


stop

break()
 exit()

Correct


terminate
Question 20 of 20

If you execute the following expression, what is the output?

>>> 3 + 5 * 2


10

 13

Correct


16

Introduction to variables and data types


Selecting transcript lines in this section will navigate to timestamp in the video

- Writing code typically involves the handling of information. If we're making a game, we


need to store the user's score. If we're creating a banking application, we'll need to have their
current balance and so forth. We've already seen a few variables in the course so far, but now
we're going to go into more detail about what they are and how they work. A variable is
simply a container for a value. When we run our programs, the computer gives us space in its
memory where we can put data that we want to use as a reference for later. This data is
called a variable. Much like how we label jars in our kitchen so we know where to go for our
favorite cookies. We declare variables in Python by giving them a name and then setting their
value. This is called assigning the value. And this assignment is done with the assignment
operator, which is the equals sign. As we see here, age is the name of our variable, and
because of the equals sign, we are assigning it the value of 36. That means that somewhere in
the computer's memory, it's made space for our variable and stored our value in it. Later,
when we ask the computer to print what's inside the age variable, it prints out 36, the value
that we assigned previously. Although we're using an equals sign, try not to think of it the
same way you did in high school algebra. In programming, the equals sign means I want to
take the value on the right and store it in the variable on the left. It doesn't mean the two
sides are equal. That's important to keep in mind. Okay, let's look at another example. This
time, we've created a variable named email address, and we've assigned it to the following
value. [email protected]. If we run this program, we can see that the values of our
variables are printed out. Now even though we've created both of these variables by giving
them a name, there's something different about the value that we assigned them. Can you
spot the difference? Yes, it's that with the first value, we are only using numbers, and for the
second value, we have double quotes with letters and symbols inside. This is because we
want the variables to represent different data types. A data type allows us to put our
variable in a particular category so that the computer knows how much space to give us in its
memory. And it generally knows how we plan to use our variable later on in the program. In
this example, the 36 is an integer, like what we're already familiar with from math class. In
case you've forgotten, an integer is a whole number, no decimal places, and the letters and
symbols inside of the double quotes is called a string because it's made up of a string of
characters. In fact, you can check the type of any value or variable in Python by using the type
function. Let's take a look. We'll print out the type of the age and then the email address
variables. And look, the age is in INT, or short for integer. And the email address is STR, short
for string. There are many other types of values available in Python, as well. We'll get a
chance to explore more of them soon.

Variables across languages


Selecting transcript lines in this section will navigate to timestamp in the video

- Depending on the programming language that you're working with, variables can behave in


different ways. Some languages require that you define your variables and their types before
you can use them, like Java, C#, and C++. For example, if we want to store the name of a
cookie in Java, we would have to first declare that it's going to be of type string and then give
our variable a name, cookie. When we run this code, we get sugar as our output. Since we've
defined our variable as type string, we can't then later decide to set this variable to a different
type. Let's say an integer. When we compile our program, we'll receive an error because Java
is a strict programming language. It doesn't allow you to reassign data types in this way. But
if we were to compare this to a more relaxed language like Python, we don't need to declare
a variable's type before using it. Here, we declare the variable and use it at the same
time. Notice how we only provide the name, not the type. But when we run this code, the
Python interpreter was able to figure out the type based on the value that we
provided. What's more? If we wanted to provide a value of a different type, Python has no
issues with that, either. It just figures it out and keeps on moving. There are pros and cons to
both styles. The more relaxed languages tend to be more flexible and result in less
code, whereas with more strict languages, you have fewer surprises when the code is actually
run. If you created an account balance variable as an integer, you don't have to worry about it
later being set to fill, for example. Like all things in programming, there are a few rules that
we have to follow when working with variables in Python. First, variable names should contain
only letters, numbers, and underscores. And even though numbers are allowed, the name
shouldn't start with a number. 3blindmice would not be a valid name, for instance. Second,
spaces are not allowed in the name. Account space balance would be invalid, but
account_balance would be just fine. Third, it's important to know that names are case
sensitive. Capital C Cookies is not the same as lowercase c cookies. Python considers them to
be two different locations in memory. And finally, variables cannot be keywords. These are
reserved words that mean something special to the Python interpreter, like and, break, or
try. We'll learn more about keywords shortly. However, if you're ever curious about the
complete listing of keywords in the Python language, you can run the following code
snippet to get a listing of all of the keywords. In general, you want to use short, descriptive
names that make your code easy to understand. Naming things takes practice. In fact, it's
often cited as one of the hardest problems in computer science. Keep that in mind and be
patient with yourself as you get used to working with variables.

Working with numbers


Selecting transcript lines in this section will navigate to timestamp in the video

And before you start to have flashbacks to high school algebra, I'm not talking about
complex mathematical calculations. We use numbers numbers to store a person's high score
in a game, or even the number of likes on a picture. Python treats numbers in a few different
ways depending on how they're being used and the difference is largely based on whether
it's a whole or fractional one. Let's look at a few examples. We can use the command line for
this as we just want to do some basic math. So let's go to the command line and start our
Python prompt. We'll start out easy, two plus three. Let's try another one. Two, asterisk and
then three. When we hit Enter, we get six. An asterisk or star is used for multiplication in
Python. Let's go to another one. This is used for exponents. It's exactly the same as two times
two, times two. Let's try another one. This is how we do division inside of Python. Now, what
if we didn't want to have a remainder as a result of our division. Then, we could take
advantage of a separate operator called the floor division operator. Let's see that one
now. We'll use a three with a space and two forward slashes and then our two. This time, we
only get one as a result. And the converse is also available to you. If you only wanted the
remainder, For that one, we're going to do five, the percent sign This is going to give us two
because three only goes into five one time and has a remainder of two. like 45,000, how does
Python handle that? Let's take a look. We'll type 45,000 and then we're going to divide it by
five. Not what we were expecting. That's because the use of the comma really confused the
Python interpreter. So no matter how large the number is, we use no commas when we're
working with Python. So let's try that again, this time, we're going to remove the use of the
comma. 45,000 divided by five and there you have it, we get the answer that we were looking
for. Now, here's a table of the most common operators that you'll work with. So far, we've
only been using whole numbers or integers to do our calculations but in the real
world, fractional numbers are a daily part of our lives. would you be okay if we just drop the
75 cents and only paid you $24? Of course not. That extra 75 cents has value to you. In some
programming languages like JavaScript, There's no distinction between numbers with and
without decimal places. and without decimal places. Whereas in most programming
languages, Whereas in most programming languages, numbers with decimal places are
considered special numbers with decimal places are considered special and they're typically
called floats. and they're typically called floats. A float is any number with a decimal point. A
float is any number with a decimal point. It's short for a floating-point number. It's short for a
floating-point number. It's used for when more precision is required like in the case of our
hourly rate. Let's look at how Python works with floats. First, we're going to try 0.2 plus
0.3. First, we're going to try 0.2 plus 0.3. And when we hit Enter, we get 0.5. We use two floats
and we get back a float in our answer. We use two floats and we get back a float in our
answer. Let's try another one. 0.2 divided by 0.3. 0.2 divided by 0.3. Whoa, it's a huge
number. That's because Python tries to be as precise as possible. That's because Python tries
to be as precise as possible. You may notice lots of extra decimal places when working with
floats. Just ignore them for now. Just ignore them for now. Later, we'll learn how to tweak it
to our own liking. Later, we'll learn how to tweak it to our own liking.

Working with strings


Selecting transcript lines in this section will navigate to timestamp in the video

- Let's talk about strings. In programming, whenever we have to deal with text, be it an email
address, someone's name, or even an emoji, we use strings. The name comes from the
fact that it's a string of characters one after the other. The characters can be letters,
numbers, symbols, and even spaces. They're typically represented by beginning and ending
quotes. Strings can be passed directly to functions or they can be stored in variables. For
example, we can use the print function in Python to display a message on the screen, like, "Hi,
there!" Notice that we have the opening and closing double quotes. These let Python know it
should print out the characters in between the quotes just as we've typed them. We could
also define a variable which contains the same string and we'd get the same result. In fact,
with Python, we could use single quotes to create a string as well. But you might be
wondering why would I want to use the double quotes version over the single quotes version
of strings? Well, it depends on the contents of your string. Let's see what happens when we
print out the following string that's wrapped in double quotes. "She said," double quotes,
"Unbelievable," double quote, "to him." We receive a syntax error because we've confused
Python with our nested double quotes. Instead, we will want to use single quotes to open
and close our string. This way, Python knows just what we mean. And the inverse is also
true. If we want to use single quotes, or an apostrophe inside of our string, like, "Let's get
started," then we would use double quotes at the beginning and end. But keep in mind you
don't want to mix single and double quotes together. Something like this print
statement where we have an opening double quote and a closing single quote will also lead
to a syntax error. When you're first starting out with programming, I recommend using the
double quotes version of strings as your go to. For this course, I'll be using double quotes for
strings, as it's the most common version that you'll see across languages. Now that you've
got a handle on strings, let's put 'em to work in our programs.

Properly using whitespace


Selecting transcript lines in this section will navigate to timestamp in the video

- Python doesn't care about extra empty lines in your programs, just like most languages. In
fact, it's common for developers to add extra empty lines, also known as white space, You
can think of it kind of like paragraphs in a book. Let's take a look at the 03_05_begin
file. Things are really squished together and we even have a new function thrown in. You
might find it especially challenging to figure out what this code is going to do, and that's
totally understandable. Let's add a few blank lines so that it can be easier to read. We'll put a
little bit of space here, some here after line number five. Much easier to read. Although in
movies, programmers are often depicted as just endlessly typing on the keyboard, we
actually spend much more time reading and understanding code than we do writing it. That's
why we add white space. between the different elements of our statements. So right here on
line number two, we're going to add some white space after name and right after this equals
sign. We'll do the same thing again here on line number five. Just adding more white space, it
makes it easier to read. If we save this file and run the code, Let's give it a name. And then
when we hit Enter, it says, "It's nice to meet you, Jill. "Are you enjoying the course?" If we
type, "Yes," then it says, "That's good to hear." Hopefully, you were able to pick up on what
the input function does. It takes some input from the user, and then it stores it in a
variable, just like you see here on line number two, we get some input and then we store it in
the variable name. We do this same thing here on line number five, where we ask the user if
they were enjoying the course. Then we store it in our variable answer. You'll be seeing it
again and again, so now you know what it does. Now let's add a bit more space. This time
we're going to add space here on line number seven, after our answer variable, and once
again, after this equals sign. Let's go ahead and save it and make sure we get the same
output. and choose Run Python File in Terminal. Once again, we can enter a name, hit
Enter, and enter yes for if we're enjoying the course. where white space really matters to
Python, and that's when it relates to its special key words. If you recall, there are over 30 key
words Let's take a look here on line number seven. If is one of those key words. If is one of
those key words. If we delete the space between if and the answer variable, If we delete the
space between if and the answer variable, let's click Save and run this code. So we'll right click
once again So we'll right click once again and choose Run Python File in Terminal. and
choose Run Python File in Terminal. Notice that we get a syntax error. That's because Python
expects us That's because Python expects us to have a space after the if key word. to have a
space after the if key word. So let's go back and add a space once again after if. So let's go
back and add a space once again after if. Don't worry if you don't understand all the ins and
outs Don't worry if you don't understand all the ins and outs of the current code, just keep in
mind that most of the current code, just keep in mind that most of the time, Python is very
laid back about white space. In fact, you're encouraged to add it in wherever it helps to
improve readability. wherever it helps to improve readability. But there are circumstances
where Python takes white space very seriously, and your code will fail takes white space very
seriously, and your code will fail if you don't respect the rules. if you don't respect the
rules. The good thing is that the more you practice, The good thing is that the more you
practice, the more you follow the rules without even thinking. the more you follow the rules
without even thinking.

Working with comments


Selecting transcript lines in this section will navigate to timestamp in the video

- Comments are notes to your future self and others to describe what your code does. Right
now, we have small programs. We can keep the flow of it all in our heads, but over time as
you continue to grow your programming skills, you'll be writing much longer
programs. Some may even be thousands of lines of code spread over dozens of files. It would
be impossible to keep all of that in your head. By adding comments to your code, you can
document the purpose of the different variables, functions, and classes. You can think of
them as virtual sticky notes. Here we are with our program that asks the user for their
name and also asks if they're enjoying the course. Comments are started with hash
marks. Let's add our very first comment here on line number one. We'll type the hash mark
and then a space and type greet the user. Notice how the entire line is now colored
green. This is how the IDE is letting us know that this line is a comment. We can go ahead
and save it. Now there's another way that you can include comments in your code. You can
also add them to the same line as existing code. Let's come down here to line number
four, and right after we have our input function, we'll add a space and another hash
mark, and we'll type ask the user their name, and just like that, we have another comment. So
when Python sees this hash mark, it knows that it can ignore the rest of the line. Let's save
this and see if it has any impact on our output. We'll right-click in the file and go to Run
Python File In Terminal. Just like before, we get our greeting, and it asks us what's our
name. After hitting enter, we're asked if we're enjoying the course. So let's type yes, and we
get the same result that we had before. The comments do not impact the output of our
code. Other than serving as reminders for us, we can also use comments to help temporarily
ignore some code. Programmers use this technique all of the time. It's called commenting
out code. Let's try it. Let's say we didn't want to ask the user how they feel about the course
anymore. Instead of just deleting those lines of code, we can comment them out. Let's close
the terminal and then come to line number seven, and at the very beginning, we'll put a hash
mark and a space. Let's do the same thing for lines nine through 12. Hash mark and a
space. Once again, on line number 11 and then finally on line number 12. Let's save our file
and check the output. We can right-click and go to Run Python File In Terminal. So we get
our greeting, and then it asks us for our name. Let's type Jill, and when we hit enter, that's
it. It doesn't ask us how we feel about the course. That's pretty cool, right? Comments are a
powerful tool for programmers, and as you've seen, we can use them to serve as
reminders, provide explanations for other developers, and even help us with debugging code.

Challenge: What's the output?


Selecting transcript lines in this section will navigate to timestamp in the video

(digital pulsing music) - All right folks, it's time for a challenge. Now that you have an
understanding of variables and data types, let's put your newfound powers to use by figuring
out what the following lines of code will output. I really want you to try and figure out the
output on a sheet of paper. Go line by line just as if you were the Python interpreter, and
write down what you think the output will be. This is going to help you think like a
computer. If you find yourself really struggling you can always type the code into your IDE
and run it. Either way, make sure you watch the solution video where I'll provide a detailed
explanation of why the output is what it is.

Solution: What's the output?


Selecting transcript lines in this section will navigate to timestamp in the video
(upbeat music) - How'd it go? Ready for the answer? Let's work our way through the code
line by line to figure out the output. Starting here on line number one, it's our familiar print
statement. This is just going to print out challenge one. Moving on to line number two, it's a
blank line, so Python just ignores this completely. Coming down to line number three, notice
that it starts with a hash mark, this means that it's a comment. So whenever Python sees a
hash mark, it just ignores the rest of the line. No output here either. Next on line number four
we come to our very first variable, it's named message and the value is the string, this is
going to be tricky. Whenever I come to a variable, I write it down and I keep its current
value. So let's create a box for message, and its value, this is going to be tricky. Great. Next
on line number five we update the message variable with the value, very tricky. Actually is
that what happened here? Did you catch our trick? We created a new variable named capital
m message, so let's write that one down. We'll create a new box and it's capital m
message, and its value is very tricky. On to line number six. We print out the lower case m
message variable. Keep in mind variables in Python are case sensitive so this is only going to
print out our very first message variable, and then next we have a hash mark. Once again this
is an end line comment so Python is just going to ignore this entire section of the
code. Hopefully you're starting to get the hang of how Python ignores white space and
comments, so let's move to the next one, line number seven. This is also a blank line, nothing
to do here. Coming down to line number eight we have another hash mark, this entire line is
a comment, there's no output. Next, on line number nine, we have a new variable and it's
named result, let's create a box for it as well. Perfect. And if we look at the value of result it's
two with two stars and a three. Did you remember that two stars next to each other represent
an exponential operation in Python? If so, that's awesome. So this is two to the third power,
or eight. Let's write it down inside of our box. Then on line number 10 we're going to print
out the value of our result variable, but first we're going to print two to the third power
equals, great. Moving on to line number 11, we're changing the value of our result variable
just to a simple subtraction, five minus three. So now we can update result with the value of
two. Cool. On to line number 12, here we print out the result, or do we? No, we don't actually,
can you spot why? Yes, it's because of the hash mark, whenever Python sees this it's going to
treat this entire line just like a comment and completely ignore it. Moving on to line number
13, it's just another blank line, nothing to see here, and then finally, line number 14, we print
out challenge complete. Were you able to figure out the output on your own? If so, that's
awesome, you're well on your way to mastering the ins and outs of variables and data
types. If not, that's okay too, it takes lots of time and practice for most people to really
understand how these things work. I'll be including reminders and tips as we go along, and
you can always check the exercise files for more practice, but whatever you do, don't give up.

Question 1 of 17

When would a removal of white space be problematic?

 removing an empty line between two lines of code

Incorrect
Empty lines inside the code are only used for better readability and do not
impact the code itself.


removing the space after the "if" in if x==1

 removing the space after the string in print("Hi, ",name)

Incorrect

 removing the spaces around the equal sign in x = 1

Incorrect

Replay

Review this video

Properly using whitespace

4m 22s

Question 2 of 17

Although Python ignores empty lines, one place where whitespace does make a
difference is inside of _____.

 strings

Correct

 functions

Incorrect


files
Question 3 of 17

Which expression evaluates to 2?


5 // 3

 5%3

Correct

 2/4

Incorrect

Question 4 of 17

Python treats all numbers the same, regardless of numerical value.

 FALSE

Correct


TRUE
Question 5 of 17

Which string definition results in an error?

 'It is 10am'

Incorrect


"It's 10am"

 'It's 10am'

Correct

 "It is 10am"

Incorrect
Question 6 of 17

Which of these will not output:

I'm learning a lot!

 print('I'm learning a lot!')

Correct

 message = "I'm learning a lot!" print(message)

Incorrect


print("I'm learning a lot!")
Question 7 of 17

Which character starts a comment in Python?


?

/

 #

Correct


$
Question 8 of 17

What is the output of the following Python code?

first_name = "Jeff" #first_name = "Sara" print(first_name)

 JeffSara

Incorrect


Sara

 Jeff
Correct

Question 9 of 17

What will the following code print?

number = 3**2 Number = 2**3 print(number)

 9

Correct


6

8

This code will error out.
Question 10 of 17

What is the output of this Python code?

first_name = "Jeff" first_Name = "Sara" print(first_name)


Jeff, Sara

Sara

 Jeff

Correct

Question 11 of 17

Which variable name is valid in Python?

 3blindmice

Incorrect

 $money

Incorrect

account_balance

Replay

Review this video

Variables across languages

3m 27s

Question 12 of 17

Which data type in Python represents a number with a decimal point?

 Float

Correct


Frac

Dec

Int
Question 13 of 17

How would you fix the following code to return the remainder of 1,000 divided by
300?

1,000 % 300


1,000 // 300

 1000 % 300

Correct

1000 ** 300

1,000 %% 300
Question 14 of 17

What will the following code print?

variable = "12" print(type(variable))

 int

Incorrect

Since the variable is defined as a string, its type is not going to be int.

 12

Incorrect

 twelve

Incorrect


str

Replay

Review this video

Introduction to variables and data types

3m 37s

Question 15 of 17

What operator do you use to assign a value to a variable?

 plus (+)
Incorrect


equals (=)

 assign (&)

Incorrect

Replay

Review this video

Introduction to variables and data types

3m 37s

Question 16 of 17

Which data type is a whole number?


bool

 int

Correct


str

num
Question 17 of 17

Which variable name is valid in Python?

 max distance
Incorrect

 break

Incorrect

 1st_rule

Incorrect

This variable name is not valid, since it starts with a number.


speed_limit

Making decisions in code


Selecting transcript lines in this section will navigate to timestamp in the video

- Have you ever received a note from someone that read, "Would you like to go out?" And
the options were yes, no, or maybe. As humans, maybe is often a valid response to a
question. It's not quite yes, but not exactly no. However, computers are different. Things are
absolute. There's only yes or no, true or false, one or zero. Any expression that breaks down
to either true or false is called a Conditional or Boolean expression. Yes, that would evaluate
to true. But is it greater than five? No, then it will be false. And finally, here's an example that
doesn't involve numbers. Is my name Frank? Nope, it's Annyce. This would evaluate a false as
well. These are Boolean expressions. But as we've learned, computers don't speak our
language. We can't just ask a questions directly the way we have it here. And that's where
relational operators come into play. We've already worked with several operators, we've used
arithmetic operators, like the plus for addition, or the percent sign for modulo. We've also
used the equal sign for assignments. Relational operators work with two operands, and they
return a value, true or false, based on their relation to each, thus the name. The first relational
operator we'll look at is the equality operator. It's represented by a double equal sign, and it's
used to evaluate the equality of the operands. As an example, four double equals two times
two will be true. The left side of the operator equals the right side of the operator. Here's
another example. We have a variable called name. And we've said it to equal to the string
Annyce. to compare it to the string Frank. The result will be false because these two strings
are not the same. when you mean to test for equality. So be on the lookout for that potential
bug if your program isn't doing what you expect. Let's come over to our python shell, and
we'll do a rapid fire round of conditional checks. Let's start out easy. First, we're going to start
our shell. We got back false, because of course, five is not the same as four. But what If we
wanted to check the fact that they are not the same? Then we would use the not equal
sign. That looks like this. Five with an exclamation mark, no space, When we hit Enter, we get
back true. Let's try another one. Let's check for five being greater than four. that you already
know from mathematics. So we'll put our five, space, the greater than sign, and then the
number four. When we hit Enter, Now if you forget which way to place the angle bracket, just
remember the hungry alligator opens his mouth to the bigger number. Okay, let's look at
another example. then we're checking for five being less than four. So let's do that now. Five,
space, less than sign, and then the number four. And of course, we get back false Are you
starting to get the hang of relational operators now? Let's do a few more. This time, we're
going to create some variables. First, we're going to create a variable named age, and we'll
set it equal to 15. Then, we'll create another variable named age to drive. Now, let's do some
comparisons with these two variables. Now, let's do some comparisons with these two
variables. First, we'll check if age is equal to, First, we'll check if age is equal to, using the
double equal sign, age to drive. using the double equal sign, age to drive. And when we hit
Enter, we get back true. And when we hit Enter, we get back true. Here the Python
interpreter is comparing the values of the two variables, is comparing the values of the two
variables, and yes, the value 15 is equal to the value 15. and yes, the value 15 is equal to the
value 15. There are a few other relational operators available in Python as well. available in
Python as well. Here's a listing of them and how they're used. Here's a listing of them and
how they're used. You can compare if two numbers are greater than or equal to, You can
compare if two numbers are greater than or equal to, less than or equal to each other, and
much more. less than or equal to each other, and much more. I've created a worksheet in the
exercise files I've created a worksheet in the exercise files that will help test your skills with
Boolean expressions that will help test your skills with Boolean expressions and relational
operators. I really encourage you to spend the time going through each item. going through
each item. This will help you to master Boolean expressions.

Exploring conditional code


Selecting transcript lines in this section will navigate to timestamp in the video

- In the real world, we make decisions based on current conditions all of the time. If it's
raining, I'll take my umbrella. If I'm sleepy, I'll grab a cup of coffee and so forth. The most
common way for a computer to make decisions is by means of an if statement. We've seen
an example of an if statement earlier in this course, but let's dive deeper to understand the
ins and outs. An if statement has this general structure. It starts with the word if, that's
where it gets its name from, then it checks some condition, and if that condition is true, then
it will do the work that you've placed inside of it. To visualize this, think of it as a flow
chart with only one check, if it's true, then the program will proceed to execute the
statements. If not, then it just ignores those statements as if they don't exist. Here's a simple
if statement in Python. It starts with the word if, then we have our condition, or boolean
expression, five less than six. Next we have a colon, this is part of the syntax, or rules, of the
Python if statement. And on the next line we have an indented line of code. This contains a
print statement that will run if our condition is true. The indentation in Python is very
important because that's how the interpreter knows which statements belong to the if. In
fact, we have a name for statements that are grouped together this way in programming. It's
called a block. In this example, there's only one statement in our block, but we could easily
extend it like this. And now we have two statements in our block. In Python, the block is
ended as soon as we come to a line that's not indented anymore. This new print statement
isn't indented. It's not part of the previous if statement's block. Let's move over to VS Code to
take a look at an example in the 04_02 exercise file. This code should be familiar to you. This
is our code that we used before that includes an if statement. Let's go ahead and run it and
check out the output. So we'll come down to run Python file in terminal. First, it asks what's
your name? Let's go with Frank. And then, are you enjoying the course? We'll choose
yes. And we can an output, that's good to hear, and then the final statement, but let's see
what happens if we choose something other than yes. We'll close this down and run this code
again. Once again for what's your name, I'll go with Frank. And then are you enjoying the
course? I'm going to type dance. And as our output, we only get the final statement. Did you
see what happened? I only got the final statement because the line of code inside the if was
ignored. That condition answer equals equals yes evaluated to false. The only thing our if
knows how to handle is an answer of yes, and dance certainly doesn't equal yes. Recall what I
mentioned earlier about the importance of proper indentation. Let's see what would happen
if we failed to properly indent our code for the if statement. We'll change that here on line
number nine. So first let's close this down, come on line number nine right in front of our
print statement, and we're going to delete it. Let's save and run our file again. This time we
get an error, an indentation error. That's because Python requires the indentation so that it
knows what code belongs to the if. It's standard in Python to use four spaces for doing some
indenting. If we come back here to our code, we can close this down, we can use the tab
button to add four spaces to our code. When we click tab, the IDE automatically converts this
to spaces for us, which is pretty handy. If statements allow a program to take different paths
while it's running. So far, we've just gone from top to bottom processing every single line
only one path, but that's boring and it doesn't give us much opportunity to interact with the
user. Now, we know how to switch things up based on some condition and we can start to
write much more interesting programs.

Working with simple conditions


Selecting transcript lines in this section will navigate to timestamp in the video

- With if statements, we've discovered a way  to add a bit more flexibility and excitement to
our programs, but so far, if the condition is false, then we just don't do anything. But typically,
we'll want to take some action in this case as well. This is done with the if-else statement. It
looks very similar to our normal if statement, except that it has the else keyword along with a
colon, followed by a code block. In programming, this is called the else clause, because you
never find it without an accompanying if statement. Just like in English, if I said, every
morning before I go to work, and that's it, that would be an incomplete thought. It's just a
clause. I'd have to add something like, every morning before I go to work, I enjoy a cup of
coffee. Now and the thought is complete. Similarly, we don't use the else clause without an if
statement. Let's look at an example. If five is greater than six, then we're going to print five is
greater than six, weird! Otherwise, if it's not true that five is greater than six, then we're going
to print five is not greater than six. When this code is run, first the condition of the if
statement will be checked, and since five is not greater than six, then it'll evaluate to
false. Since the condition is false, it's going to skip over the if's code block and drop down to
the else clause. The result will be the output of the else's print statement. Five is not greater
than six. Let's switch over to VS Code to see a few more examples of the if-else
statement. We are here in our 04_03 exercise file. Starting on line number one, we've created
a variable named plant, and we've set its value equal to Cacti. Then, coming down to line
number three, we have our if statement. Here, our condition checks to see if the value of the
variable plant equals Cacti. If it does, then we're going to execute what we see here on line
number four. That is, we will print the name of the plant and the fact that it doesn't need a lot
of water. But if it's not true that the value of our plant variable equals Cacti, then we're going
to print the name of the plant and the fact that it loves water, which is what we see here on
line number six, but regardless of what the value of our plant variable is, we're going to print
thanks. Let's go ahead and run this to check the output. And there you have it. Cacti don't
need a lot of water. Thanks. Is that what you expected? I hope so. Our variable plant was
assigned the value of Cacti. So when we came to our if statement on line number three, Cacti
does equal Cacti, and so we print it out, Cacti don't need a lot of water. Now let's go ahead
and change our plant variable to something else. Let's change it to Irises for example. Let's
save it and run it again. This time, we get Irises love water. Thanks. That's because Irises does
not equal Cacti, so we skip over the body of the if statement here on line number four, and
instead, we execute what's inside of the else clause, Irises love water, but the important thing
to remember is that regardless of what the plant variable's value is, we always print out
thanks. With if-else statements, we will always execute one of the code blocks depending on
the result of the condition test. This makes it perfect for when we need some action to take
place in our programs.

Conditionals across languages


Selecting transcript lines in this section will navigate to timestamp in the video

- By now you're pretty familiar with conditionals in Python.  We start with an if, followed by
our condition, then we end the condition with a colon. Then on the next line we indent the
block of code that we want executed. And the else is very similar, except there's no condition
to check. In the Java programming language, the if else statement has a slightly different
syntax. Remember, syntax is just the same thing as saying rules for how a programming
language expects its code to be written. After the if keyword, there's a set of opening and
closing parentheses. Inside these parentheses is where you put the condition you want to be
tested. And instead of a colon after the condition, a pair of opening and closing curly braces
are used to denote the code block for the if. The same thing is true for the else clause. Now
although it's customary to indent the statements in the code blocks, it's not a requirement
the way it is for Python. Let's look at one final example with the Ruby programming
language. Ruby is known for its ease of use and flexibility. But just like the other
languages, the structure of the if else statement is the same, but the syntax differs. With Ruby
there are no parentheses or a colon to separate the condition. Even the else clause just
stands on a line all alone. The one thing that's more noticeably different is the addition of the
end keyword. This is how Ruby notes that it's done with this if else statement. Not based on
indentation or a curly brace like Python and Java. The cool thing about learning the basics of
programming is that once you grasp the structure of the core statements and principles, you
can more readily recognize them across all languages.
Challenge: Guessing game
Selecting transcript lines in this section will navigate to timestamp in the video

(upbeat music) - Hi folks, it's time for another challenge. And guess what? We're going to
create a guessing game. Using your knowledge of relational operators, Boolean expressions
and the powerful if else statement, I want you to create program, with the following
requirements. First it asks the user, what's my favorite food? If the user enters the name of
your favorite food, output, Yep! So amazing! If the user doesn't enter the name of your
favorite food, output, Yuck! That's not it! Regardless of what the user enters, output, Thanks
for playing! You may find it helpful to plan your program on a sheet of paper first. Think
about what functions you'll need to call and what variables you'll need to create. This will
make it easier for you when you start to write your code in the IDE. Keep in mind there's no
single one way to do this. Just have fun and try your best. But make sure you watch the
solution video, where I'll share how I tackled the problem.

Solution: Guessing game


Selecting transcript lines in this section will navigate to timestamp in the video

(upbeat music) - Welcome back. I hope you had fun thinking of your favorite food and
writing the code for this game. I know I did, and here's the solution I came up with. First,
starting on line number one, I've created a guess variable. This is where I'm going to store the
result of the user's answer to my question, what's my favorite food? Then coming down to
line number three, I start the if statement. If the user's guess equals cookies, because of
course, cookies is my favorite food, then I'm going to print out yep, so amazing. But if the
user enters something other than cookies, I'll fall down to the else statement, which starts
here on line number five, and I'm going to print what's inside its indented block, which is
yuck, that's not it. And finally, regardless of what guess the user enters, I'm going to print
thanks for playing. Let's go ahead and run it and check out the output. So we'll right click and
we'll come down to run Python file in terminal as usual. So let's see, what's my favorite
food? Let's enter cookies. Yep, so amazing. Thanks for playing. And that's it. Did you come up
with something similar? Maybe you used a different name for your variable, or maybe you
used the not equals operator instead. That's perfectly fine. In programming, there's almost
always more than one way to solve a problem. That's one thing I've always loved about
coding. You get to be creative and come up with unique solutions to challenging
problems. And now, you're more than halfway through the course. You should be proud of
yourself for hanging in there and getting to know the basics of what programming is all
about.

Question 1 of 12

With if-else statements, we will always execute one of the code blocks depending on
the result of the condition test.

 FALSE
Incorrect


TRUE

Replay

Review this video

Working with simple conditions

4m 15s

Question 2 of 12

Which operator will you use to return true if two variables are different?


>=

=

>

 !=

Correct

Question 3 of 12

Which if-else statement will not produce this output?

is divisible by 3

 num = 36 if num % 3 == 0: print("is divisible by 3") else: print("is not divisible by 3")

Incorrect


num = 16 if num % 3 == 0: print("is divisible by 3") else: print("is not divisible by 3")
 num = 15 if num % 3 == 0: print("is divisible by 3") else: print("is not divisible by 3")

Incorrect

Replay

Review this video

Working with simple conditions

4m 15s

Question 4 of 12

Any expression that breaks down to either true or false is called a conditional, or
_____.


boolean expression

 string expression

Incorrect

 truthy expression

Incorrect

Replay

Review this video


Making decisions in code

5m 37s

Question 5 of 12

This if-else statement is written in _____.

if "apples" == "apples" puts "You're comparing the same thing" end

 Java

Incorrect

 Python

Incorrect


Ruby

Replay

Review this video

Conditionals across languages

1m 52s

Question 6 of 12

Which of these is not a relational operator?

 =

Correct


>
 !=

Incorrect

 >=

Incorrect

Question 7 of 12

What is the issue with the following code?

number = input("Enter a number: ") if number == 10: print("The number is greater than 10.") else:
print("The number is less than 10.")


The input statement cannot accept numbers.

 The print statements are not indented correctly.

Incorrect


The if-else syntax is incorrect.

 The conditional statement is checking for equality.

Correct

Question 8 of 12

What is the output of this program?

num = 16 if num % 3 == 0: print(num, "is divisible by 3") print("The end")

 16 is divisible by 3

Incorrect


16 is divisible by 3 The end

 The end

Correct
Question 9 of 12

Multiple statements grouped together are called a _____.

 cluster

Incorrect


block

 grouping

Incorrect

Replay

Review this video

Exploring conditional code

4m 49s

Question 10 of 12

Which code can you use to print if a test score is a pass or a fail?

 if (score>=60) print("passed") else print("failed")

Incorrect


if (score>=60): print("passed") else: print("failed")

 if (score>=60): print("passed") else: print("failed")

Incorrect

While this code flow is correct, it is missing something else that is required.
 if (score>=60): print("passed") else print("failed")

Incorrect

Replay

Review this video

Working with simple conditions

4m 15s

Question 11 of 12

What will the following condition print?

if (num>=0): print(num) else: print(-1 * num)

 the value of num

Incorrect

 the value of num minus 1

Incorrect

There is no subtraction in this code, only multiplication by -1 in a certain


condition.

 an indication if num is negative

Incorrect


the absolute value of num
Replay

Review this video

Working with simple conditions

4m 15s

Question 12 of 12

What is the output of the following program?

number = 10 if number % 4 == 0: print(number, "is divisible by 4") print("All done")

 10 is divisible by 4

Incorrect

 The program will error out.

Incorrect


10 is divisible by 4 All done

 All done

Correct

Introduction to functions
Selecting transcript lines in this section will navigate to timestamp in the video

- Functions make programs easier to read, write, and debug. A function is a block of code
packaged together with a name. They're a core feature of all programming languages, yet
depending on the language, they may be called subroutines, methods, or some other
name, but it's all referring to the same thing, a way to break up your code. In this course, we'll
call them functions. In fact, you've already used a few functions like print to show something
on the screen and input to get a response from the user. There are hundreds of other
functions in Python that we've yet to explore. One key benefit of functions is that they help
us to avoid writing the same lines of code again and again. You can think of it this way. I love
to dance, and if I wanted to teach you my favorite dance move, the shimmy, I'd have to first
walk you through all of the steps, one by one. First, you take one step to the right and
stomp. Then one step to the left and stomp. Then shake those hips. That's it. Now you know
how to do the shimmy. And the next time I say, "Let's shimmy", you know exactly what to
do. I don't have to walk you through the steps each time. It's the same way with
functions. We teach the computer how to do something one time and from then on out, we
just call the function by its name and the work gets done. No need to repeat ourselves. Let's
imagine this is our shimmy code. It lists out all of the steps of doing the shimmy, one by
one. Without functions, we'd have to copy and paste this code again and again each time we
wanted to shimmy. So let's go ahead and copy lines three through five. And then we're going
to paste it down below. One and two. This makes the code so much harder to read and we're
just repeating ourselves again and again. Let's run the program to check our output. We're
going to right click and go down to run Python file in terminal. And we get the shimmy steps,
but look. We have a bug. It's supposed to be right and stomp, not stamp. Now we need to go
back to each place where we have stamp and fix it. Let's close this down. Can you imagine
how difficult this would be if we had a program with hundreds or even thousands of lines of
code? It makes me sad just to think about it. Functions save us from having to do this type of
tedious error-prone work as programmers. Let's go ahead and make a change and we're
going to create a function. We're going to call our function shimmy. Don't worry so much
about the syntax. I'm going to walk you through how to create a function shortly. Just notice
the benefits that we get. So we're going to delete all of this extra copy and paste. Let's fix our
typo here on line number four and then we're going to call shimmy. Let's do it a few more
times. Much easier to read, much cleaner. Let's save our file and then we'll run the
code. Perfect, we get all the steps to the shimmy. Pretty cool, right? I'm sure you're eager to
learn how to take advantage of functions in your own code. Let's get started.

Creating and calling functions


Selecting transcript lines in this section will navigate to timestamp in the video

- To better understand how functions work, let's create one. We're going to work with an
empty file over here in VS Code. Here on line number one, we're going to create our first
function. To do that, we're going to use the define statement. That starts with the def
keyword, which is short for define. This lets Python know that we are ready to create a
function. Next, we need to give our function a unique name. We're going to call this one
say_hello. We have to give our function a unique name, just like variables. We could name it
anything we want, but we have to make sure we respect the rules of the Python
language. For example, I could name this function funky town, but that might confuse me or
the next developer who reads my quote. So we're going to stick with say_hello for now. The
next thing we need to do is add an opening and closing parentheses, and then a colon. This
is the requirement of Python syntax for creating a function, similar to what we had for the if-
else statement. Once we hit Enter, the IDE automatically indents this line of code, because it's
trying to help us out. It knows that functions have to be indented. Now, where have we seen
this before? That's right. We do the same thing for our if statements. We often call the code
block that belongs to a function its body, so when you hear function body, that's simply
another way of referring to the code that the function is going to execute. So let's move back
over to our code. In order to get the hang of this syntax, we're only going to have one line
inside of our function's body, and it's going to be a print statement that says hello,
friends. This is the say_hello function after all. With that done, we can save our file and let's
run it. Uh-oh, we don't get any output. Can you guess why? Think back to when I taught you
the steps to my favorite dance move, the shimmy. How could I get you to dance with me? I'd
have to call out, let's shimmy, and then you'd know it's your time to shine. Similarly, creating
a function means that we taught the computer how to do some work, but in order to have
that work done, we need to call the function first. In Python, you call a function by using its
name, followed by a set of open and close parentheses. Let's do that now. Right underneath
where we have line number two, we're going to hit Enter, and by default, the IDE tries to help
us out and it automatically indents this line of code. But that's not what we want. We're done
with this function. So we'll hit Enter one more time and delete this indentation, so that it's
clear say_hello is complete, and finally, in order to call the function, we're going to type its
name, say_hello and open and close parentheses. Let's save it and run our file again. This
time, we get the hello friends message. Pretty cool. In fact, what if we wanted to say hello a
few more times? We can copy line number four and paste it right underneath on line five and
line number six. We'll save and we'll run it one final time. And there we go. Hello friends, hello
friends, hello friends. Now you know how to define and call your very own functions. While
this may seem basic, soon you'll learn how powerful it is to incorporate functions in your own
programs.

Setting parameters and arguments


Selecting transcript lines in this section will navigate to timestamp in the video

- Growing up, my siblings and I loved to go to the carwash. And depending on how much
money my parents were willing to pay, we could have a different experience each time. For
$6, we would get the most basic wash, white foam, one rinse and a drip dry. But if they were
willing to spend $12, then we could get tricolor foam, two rinses Just like the carwash, in
programming, we often want to provide different outputs to our users based on some
input. Functions allow us to do this, in fact, let's go over to VS Code to see a function that
simulates our carwash experience. We're here in 05_03_begin. We have this function defined
on line number one, wash_car, and then we have our open and close parentheses Inside, we
have our carwash steps. First, we have the steps for the Platinum car wash on lines two
through four. Then, down on lines six through eight, we have the steps for the Basic car
wash. Let's go ahead and call this function using its name. We'll do that down here on line
number 10. Wash_car and then a set of open and close parentheses. and go down to Run
Python File in Terminal. And you probably guessed it, when the amount paid is $12 and the
Basic steps only if the amount paid is $6? How do you think we can do that? I hope the first
thing that came to your mind is an if statement. Let's pretend we have the amount
paid stored in a variable named amount_paid, So let's close this down and then, we'll come
back inside of our function and we'll start right on line number two, we'll add our if
statement. So, if the amount paid is equal to 12, this is for the Platinum wash, Remember, we
have to indent the statements, that all three of these print statements belong to this if. Next,
we'll come down to line number seven and we'll create another if statement, this time it's for
the Basic car wash, if the amount_paid is equal to six, then we're going to do the steps in the
Basic car wash. Once again, make sure you indent the statements that you want to include
inside your if. So now, let's save it and run our file again. And look, we get an error, Python is
letting us know so we need to do something to bring this variable inside of our functions
world. In order to do that, let's close down this window and we're going to come up to where
we've defined our function, inside of our open and close parentheses, then we're going to
include amount_paid, this is how we let our function know Let's save our code now. In
programming, this variable has a special name, a parameter. to change their behavior based
on some input. Let's go ahead and run our code again. But look, we get an error, but what
does that mean? What is a positional argument? Well, similar to how we have parameters as
the name for the variables we use in our function definition, we have arguments as the name
we use for the values that we give to our functions. And we give our functions arguments by
placing a value between the open and close parentheses by placing a value between the
open and close parentheses in the function call. in the function call. Let's do that now. Let's
do that now. We're going to close down this pane and then here, on line number 12, and
then here, on line number 12, where we call our function, let's give it the number six where
we call our function, let's give it the number six in between the open and close
parentheses, in between the open and close parentheses, this is going to serve as our
argument. After we save our file and run it, let's notice the output. And indeed, we get all the
steps for the Basic car wash. And indeed, we get all the steps for the Basic car wash. On your
own, provide 12 as the argument to the function On your own, provide 12 as the argument
to the function and enjoy the Platinum wash experience. and enjoy the Platinum wash
experience. One thing to note is that sometimes parameters and arguments One thing to
note is that sometimes parameters and arguments are used interchangeably, you'll have to
rely are used interchangeably, you'll have to rely on the context to figure out the
meaning, but don't despair, the key thing to know but don't despair, the key thing to know is
that functions have the ability to change their behavior is that functions have the ability to
change their behavior based on their parameters and arguments. based on their parameters
and arguments.

Returning values from functions


Selecting transcript lines in this section will navigate to timestamp in the video

- Growing up, your mom may have asked you to go get the mail so you leave, get the
mail and then you bring it back to her. Other times, she may have asked you to wash the
dishes so you go and wash the dishes, there's nothing to bring back. That's kind of like the
functions we've been creating so far. They do some work and then that's it. But when you
need something back, just like how your mom expected the mail, you can use functions that
return a value. Let's move over to VS Code to see it in action. Here, on line number one we've
created a function and it's called, withdraw money. It takes in, two parameters. The first one is
current balance. This stores the current amount of money that you have in your account. The
second parameter is named amount. This is the amount that you want to withdraw. Then, on
line number two, we have an if statement. The if statement checks to make sure you have
enough money in your account to cover the amount that you want withdrawn. If you do, then
we come to line number three where we update the current balance with some simple
subtraction. We take the current balance and we subtract the amounts. And then finally, on
line number four, we print out what the current balance is using this variation of the print
function. Here on line number six, we make our call to the withdraw money function. We pass
in a 100 for the current balance and 80 for the amount. Let's run the function to check the
output. And as you expected, the balance is 20. Now, let's say after we withdraw the
money we want to check the new balance later in our program to see if we need to make a
deposit. This is where returning a value from our function comes in handy. In Python, when
creating a function using the def statement, you can specify what the return value should
be with a return statement. A return statement consists of the return keyword and the value
that the function should return. Remember, Python has over 30 keywords that it uses to take
special actions. Return is one of those keywords. We need to make a few changes in order to
take advantage of the return keyword. The first thing we're going to do is on line number
three, we'll hit Enter and then we're going to return the current balance. We're going to use
the return keyword in order to do that so we'll just type return and then current underscore
balance. The next change we need to make is to copy this print statement and remove it
outside of our function. Since we're going to use it later, let's not completely delete it. We'll
just cut and remove that extra line. Okay, great, we're almost done. Coming down to line
number six, since we want to use the value for later, we need to store it inside of a
variable. To do that, we'll go to the beginning of the line and we're going to create a variable
that we call, balance. And we'll use the equal sign in order to assign it the value from our
function. The final thing we want to do is to add an if-else to check and see if we need to
make a deposit. So, underneath this function call, let's create our if statement. We're going to
check if the balance is less than 50. Then we're going to say, we need to make a
deposit. Else, we're going to print out, nothing to see here and that's it. Let's save our file and
then run this code. Perfect, we get a message, we need to make a deposit. That's because our
balance variable now has the value of 20 so we fall inside of our if statement and it's letting
us know that we need to make a deposit. Being able to return values from functions is a
cornerstone of programming. I can't wait to see what you're able to build with this new
power.

Functions across languages


Selecting transcript lines in this section will navigate to timestamp in the video

- We've seen functions that fall into four main categories. Those that have no parameters and
no return value, no parameters but do return a value, those that have parameters and don't
return a value, and finally, those that have both parameters and a return value. But so far,
we've only seen these functions in Python. Let's take a look at how a few other
languages handle functions. First up is Java. This is the no parameters and no return value
example of a function. Does it feel slightly familiar? Yes, the curly braces are used to mark off
the function's body, just like how we saw curly braces used for the code block of an if
statement. Also, we have the function named followed by empty parentheses, just like we do
in Python, but what's totally different here? It's the void keyword in front of our function
name. Can you guess what that void means? It means that our function doesn't return a
value, similar to how we can't get any money back from a voided check. It has no
value. Unlike Python, Java requires that you always specify explicitly what the return type is of
a function, even when that function isn't going to return a value. Okay, onto the next
example, Kotlin. This is the has parameters and no return value example of a function. Let's
look for similarities and differences from what we've seen so far. Just like Java, we use curly
braces to mark off the function's body, and similar to Python, we have a keyword, fun, which
is used to signal that we've creating a function. However, unlike Python, we have to specify
what the type of our parameter is when we define the function. We do that by placing a
colon after the parameter's name, and then the type. In this case, it's a string. As you explore
now programming languages, I encourage you to compare languages, as it makes it easier to
learn. Do you feel like you can spot a function now? Let's put your skills to the test in a rapid-
fire round of Find That Function. Where's the function here? Yep, you got it. This is the Ruby
programming language, and this is how functions are defined. Okay, onto the next one. Can
you spot the function? Yes, that's right. This is the C++ programming language, and this is
the syntax it uses for functions. One more. Can you find the function? Nice work. This is some
JavaScript code, and indeed, this is how you define functions in JavaScript. Okay, thanks for
playing along. Even if you didn't understand exactly how each function worked, you were
able to spot them easily in code. That's half the battle. You're now an expert function spotter.

Challenge: Favorite cities


Selecting transcript lines in this section will navigate to timestamp in the video

(upbeat music) - What's your favorite city? Mine is Santa Barbara, California. I love the food
and the amazing landscapes. As you drive through the city, you can see both the
mountains and the ocean at the same time. I just love it. So for our final challenge, I'd like
you to create a program that prints out the name of your favorite cities, but here's the
catch, you need to use at least one function. Your program should meet the following
requirements, has a function named favorite_city, favorite_city should have one parameter,
name, you should call favorite_city at least three times, the output should include one of my
favorite cities is. Remember to have fun and try your best. In the solution video, I'll share how
I tackled the problem.

Solution: Favorite cities


Selecting transcript lines in this section will navigate to timestamp in the video

(upbeat music) - Welcome back. I hope you had fun thinking of your favorite cities and
writing the code for this challenge. Here's the solution I came up with. On line number one, I
just have a comment. It prints out the name of a favorite city, because that's what our
function does. Then on line number two, we have the function definition. Notice that we're
using the def keyword. This is how we let Python know that we are creating a function. It
takes in one parameter named Name. Then, inside of our function on line number three, we
use the variation of the print method that first displays a string, one of my favorite cities
is, and then it prints out the value of the name variable with a space before it. On line number
five, I call the favorite city function with one of my favorite cities, Santa Barbara,
California. Then on line number six, I call it again with another one of my favorite
cities, Asheville, North Carolina. And finally, on line number seven, we call our function using
Amsterdam, The Netherlands. Let's run this code and check out the output. Yay, it called the
function three different times, with each of my favorite cities. Cool! Were you able to use a
function to print out your favorite cities? How'd it go? Did you come up with something
similar? I hope so. Breaking up our code into reusable chunks helps us to develop
programs that are easier to read, write, and debug. Now you have one more tool in your
programmer's belt.

Question 1 of 15

Which keyword does Python use to define a function?

 def

Correct

 func

Incorrect

 function

Incorrect

This keyword is used to define a function in some other languages but not in
Python.


sub
Question 2 of 15

What does the void keyword mean when it is specified before a function definition?

 The function does not return a value.

Correct

 The function name is "void".

Incorrect

Since this keyboard comes before the actual function name, it is not the
function name itself.

The function voids all variables it received.

The function is not expecting any variables.
Question 3 of 15

What can the following function be used for?

def mystery(x): if (x % 2 == 0): return("yes") else: return("no")


checking whether a variable is a numeric value

 checking whether a number has more than 2 digits

Incorrect


checking whether a number is larger than 2

 checking whether a number is even or odd

Correct

Question 4 of 15

Which is not a benefit of functions?

 Functions eliminate crashes in your code.

Correct


With functions, you don't have to repeat the same code over and over again.

It's easier to fix a mistake in a function than one copied and pasted in multiple
places.
Question 5 of 15

What will the following code print?

def compare(): print(5, "is greater than", 6)



5 is greater than 6

 This code will not print anything.

Correct


56

is greater than
Question 6 of 15

The code that's contained inside of a function is often called the function's legs.


FALSE

 TRUE

Incorrect

Replay

Review this video

Creating and calling functions

3m 47s

Question 7 of 15

Why is there no output for this program?

def goodbye(): print("Bye")

 We never called the goodbye function.


Correct


It's so hard to say goodbye.

 We didn't spell goodbye properly.

Incorrect

Question 8 of 15

Given the following code, how would you call the function?

def hello(name): print("Hey,", name)

 hello() "John"

Incorrect

This calling form is not accepted by Python, as the provided argument is not
inside the parentheses.


"John">hello()

name("John")

 hello("John")

Correct

Question 9 of 15

What is the issue with this function as defined?

def double(): print(x*2)

 The function name must start with a capital letter.

Incorrect

 The number to double is not received.

Correct


A numeric value must be converted to string before printing.

 The function must return a value.

Incorrect

Question 10 of 15

Which Python keyword is used to send back values from a function?

 return

Correct

 put

Incorrect

 send

Incorrect


pass
Question 11 of 15

What will the following program print?

def mult_inv(num): return 1/num result=mult_inv(3) print(result)


result

 1/num

Incorrect


3

 0.333333333

Correct
Question 12 of 15

What is the output of the following program?

def isEven(num): return num % 2 == 0 if isEven(3): print("3 is even") else: print("3 is not even")

 3 is even

Incorrect


3 is not even

 There is no output.

Incorrect

Replay

Review this video

Returning values from functions

4m 45s

Question 13 of 15

Given the first line of the function definition below, which programming language is
this?

def factorial(number)

 Ruby

Correct


Java

C++

 Python

Incorrect

This cannot be the first line of a Python function definition, since it does not
end with a semicolon.

Question 14 of 15

Which value is not an argument to the "hello" function?

def hello(name): print("Hey,", name) hello("John") hello("Mya")

 Hey

Correct


Mya

John
Question 15 of 15

In this function definition, the variable "name" is _____.

def hello(name): print("Hey,", name)

 a parameter

Correct


a function body

 an argument

Incorrect

Exploring languages
Selecting transcript lines in this section will navigate to timestamp in the video

- There are hundreds of programming languages out there. Let's take a look at the most
popular. At the time of this recording, Java is number one, closely followed by C, C++, and
our friend, Python. And you might be surprised to know that they're all related. That's right,
Java, Python, and C++ all inherit ideas from C. The C programming language was developed
in the 1970s. It's used for programming operating systems, games, smart devices, and even
compilers for other languages. Here's an example of a program in C. Let's walk through
it. The include statement we see here on line number one is very common in C and C-based
languages. It's the way you as a programmer get to link to code that someone else wrote into
your program. Did you know you could do that? This is very important to know because as
programmers, it's our job to develop software that meets a business need. And we often do
this with a specific deadline in view, so it wouldn't make much sense to write the same types
of functions again and again, especially if they're already available and have been verified by
other programmers. In fact, this is so common in computer programming that we call the
grouping of variables and functions that someone else has written and verified, a library. In
our example, we want to link to the standard input and output library. This is available for the
C language. This library includes the code for the print F function. We use print F to display
our hello world message to the user. Okay, enough about libraries. Let's go to the next line of
code. The two forward slashes are how we create a comment in C, whereas if you recall in
Python, we use a hash mark. There's a function here called main. This is a very important
function in C-based languages because it's the function where the program begins. And just
like what we've seen with Java and JavaScript, curly braces are used for the function's
body. Both languages adopted that syntax from the C language. And finally, as our function's
body, we make a call to the print F function to display our message. And that's it for our C
program. If you're interested in making a desktop, web, or mobile application, C is not really
your best choice. It's a low-level language and requires more advanced programming
knowledge. However, some of its successors are ideal for these types of applications. Let's
take a look at a few. First up, Java. Java is a cross-platform programming language. This
means that you can write your Java code, compile it, and then any device that has the needed
program installed can run it. It's famous for desktop applications and mobile apps for
Android. It's a C-based language, but unlike C, everything in Java is in what's called a
class. Here's some Java code. It looks pretty close to what we saw with the C programming
language with its use of curly braces, semicolons, and the main function. One of the new
additions is the class keyword. This is how the Java language prefers to organize its
code. Now let's take a look at one more language, C++. C++, as the name hints at, is also a
C-based language. It's earlier to learn and work with than C because it has something called
garbage collection. Garbage collection cleans up unused variables for us so that we don't
have to worry about handling it ourselves. C++ is used for games and game engines. In this
video, we've only covered three programming languages and there are dozens more. Swift
and Objective-C for iOS development, Ruby and JavaScript for web apps, and so on. With so
many languages available, you're probably wondering, which one should you choose? Well,
you already have the basics of the Python language under your belt, so it makes sense to
continue with that. I recommend the course Python Essential Training to help further your
knowledge of the language. However, depending on what you want to develop, now you
know what other languages you can pursue.

Next steps
Selecting transcript lines in this section will navigate to timestamp in the video

- We're done, congratulations. You now know the fundamentals of programming. You've


learned how programs are created using programming languages. You've been able to write
source code and run it from your own computer. And you have a foundational
understanding of the Python programming language. I hope you feel inspired to keep
learning more. At this point the best thing you can do is start practicing. Don't feel that you
have to learn everything there is about a specific language or tool before you can get your
hands dirty. I could watch every video there is about Salsa dancing, but I won't get any better
until I get up and start moving. It's the same for you and learning how to program, you need
to practice coding on a consistent basis to get better, and to feel more confident. Here are a
few ideas to help you get started. Search online for a local meetup group that caters to new
programmers. Participate in an online community focused on your chosen language. Create
some programs that use the skills you've learned so far. Check out the exercise files for a few
ideas. Also, be sure to check out the other courses in the Programming Foundation
series. This will give you a well-rounded view of what's involved in developing software. I
really enjoyed putting this course together for you, and I wish you much success on your
coding journey.

You might also like