0% found this document useful (0 votes)
44 views

Learn Python - Full Course For Beginners (Tutorial-WT - Summaries

The document is a tutorial for learning Python. It introduces Python and explains how to install Python and PyCharm, an integrated development environment. It then covers creating a "Hello World" program in Python and how to draw basic shapes using print statements. It discusses using variables to store and modify data in Python programs. The key data types in Python - strings, integers, and Boolean values - are also introduced.

Uploaded by

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

Learn Python - Full Course For Beginners (Tutorial-WT - Summaries

The document is a tutorial for learning Python. It introduces Python and explains how to install Python and PyCharm, an integrated development environment. It then covers creating a "Hello World" program in Python and how to draw basic shapes using print statements. It discusses using variables to store and modify data in Python programs. The key data types in Python - strings, integers, and Boolean values - are also introduced.

Uploaded by

techv.sahil
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Learn Python - Full Course for Beginners [Tutorial]

Made by Wordtune Read | Open

Introduction
Python is one of the most popular programming languages out there and it's by
far one of the most sought after for jobs. It's so easy to use that you can start
writing your first program in seconds.
In this course, I'm going to teach you everything you need to know to get started in
Python. I'm going to hold your hand through this entire course and help you feel
confident and start writing scripts and programs that are awesome.
I can't wait to teach you guys Python. Follow along with the course and learn this
amazing programming language.

Installing Python & PyCharm


We're going to install Python on our computer and also a text editor that we can
use to write our Python programs in. So the first thing we have to decide is which
version of Python we want to download.
Python two is a legacy version of Python, meaning it is not being actively maintained
or supported by Python anymore. Python three is the newest version of Python, and
it is the one that is getting actively maintained and supported.
For the purposes of this tutorial, I'm going to be teaching you guys Python
three. You can learn Python three or Python two, the differences aren't very big, and
you can jump right in and code in Python two as well.
We need to choose a text editor so we can write our Python code. An IDE is a
special type of text editor that lets us run and execute our Python code and tells us
how we're doing.
In this course, we're going to be using an IDE called pie charm. So let's go
ahead and download this program.
Pie charm is a free and open source Python development environment. You can
download it from this page and run it from your applications folder.

Setup & Hello World


The first thing I want to do is open up pie charm, and I want to change the
appearance of the IDE. So I just went over here to appearance and behavior,
appearance, and I changed the theme to dark.
We want to select Python version three as the interpreter, and create our project. If
you don't, you might not be able to follow along with this tutorial.
We have our Python project up and running, and we want to create our first Python
program. So we can just call this app, and we can just click okay.
We have our first Python file open, and we can just start typing in some Python.
We're just going to print something out onto the screen and say hello world. This is a
very basic Python program that prints out hello world onto the screen. To run it, you
just click run and choose the app to run, and the text hello world gets printed out in
the console.
You've written your first Python program, and we're going to be writing all
sorts of awesome Python programs.

Drawing a Shape
In this tutorial, I want to talk to you about writing a basic Python program. We're
going to look at how we can go ahead and write our programs, how they're getting
executed by Python, and we're also going to draw a little shape onto the screen.
We can use some Python instructions or Python code in order to draw out a shape
onto the screen. To do this, we can use something called print statements.
The print statement is used to tell Python to print something out to the console. We
can use it to see what's going on in our code, and we're going to use it to
print out a triangle onto the console.
We were able to print out our shape using Python. The order in which we write the
instructions matters a lot, so if we put this last line at the top, it will draw out a little
upside down bottom thing at the top, and then the rest of the triangle down here.
In this tutorial, we're going to talk about using variables in Python. Variables are
basically like little instructions that we can give to the computer to execute.

Variables & Data Types


In Python, we use variables to store data, and they make it easier to work with and
manage all of the different data inside of our programs. So in this tutorial, we're going
to look at variables, why they're useful, and what types of data we can store inside of
variables.
A man named George, who was 70 years old, really liked the name George, but
didn't like being 70. So he wrote a little Python program that prints out some text onto
the screen.
If you want to change a character's name, age, or gender, you have to go through
every place where the character's name or gender is mentioned and manually
change the text to reflect the new information.
In order to change the character's name and age in our program, we have to
look through the entire program, find it where that value was and change it. But we
can use a variable in order to store the character's name and age in our
program.
We can create a variable for the character's name and age in Python by giving
Python a couple pieces of information. The first piece of information we need to give
Python is the name of the variable.
I'm going to type out the character's name inside of quotation marks, and
then I'm going to type out the character's age below this character name
variable. Now I can replace the character's name inside of the story with this
variable.
In this program, I want to print out the character's name, plus all of this text,
plus the value that's stored inside of character name, plus all of this text. And
what Python is doing is getting the value from the character name variable and
inserting it inside of the text.
I can also do this for the character's age. Now when I run this program, it
won't print out the same story that we were printing out before, but I won't
have to type in the character's name and the character's age.
Variables are a really awesome way to control and manage the data that's
inside of our programs. You can also modify their values, so halfway through the
story, I changed the character's name to Mike, and now it says there once was
a man named Tom.
Python stores data inside of variables as strings, which are basically just plain text.
So if you want to store something like a character name, you can store it inside of a
string.
Python can also store numbers, so instead of storing the character age inside of a
string, we could instead store a number. And you can store all different types of
numbers inside of Python.
There are three basic types of data that we can work with in Python: strings,
integers, and Boolean values. We can also store what's called a Boolean value,
which is essentially a true or a false value.
There are three basic types of data in Python: strings, decimal numbers, and true or
false values. You're going to be using variables all the time in Python, so practice up
and get comfortable using them.

Working With Strings


In this tutorial, I want to talk to you guys about working with strings in Python. We're
going to get a full introduction into why strings are awesome, and we're going to print
out a string to demonstrate what we can do with strings.
When we have these strings, we can do a bunch of cool things with them. For
example, we can create a new line inside of the string and say draft Academy.
In addition to the backslash n, you can also use a backslash quotation mark to
include a quotation mark inside of your string. You can also use a backslash
quotation mark to print out a backslash.
In addition to just typing out a string, you can also create a string variable. You can
then access that string variable by typing the name of the variable.
I can also use something called concatenation, which is basically appending another
string onto another one.
In addition to doing all that stuff, we can also use special things called functions. And
I'm going to show you guys a couple common functions we can use with these
strings, and they're actually going to do awesome stuff.
In addition to converting a string into uppercase and lowercase, you can also check
to see if a string is entirely uppercase or entirely lowercase.
I can use these functions in combination with each other, so I could say phrase dot
upper, and then parentheses, and then dot is upper, and it would convert the whole
thing into uppercase. There are a few other functions that I want to show you, like
how to figure out the length of a string. And that's going to be really useful as
we go forward. Python allows you to get individual characters inside of a string using
open and closed square brackets. For example, phrase square bracket zero will give
you a capital G.
In Python, when we work with strings, we start with zero. So if I wanted to access the
first character in the string, G, I had to put zero 123, and if I wanted to access the
first a, I had to put three inside of here.
The index function will tell us where a specific character or string is located inside of
our string. We can give this function a value, and we would call this passing a
parameter, and you’ll hear that word a lot.
You can put actual words in here, like Academy, or even just a C A D, and it will tell
you where this starts inside of your string.
If I put something in here that wasn't in the string, it's going to throw an
error, so the index function can be really useful.
There's also a function called replace, and you can give it parameters like what
you want to replace with and what you want to replace it with.
Python has a lot of different functions that we can use with strings, and you should
definitely get comfortable working with them. In this tutorial, we'll talk about working
with numbers in Python.

Working With Numbers


Python numbers are one of the most common data types, and any Python program
you write is most likely going to be dealing with numbers at some point. We can use
numbers to do certain mathematical operations, and we can also do some awesome
things with numbers.
Python allows you to do basic arithmetic operations such as addition, subtraction,
division, and multiplication. You can also use parentheses to specify order of
operations, such as multiplying three times four plus five to get 17 or adding four and
five to get nine.
We can also use the modulus operator, which is going to take a number and divide it
by another number, and give us the remainder.
We can also store these numbers inside of variables, so we could create a variable
called underscore num, and print out a value of five.
In addition to basic arithmetic, you can also convert numbers into strings. This is
useful when you want to print out numbers alongside strings, or if you want to get rid
of a string right before printing it.
There are a bunch of different math functions that we can use with numbers in
Python. A function is basically just a little collection of code that does something.
I'm going to show you guys some of the most common functions that
you'll be using in Python related to numbers, including ABS, which gives us the
absolute value of a number.
There are a few other functions that I can use, like pow, which allows me to take
numbers to specific powers. So I can say three comma two and get nine, or four and
get 4096.
In addition to using the pow function, we can also use the max function, which
returns the larger of two numbers.
Another cool function is called the round function, and it allows us to round a
number. It follows standard rounding rules, so 3.2 would be rounded down to three,
and 3.7 would be rounded up to four.
In order to get access to other math functions, we have to import something called
Python math into our file. This line of code is going to grab a bunch of different math
functions that we can use.
There are several functions inside of the math module that allow us to do a lot more
things with math inside of our program, including the floor method, the seal function,
and the square root function.
There are a bunch of different math functions in Python, and you can search online
for them. They are all documented, so you can just use them inside of your programs
to perform different math operations.

Getting Input From Users


I want to show you guys how we can actually go about getting input from someone.
All I have to do is type out input, and then an open and closed parentheses, and
inside of these parentheses, we can actually type a prompt.
What I want to do is store the value that the user inputted into our program inside of
a variable. So I created a variable, I set it equal to input, and then I can print out
whatever the user entered into the name variable.
We could prompt the user to enter in more than one piece of information, so we
could say hello, name, and then I can say, you are, and I'll print out their age.
So now we can get two pieces of information from the user, name and age.
The program can get input from the user and tell them how old they are, and it can
also do some really cool things with these inputs.

Building a Basic Calculator


In this tutorial, I’m going to show you how to build a basic calculator in Python. We’ll
talk about getting input from users and adding numbers together.
We want to create two variables and store the two numbers that the user wants to
add together inside of those variables. We then want to print out the result of adding
these two numbers together.
When we get input from a user, Python is just going to convert it into a string. So
when we try to print out the answer, it's not quite the answer we were looking
for.
In order to make these variables numbers, we have to convert the strings that we get
from the user into numbers. We can do this by coming over here and using a special
Python function called int.
We'll just see if this works, and then enter our first number and see the result.
This is essentially our basic calculator. We have one problem with this calculator,
and that is that we can't use decimal numbers inside of the int function. But if we use
the float function instead of an int, we can use decimal numbers inside of the int
function.
In this tutorial, I want to show you guys how to build a madlibs game in Python. We'll
convert two numbers that the users input into numbers, add them together, and print
out the results.

Mad Libs Game


Madlibs is a game where you enter a bunch of random words and put them into a
story randomly. It can be pretty fun, and I'm sure you've heard of madlibs before. I
want to create a madlib for a poem that says roses are red, violets are blue, I love
you. But instead of having the user enter in their own color, violets, why don't they
enter in a plural noun?
We're going to create three variables, color, plural noun and celebrity, and
we'll store the user's inputs inside of those variables. We'll then take
all of those words and put them inside of our madlib, and hopefully we end up with
something that's pretty funny.
We created three variables: color, plural noun and celebrity. We replaced these guys
with those variables, and now we have a color, plural noun and celebrity.
We created a very simple mad lib that prints out the names of colors, plural nouns,
celebrities and the color of microwaves when we click enter. You can see how you
could take something like this and create a little mad libs game.

Lists
In this tutorial, I want to talk to you guys about working with lists in Python. Lists are
essentially just a structure that we can use inside of Python to store lists of
information, and they allow us to organize them and keep track of them a lot easier.
In my program, I'm actually going to create a list. We create a list a lot like we create
a Python variable, by giving it a name and setting it equal to the list of values that
represent our friends.
When we create a normal variable in Python, we just give it one value, but when we
make lists, we can store multiple values inside of the same little item inside of the
same object. And then we can access these individual items inside of our program.
So now that we've created our list, how can we access individual elements
inside of this list? We can refer to elements by their index, which is start at zero.
If I wanted to access a specific element inside of my friends list, I could just type its
index inside of an open and closed square bracket. I could also use negatives to
access items at the back of the list.
There are several ways to access portions of a list, including grabbing the element at
index position one and all elements after that, or grabbing the element at index
position one up to but not including the element at index position three.
You can either put one with a colon after it, or specify a range that you want to grab
from, and you can also modify elements. So I could change Karen's value to Mike by
just accessing Karen.
Python lists can be really useful for modifying values inside of arrays, and in the next
tutorial, we'll learn how to add elements to a list, delete elements from a list, copy
lists, and more.

List Functions
In this tutorial, I want to talk to you guys about using functions with lists in Python.
Lists are one of the most important structures where we can store our information,
and there are a bunch of different functions that we can use with lists.
The extend function allows you to take a list and append another list onto the end of
it. So I could say like friends dot extend lucky numbers, and now my friends list also
contains all the elements inside the lucky numbers list.
A simple way to add two lists together is to use the append function. You can also
add individual elements onto a list. To add an item into the middle of the list, you can
use the insert function. It takes two parameters, the index where you want to insert
the item and the name of the element you want to add.
Kevin, Kelly, and all the other elements have been pushed off to the right, and you
can also remove elements by saying friends dot remove. You can also remove all
the elements by saying friends dot clear.
There's also another really cool method, which is called pop. It basically
removes the last element in the list, so if I wanted to check if my friend Kevin was in
the list, I could say friends dot index.
If you put a name in a list, and it says Oscar is in the list, then you can get the index
of Oscar, and if it says Mike is not in the list, you can get the number of similar
elements in the list.
So we could say friends dot sort, and it would put the list in ascending order. We
could also say lucky numbers dot sort, and it would put the list in reverse order, so
instead of 4815 1623 it would be 2316 15 842.
There are some basic list functions that you can use to get your feet wet with lists
and the more you practice using them, the more comfortable you're going to get.

Tuples
First thing I want to do is show you how to create a tuple. A tuple is a list of values
enclosed in open and closed parentheses.
Tuples are immutable, meaning that once you create a tuple, it can’t be changed. To
access the elements inside a tuple, you use an open enclosed square bracket and
pass in an index.
A tuple is a collection of elements, and you can access each entry individually. If you
try to change the values of the elements inside a tuple, Python will give you an error.
Tuples are basically doing the same thing as lists, but you can't mutate them. A list is
immutable, but a tuple isn't.
Tuples are used to store data that can’t be changed or mutated, so they’re a good
candidate for storing data like coordinates. But in practical use cases, people will use
lists most of the time, and tuples are more of a niche.

Functions
A function in Python is just a collection of code that performs a specific task. To
create a function, you just type a keyword called death, give the function a name,
and then enter the task that the function performs.
Just like we give variables descriptive names, we also want to do the same thing
with functions. When we create a function, we just type out the name of the function,
and then we can either type out say hi with no underscore or say hi with an
underscore.
When I clicked enter, my text editor automatically indented the text. If I wrote some
code outside of this function, it would no longer be considered inside the function.
A function is a piece of code that performs a certain task, and in order to execute it,
we have to specify that we want to execute the function by calling it.
To call a function in Python, you type out the function name and open and close
parentheses. When Python executes your program, it goes over the first line, prints
out the word top, and then goes down here, prints out the word say hi.
Python actually jumps up to the say hi function, executes all the code inside of it, and
then jumps back down to the next line, which is bottom.
In Python, functions are named in lowercase and if there are two or more words, we
use an underscore in between them. But sometimes it's easier to leave it without an
underscore.
We can give our functions information by using parameters. So if you want to call
this function, you need to give us some information, so type out the name of the
parameter that you want to receive. If we allow the code calling this function to tell it
what name to say hi to, we can access this parameter or this variable inside of our
function, and instead of saying hello to the current user, we can say hello to
whatever name got passed in.
We can give a function information, and depending on the information we give it,
it'll perform its task a little bit differently. We can also include more than one
parameter, and we can write out one line of code that prints out like hello to
someone.
You can pass anything you want into a function, and it's going to work just the
same. As you go through with Python, you're going to be using functions more
and more, and it's a good idea to break your code up into different functions.

Return Statement
When we call a Python function, we want the function to perform a specific task, and
then we want to get information back from the function. The return keyword allows us
to do this, and we'll show you guys how we can do this.
We're going to make a function that will cube a number. We can specify any
parameters that this function will take, and we can execute this function by calling it
and passing it a value.
When we call a function in Python, it doesn't do anything, but when we use the return
statement, it gives us a value back. So when we print out cube three, it should print
out the answer because we got an answer back.
Using the return statement allows us to return a value back to the caller, in other
words, back to whatever's calling the function. So if I create a variable called
result and set it equal to cube four, it will store the value that gets returned from
executing that function.
The return statement is used to get information back from a function, and can be
used to give information to a function. The return statement will break us out of the
function, so we won't be able to put any code after this return statement.
You can return any data type from a function, including a string, Boolean or an array.

If Statements
In this tutorial, I want to talk to you guys about using if statements in Python. If
statements allow our programs to respond to the input that they're given, and
they are things that we as human beings deal with every day.
In my text editor, I wrote out a bunch of if statements, and the first one says I wake
up, if I'm hungry, I eat breakfast. If the condition is false, I just skip it and move
on.
Another example of an if statement, this one is a little bit more complex than the one
we just looked at, it says I leave my house, if it’s cloudy, I bring an umbrella,
otherwise, I bring sunglasses.
If I want meat, I order a steak, but if I want pasta, I order spaghetti and meatballs. If
neither condition is true, I order the salad.
All three of these if statements are valid if statements, it's just that they get more and
more complex as we go down the list.
In this tutorial, I'm going to show you guys a very basic if statement. I'm going to
create a Boolean variable called is underscore mail, and I'm going to set it equal to
true because I'm a male. If I want to use an if statement, I just type out if, and then I
just need to type out a condition. The condition has to be reducible to a true or false
value.
If I specified my true or false condition, and now I want to just type a colon, then I
want to make a new line, and anything that I put below this with an indentation is
actually going to be executed when that conditions true.
We can use another keyword in Python, called else, to make if statements that cover
more situations. For example, we could have dozens of lines of code inside of an if
statement, so let's make this more complex.
If I want to write an if statement that checks both variables, I can use the or keyword,
or if is male or is underscore tall.
If one of these values is true, then this code will execute, otherwise it will say you are
neither male nor tall. So let me show you guys how this works.
The and operator is similar to the or operator, except both conditions have to be true.
So if the person is male and tall, then we're going to execute whatever is in
here, if not, we're going to execute the one inside of the else.
There's one more thing we can do to check if someone is male and tall, we can use
another keyword called else if, and we can put another condition after this, such as
male and not tall.
We have an if statement that covers every single condition for these two variables. It
will print something out in every single situation of the two values of these variables.
If I make both of these true, it tells us that you are a tall male, if I make both of these
false, it tells us that you are either not a male or not tall, or both.
In this tutorial I want to talk to you some more about if statements in Python. I'm
going to show you guys how you can actually use something called comparison
operators.

If Statements & Comparisons


In this tutorial we're going to look at using comparisons inside of our if statements, so
instead of just using Boolean values we can actually compare different values. And
depending on the results of those comparisons we can do certain things.
For this tutorial, I'm going to create a Python function that will give us the
maximum number that we pass into it. To figure out which of these numbers is the
largest, we're going to have to use an if statement.
We can compare two numbers using a comparison operator to determine if one
number is greater than or equal to the other. If the results of these two comparisons
end up being true, then we know that number one is the biggest.
We can return num one, we can say return num one, but we also want to check
some other conditions. So we can say L if, and now we want to do some other
comparison, so we can say else if num two is greater than or equal to num one.
We are basically calling the new function that we just made, and we are printing out
the output. It can take on any size without a problem.
Comparison operators are a common way to make if statements in Python. They can
be used to compare different values, such as numbers, strings, and bullions.
In this tutorial, I'm going to talk to you guys about building a calculator in Python.

Building a better Calculator


In the beginning of the course, we created a very basic calculator that would add two
numbers together and spit out the answer. In this tutorial, we will build a more
advanced calculator that will allow the user to specify which arithmetic operation they
want to perform. We want to get input from the user, so we're going to create
three variables and set them equal to input. We're going to give this a prompt
and convert the first number the user enters into a number.
In Python, when we get input from the user, it just gets converted into a string. We
want to immediately convert it to an actual like number data type instead of a string,
so we can add, subtract, or multiply these numbers together.
We need to figure out what operator the user was trying to do, so we can use an if
statement. Then we can print out num one plus num two if the operator is equal to a
plus sign, or num one minus num two if it is equal to a minus sign.
We can just do these for everything, so we can print out num one divided by num
two, and num one times num two.
Alright, so basically we have every condition, and an else statement. If the user
enters an operation that isn't one of the four specified, we throw an error.
Alright, so we got the input from the user and then we used if statements to figure
out what they wanted to do. So we can check to see if they wanted addition,
subtraction, division or multiplication, and we can also check to see if they entered in
an invalid operator.
We can use if statements in combination with getting the user input to create a little
calculator.

Dictionaries
A dictionary is a special structure in Python that allows us to store information in
what are called key value pairs. In this tutorial I want to create a little program that
will allow us to convert a three digit month name into the full month name.
We're going to create a dictionary and then we're going to define key
value pairs. We can do this by typing out the key and then typing out the value, so
we can convert three digit month names into the full month names.
I can create a dictionary for each one of the months by specifying a key and a value.
The keys have to be unique, so I won't be able to create a dictionary with duplicate
keys.
I've created an entry for each of the 12 months and we can access them from
inside of this dictionary by name. There are a bunch of different ways that we can
access these month names, such as by making an open and close square bracket
and typing in one of the keys.
When dealing with dictionaries, you might put in a key that doesn't necessarily map
to a value inside of the dictionary. In those cases, you might want to create some
sort of default value that will be used if the key is not found.
If I have a key that is not mapable to any values inside of this dictionary, I can
actually pass it a default value to get printed out.
Dictaries are really useful for storing key value pairs, and you’re going to be using
them a lot in Python. In this tutorial, I want to talk to you guys about while loops.

While Loop
In this tutorial, I'm going to show you the bare basics of how while loops work, and
then in future tutorials, we're going to use while loops to create little games and stuff
like that.
When you create a while loop, you first create an integer, then a condition, and then
a while loop declaration. Anything that's below this while loop declaration and that's
indented like this is going to be considered code that's inside the while loop.
I'm going to write a line of code that prints out the value of I and then
increments I by one. There's a shorthand we can use in Python to do
something like this though.
Python is going to check this condition before it does anything else, so if I is equal to
one, then we're going to keep looping through this code until this condition is
false, then we're just going to print done with loop.
We're going to loop through all the code inside of this while loop declaration as
long as this guy is true, and then we're going to check the condition again, and
if it is true, we'll go through and execute all this code again.
Python is going to check a condition before executing code inside a while loop, and if
the condition remains true it will keep executing the code inside the loop until i is
equal to eleven, at which point it will print out a done with loop line.
In this tutorial, we're going to build a basic guessing game in Python using if
statements, while loops, variables, and all sorts of cool programming structures.

Building a Guessing Game


We will specify a secret word, and the user can interact with the program and try to
guess the secret word. We will store the user's responses in a variable, and we will
allow the user to keep guessing until they get the secret word right.
We need to be able to prompt the user to input the secret word, and if they
don't guess it correctly, we want to prompt them to enter it again. So we need
to use a while loop, and specify a looping condition or a looping guard.
Inside of this loop, we're going to ask the user to input the secret word. If their guess
is equal to the secret word, we're going to break out of the loop and print out a
success message. This is a very simple program that will prompt you to enter a
guess as long as you don't enter the secret word. If you enter the secret word, the
program will terminate and say "Hey, you win!"
We created a fully functional guessing game, but we think it could be improved quite
a bit. So let's try to program a game where the user has three tries to guess the
word, and if they can guess the word inside of three tries, they win the game.
I want to keep track of how many times the user has guessed, so I'm going to set a
variable called guess count equal to zero and increment it every time we go through
a while loop.
We're going to need a variable called guess limit, a variable called out of
guesses, and a variable called guess limit. The out of guesses variable tells us
whether or not the user is out of guesses, so they can keep playing.
When the user has not used up all their guesses, I want to check to see if they have
any more guesses left, and if they have, I want to give them a guess and then
increment the guess count, and if not, I want to set out of guesses equal to true.
We need to add another condition to this loop guard, so that we can break out of the
loop if the user runs out of guesses. If the user doesn't guess the secret word and
they aren't out of guesses, then the loop will continue, otherwise it will break out and
print out you win, if the user guessed the secret word and they are not out of
guesses, then it will print out you win.
Alright, so now we have all the logic for this little guessing game setup, let's see
if we can run it and play through it. If we don't get it in the number of tries, we
lose the game.
We created a few variables, including a secret word variable, a guess variable, a
guess limit variable, and a guess count variable. When the guess count is less than
the guess limit, we get the input from the user, otherwise, they lose the game.
While loops and if statements can be used in combination with each other to build a
pretty awesome guessing game. If the user runs out of guesses, they lose, otherwise
they win.

For Loops
In this tutorial, I want to talk to you guys about using for loops in Python. A for loop
allows us to loop over different collections of items, and the easiest way to wrap your
head around why for loops are useful is just to see some examples.
In our case, we are going to call this letter and we are going to say for letter in. We
are going to put a string in here and we are going to put a colon and we are going to
say for every letter inside of draft Academy, I want to do something.
The first iteration of the loop printed out the first letter in draft Academy, the second
iteration printed out the second letter in draft Academy, the third iteration printed out
the third letter in draft Academy, and so on. We can also use this with other
collections, like an array.
For each friend inside of this friends array, I want to print out the friend. We can also
loop through a series of numbers, so we could say for index in range, and then in
here, I can pass in a number.
When I run this program, it will print out every number between zero and 10, not
including 10.
You can specify a range of numbers to print out, including three, four, five, six,
seven, eight, nine, and not 10.
Ranges can be really useful. For example, I could use a range to loop through an
array, and I could pass in the length of the array, and I could access each individual
friend inside of the array, just like we did before.
A for loop in Python is a way to iterate over a collection of items, and it can be used
to print out the elements of an array, or a string, or even a range of numbers.
You can use all sorts of logic inside of these for loops, like if index is equal to zero,
then print out first iteration, and otherwise, print out not first. So don't be afraid to put
some complex logic inside of these for loops.

Exponent Function
In Python, it's really easy to do exponents by using two multiplication signs. But
we can use a for loop to create our own exponent function that will take a base
number and raise it to a specific power.
Inside of this function, we need to start writing some code, but we don't
necessarily know the value of this pound.
In order to write this function, we're going to need to use a for loop. We're going to
multiply the base num by itself as many times as the power num specifies, and we're
going to loop through this for loop as many times as pound them.
We can basically say that result is equal to result times base num, and this will give
us everything we need to take this number to the specific power.
The second time through the loop, we’re multiplying result by base num again, and
the third time through the loop, we’re multiplying result by base num again, and
finally we’re returning the result.
Let's raise three to the second power, square three, raise three to the fourth
power, raise two to the third power, and print out the answer. Our raise the power
function is working just as expected. We're taking in two pieces of input, a base
number and a power number, and we're multiplying the result by the base
number and returning the results.
To build a power function, you have to type something out just like this. This tutorial
will explain two different concepts in Python.
2D Lists & Nested Loops
It's going to be a pretty cool lesson, and I want to show you guys how to create two
dimensional lists. To do that, you can just create another array and put that inside of
the first element of your list.
We can create a grid like structure inside of Python using two dimensional lists. We
can access individual elements inside of this list structure by saying number grid,
and then putting the index of the row that we want to access.
The index of the row is the same as the index of the column, and we can access
elements inside of this 2D list by putting a zero in the right square bracket.
I want to show you guys how to use a nested for loop to print out all the elements
inside of an array.
We're going to create a for loop that will print out each row of the number grid, and
then we're going to create another for loop that will give us each individual column or
each individual element inside of each of these arrays.
Python allows you to use two dimensional lists and nested four loops together, and
in this tutorial, I'm going to show you how to build a basic translator in Python.

Building a Translator
We can translate a phrase or a word into a different language by converting any
vowels in the phrase or word into a G, and then we'll get the draft language. So
let's start making a draft translator.
We need to translate English into our draft language. To do this, we need to create a
variable called translation, set it equal to the empty string, and then loop through
every letter inside of the phrase that they passed in, and add them onto this
translation one by one.
If the letter is a vowel, we can add a G onto translation, otherwise we can just add
the letter onto translation anyway. So we can check to see if the letter is in a string,
and if it is, we can convert it into a G.
We're going to test out our translate function by allowing a user to input some
information. We're going to call this function translate, and inside of here,
we're just going to pass whatever the user inputs.
We can create a little translator app using a for loop in combination with an if loop.
We can make this a little bit more efficient by saying if letter dot lower in, and then
typing out the lower case letters.
This program can translate words for us, but it doesn't keep uppercase syntax if we
start our word inside of the phrase with a capital vowel. We can fix this by using
another if statement in here, and setting the translation equal to the translation plus a
capital G.

Comments
A comment is a line of code inside of our Python file that's just not going to get
rendered by Python. It's just going to be used for us humans. A comment is
used for me or another developer to write a little comment, a little like plain text,
inside of a file. The comment is ignored by Python.
If you want to explain a line of code, you can write a comment, which anybody
looking at your file would be able to read.
If you want to make comments on multiple lines, you can just create a new line and
use another hashtag. Or you can use a triple quotation mark, but the official Python
style guides recommend using these hashtags.
Comments can be useful for removing a line of code from a program, so you can see
if the code is causing you trouble or if you want to see what your program would be
like without the line of code.

Try / Except
In this tutorial, I want to talk to you guys about catching errors in Python. I want to
show you guys an example of using this down here, where I'm prompting the
user to enter in a number and then converting whatever they entered into an integer.
When we run a program, if we don't enter a number, the program will throw an
error. This happens a lot, and we've just kind of accepted it as a reality.
If you don't want your program to break when a user forgets to enter a number, you
can use a try except block to allow your program to try out a piece of code and if
everything goes well, then you're great.
A try except block allows us to catch an error if the user enters something wrong and
print out invalid input onto the screen instead of yelling at us and breaking the
program.
A try accept block is a powerful thing that we can do in our Python programs to
protect them from errors. It will catch any error under the sun, so if I create a variable
called value and set it equal to 10 divided by zero, the program will fail.
When I run the program, it basically says zero division error division by zero.
However, we can actually catch or accept specific types of errors, such as a division
by zero error or an invalid input error.
I can create two different accept blocks to catch two different types of errors, so if
this division by zero breaks the program, it will tell us, and if this string instead of a
number is entered, it will tell us invalid input.
We can specify what happens when certain things break, such as a valid input, a
value error, a zero division error, or a division by zero error. We can also store the
error as a variable and print it out if something goes wrong.
You can print out the specific error that got thrown in Python. Always use specific
errors, and don't accept anything under the sun.

Reading Files
In Python, you can read from files outside of your Python file. You can use these files
to get information or to parse through different files and do different things. To read
the employees inside of a text file from inside of Python, you have to use the open
command. You can either type in a relative path to the file, an absolute path to the
file, or just the files name.
I can use the open function to open a file in the employees dot text folder in the
same directory as app dot Python. I want to read the information inside the file and
do some stuff with that information.
There are several modes for working with files in R, including write, append, and R
plus. We're going to be reading from the file and storing the contents inside of a
variable, which we can call employee file.
Whenever you open a file, you always want to make sure that you close the file as
well. So once you're done reading a file, you can just close it.
We can use a few different functions on this employee file to figure out what's
inside of it. The most basic thing we can print out is just the entire contents of the file,
but before we do that, we can check to make sure that a file is readable.
If I put a double you here, we can no longer read the file, we can only write to the file.
So let's change this back to our so we can just read the file, and then we can
read an individual line inside the file.
If I were to copy this code and then print it again, it would print out the first two lines
in the file. When I run this program, you'll see we print out Jim salesman and
Dwight salesman. But if I wanted to access a specific line, I could just refer to it by its
index in the array.
You can use the read lines function with a for loop to print out each line in a file.
There are a lot of cases where you're going to want to be able to parse through
information in a file.
When you want to read from a file, you can use the open function, type in the name
of the file and the mode, and then do all sorts of stuff with it.

Writing to Files
Python allows you to work with external files, and you can write new files and
append onto existing files.
Over here, I just have some written out and this essentially just reads information
from this employees.text file and spitting it out on the screen. If I wanted to add
another employee onto this list, I could come over here and append to the file.
We can add another employee into the file by writing something to the end of the file.
So let's add Toby human resources into our employees dot text file and run our
program.
You need to be careful when you're writing to files, because if you run your file
again, or if you append something on something wrong to the file, it's
permanent. So I want to talk to you guys a little bit more about appending.
If I want to add another employee onto the end of the file in a new line, I need to put
a new line character in front of it, so I can say backslash n, and this will append this
entry into the file with a new line.
When you use W, it overrides everything that's in an existing file. You can also
use W to create a new file, so I could say employee file is equal to employees one
dot text, and it would create a new file for me.
When you want to create a new file, you can use different extensions, and you can
add HTML code inside of it. Python is a great language for working with reading,
writing, and doing all that stuff with files.

Modules & Pip


Modules are just Python files that we can import into our current Python file, and
then we can use those functions, variables, and other stuff from that external file
inside the current Python file.
In my text editor, I created a file called useful tools dot Python, which has a bunch of
useful tools like variables, lists, functions, and a dice roll function. I might want to use
this file in other Python files that I work with.
When I need to use one of the functions inside of that useful tools file, I can just
import it into my program and then I can use all of the variables and attributes from
inside that file.
When I say useful tools dot, it gives me access to all the stuff that was in that file. So
I can just say roll dice to simulate rolling a 10 sided dice.
In Python, you can import functionality from external Python files. You can find a
huge list of modules on this website, and you just have to make sure that you click
the right version of Python.
There are a huge list of Python modules on the official Python docs page. You can
click on a module to see how to use it, and it will tell you how to import it and some
basics about it.
There are tons of developers who use Python, and you can actually find a lot of third
party modules to do what you're trying to do. So if you get good at using
modules and you get good at looking for them, you can actually save yourself a
bunch of time.
When you use Python, you can find all of the files and modules you need in the
same folder where you installed Python on your computer.
There is a folder here called lib, and this is storing all of those external modules.
There are also a few modules that are just like built in modules, and you can actually
tell which ones are which.
Some of these modules are external, some are built in, and I want to show you guys
how to install those external modules that don't just come pre installed with Python.
You need to find a Python module that you want to install that you want to download.
You don't have to use Python docs, you can just look up useful third party Python
modules online.
Python docs has a website, and it basically just has some installation instructions. To
install Python docs, you have to use pip, which is a program that allows you to
install, manage, update, and uninstall different Python modules.
To install an external or third party Python module, you just have to say pick install
and type in the name of the Python module. If you don't have a new version of
Python three, you might have to install pip separately.
You can install third party external Python modules using pip. They get placed inside
of a special folder called site packages, and the installation includes a docs folder
and a pi 3.6.ag info folder.
If I wanted to use the docs module inside of one of my programs, I could just refer to
the name of the module, so Python docs. And if I wanted to remove this, I could just
say PIP uninstall.
To use modules, you first need to install them using PIP. You can check if you have
them by checking the site packages folder or the libs folder.

Classes & Objects


In this tutorial, I want to talk to you guys about classes and objects in Python.
Classes and objects are extremely useful in Python programming and they can help
you to make your programs more organized and more powerful.
With classes and objects, we can create our own data types in Python, and then use
those data types in other Python programs. Classes are extremely useful, and they
are used in almost every major programming language out there.
Let's say that I want to represent a student inside of a program, and I
don't have a student data type. I can create a student class in Python, and I will
show you guys how we can do that and create our student class.
We're going to create a class called student and we can use things like strings,
integers, and booleans to define attributes about a student. We can also create an
initialize function by typing out def and then two underscores, the word I N I T, and
then two more underscores.
In this initialize function, I can basically map out what attributes a student should
have. A student has a name, a major, a grade point average, and a Boolean value
that determines whether or not the student is on probation.
The student data type has all of these attributes associated to it. In the initialize
function, we have to do something, and we're going to be writing out some stuff that
might not make total sense right now, but it will make total sense in a second.
Now that we have a student class defined, we can actually use this class inside of
our other files. We can create a student by giving it some information and then using
the student data type. To use this student class, you need to import it from the
student file. The student class is an actual student object with a name, a major and a
GPA.
We can create a student object by giving it a name, a major, a GPA, and an is on
probation value. We can also set whether or not this student is on probation by
saying false.
I'm going to create a student object and then print out some information about
it. I can access each of the attributes from inside of this object, so if I wanted to print
out the student's name, I could say student one dot name.
Now that I created a student object, I can access the information about the student. I
could create as many students as I wanted, and each student would have different
information, like their major, GPA, and probation status. When we create a student
object, we're actually calling this init function, and passing in the name, the major,
the GPA, et cetera.
I'm saying that the name of the student is equal to the name that I passed in,
and that the name of the student's major is equal to the major that I passed in,
and that the name of the student's GPA is equal to the GPA that I passed in.
In Python, we can model real world objects and create our own data types. For
example, we can model a student inside of our program and then create student
objects off of that.

Building a Multiple Choice Quiz


In this tutorial, I'm going to show you guys how to build a multiple choice quiz in
Python. We'll use things like classes, if statements, and loops to build this
program.
I wrote out all the questions that are going to be inside of my multiple choice quiz
beforehand, so I didn't have to spend time doing that. The first thing I want to think
about is how can I represent the questions in the test?
I'm going to create a question class and inside of this question class,
we're going to be able to store the question prompts and the questions answer.
So inside of this question class, we're going to define the different attributes
that will describe or that will be included in a question.
We have a question class set up, so let's create an array of questions and start
creating question objects. We want to pass the first question and the answer to the
first question, and we get an error saying unresolved reference question.
Before we can create these questions, we need to import this question class. So
I'm going to come up in here, come up here and say from question import
question.
Alright, so now we're creating three questions, and the answers to each question are
C and B. We need to write a function that will ask the user the questions and check
to see if they got the answer right.
We're going to create a function called run test that takes a list of questions and
runs through each question, getting the user's answer and checking to see if
it's right. We'll keep track of how the user does through the test by
incrementing the score variable.
For each question object inside of this questions array, I want to do something. I
want to ask the user the question, store their response inside of a variable, and
check to see if they got the question right using an if statement.
I want to print out how the user did on the quiz, so I'm just going to say score
plus equals one and then I want to print out how many questions they got right. To
print a number alongside of a string like this, we're going to have to say STR
and then type in the number. We'll just say how many questions there were by
saying LEN and then saying questions.
We created a questions array with all of our question objects inside of it. We're going
to pass that into the run test function and see how we did.
The program we just wrote asked us all the questions, got our input for each
question, and also kept track of our score and printed it out. We can add another
question into here and it would automatically be able to ask it.
I was trying to show how we can use something like a class in order to model a real
world entity. So we created a question class, and then we were able to create a
bunch of different questions, and then we could run the test.

Object Functions
In this tutorial, I will talk about class functions in Python. Class functions modify or
give information about objects.
I created a student class and gave it a name, a major and a GPA. Then I created two
students, Oscar and Phyllis, and gave them all of this information.
We can use functions inside of these class files to determine whether or not a
student has honors, for example, whether or not a student has a GPA of 3.5 or
above to be on the honor roll.
If self.gpa is greater than or equal to 3.5, then we can return true if the student is on
the honor roll, otherwise we can just return false. This function allows the objects of
this class to figure out whether or not the current student is on the honor roll.
A class function is just a little function that can be used by the objects of the class. It
can either give us information about the class or modify information about the class.

Inheritance
In this tutorial I want to talk to you guys about inheritance in Python. I have created a
class called chef that can do three things: make chicken, make salad and make a
special dish.
If you wanted to create a class that modeled a different type of chef, you could
extend the chef class or create a new class that modeled a Chinese chef.
I created a file called Chinese chef dot python, and we can use this file to create a
Chinese chef class that can do everything that our generic chef can do. If we wanted
to give a Chinese chef all the functionality of the normal chef, we could copy all of
the functions from the other chef and paste them in here. The Chinese chef can also
make fried rice and a different special dish.
Alright so now we have our Chinese chef class set up and it can do everything that
the normal chef can do. So let's go ahead and create a Chinese chef object and print
out this special dish.
When I want to use all of the functionality that is inside of this chef class, I have to
copy and physically paste all of these functions down into this file. To avoid having to
copy and paste all of these functions, I can just inherit these functions from that chef
class.
This is saying that I want to be able to use all of the functions inside of the chef
class. By using inheritance, I can still run this program and have the Chinese chef
make chicken even though I didn't write out the make chicken function in this app.py
file. I can also override the make special dish function to make the Chinese chef
make orange chicken.
I can inherit functionality from an existing class into a new class, so the Chinese chef
class can make chicken and salad.

Python Interpreter
In this tutorial I want to talk to you guys about the Python interpreter, which is
basically a little environment that we can use to execute Python commands. You can
use the Python interpreter by opening up your command prompt.
The terminal is a place on our computer where we can interact with the computer
using text commands. In the terminal we can use the Python interpreter, which is
basically just a little program that we can write sort of Python in.
On Windows you may run into a problem where you can't use this Python 3
command. If that's the case, just Google how to add Python 3 to your windows
path variable and you should be able to find an answer.
For example, I could write print and inside of here I could print out hello world, or I
could create a variable and then print out num one plus num two.
I could also use some like a function in here, so I could say print hello plus name and
we'll say hi to me. Mike is going to say hello to Mike, and then you can use all
the basic Python commands inside of this Python interpreter. This is not a place
where you want to write serious Python scripts, so I would not recommend doing it
inside of this interpreter.
If you're writing an actual program, you should use a text editor, but the Python
interpreter is awesome for testing out little bits of code without having to set up some
huge environment.
If you enjoyed the video please leave a like and subscribe to draft Academy. Also if
you have any constructive criticism or questions please leave a comment below.

You might also like