Learn Python - Full Course For Beginners (Tutorial-WT - Summaries
Learn Python - Full Course For Beginners (Tutorial-WT - Summaries
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.
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.
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.
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.
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.
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.