This tutorial provides a comprehensive introduction to Python programming, covering fundamental concepts from installation and basic syntax to data types, control flow, and data structures, designed for absolute beginners aiming for applications in data science, machine learning, or web development.
Mind Map
点击展开
点击探索完整互动思维导图
in this python tutorial you're going to
learn everything you need to know to
start programming in python if you want
to learn python programming for data
science machine learning or web
development this python tutorial is the
perfect place to learn python you don't
need any prior knowledge in python or
programming in general i'm going to
teach you everything from scratch i'm
mosh hamadani and i've taught millions
of people how to code through this
channel if you're new here make sure to
subscribe as i upload new videos every
week now let's jump in and get started
all right before we get started let me
give you some ideas about what you can
do with python that's a very common
question python is a multi-purpose
programming language so you can use it
for a variety of different tasks you can
use python for machine learning and ai
in fact python is the number one
language for machine learning and data
science projects python is also very
popular in web development using python
and a framework called django you can
build amazing websites here are five
websites powered with python and django
youtube instagram spotify dropbox and
pinterest you can also use python in
automation with python you can save your
time and increase your productivity by
automating repetitive tasks so why are
you learning python are you learning it
for automation for data science or web
development let me know in the comment
all right the first thing i want you to
do is to head over to python.org to
download the latest version of python so
you go to downloads and select the
here in your downloads folder you should
see this package simply double click it
you're going to see this python
installer if you're on windows you will
see this checkbox over here add python
to path make sure to check it it's
really important otherwise you're not
going to be able to follow this tutorial
simply click on continue again
again
one more time
i agree with the terms
and install the latest version of python
now here you need to enter the username
password of your computer so
so
next you need to install a code editor
we use a code editor to write our code
and execute it the most popular code
editor for python is pycharm you can get
it from jetbrains.com pycharm
pycharm
so on this page
click on download
you should see two different editions
one is the professional edition which is
commercial and we also have this
community edition which is free and open
source so we're going to download the
now in your downloads folder you should
if you're on windows you're going to see
an installation wizard so simply click
on the next button until you install
pycharm if you're on a mac you need to
drag this pycharm and drop it onto the
now
let's open it
the first time you open pycharm you have
to configure a few settings we don't
want to spend time on this so over here
we're going to click on skip remaining
and set defaults
now let's create a new project
over here we can specify the location
and the name of our python project so
let's append hello world to this path
this is where our python project is
going to be saved so let's click on create
in this window you can see the content
of our project so here's our hello world
project currently we have only one
folder inside this project that is vn
which is short for virtual environment
we'll talk about virtual environments in
the future so currently we don't have
any python files inside this project a
real application can consist of tens or
hundreds or even thousands of python
files so let's right click on the
project name
and go to new python file we're going to
now we can collapse this project window
by clicking on this icon so now we have
more space let's write our first python
code we're going to write print all in
lowercase then add parentheses
then add quotes either single quotes or
double quotes
and inside this code we're going to
write hello world
so this is what we call a string a
string means a string or sequence of
characters in simple words that means
textual data so in python and in many
other programming languages whenever
we're dealing with textual data we
should always surround our text with quotes
quotes
in python we can use single or double quotes
quotes
now this print you see here is a
function built into python and we can
use it to print a message on our
application window so let me show you
how to run this code
on the top we go to the run menu and
then select run
note that there is a shortcut associated
with this command i always use shortcuts
because they increase my productivity so
let's click on this
now select app
and over here
you can see this little window this is
what we call the terminal window and it
shows the output of our program
so here's the hello world message
printed in the terminal window now as
you learn more python you will learn how
to build applications that have a
graphical user interface that's an
advanced topic so for now let's not
alright now let's talk about variables
we use variables to temporarily store
data in a computer's memory for example
we can store the price of a product or
someone's name their email their age and
so on let me show you so
to declare a variable we start by typing
a name for that variable let's say age
then we add an equal sign
and then we type a value let's say 20.
so with this we're storing the number 20
somewhere in our computer's memory and
we're attaching this age as a label for
that memory location so now we can read
the value at this memory location and
print it on the terminal so instead of
printing hello world we want to print
the value of the age variable
so i'm going to delete what we have
inside parenthesis
and type age note that i'm not adding
quotes because if i run this program
we'll see the text h on the terminal we
don't want that we want the value of the
age variable so let's remove the quote and
and
print the value of the age variable
now here on the toolbar you can click on
this play icon to run your program or
you can use the shortcut that i showed
you in the last video so the shortcut is
over here on a mac that's ctrl shift and r
r so
so
there you go
now you can see the value of the age variable
variable
now we can also change the value of a
variable for example on line 2
we can set 8 to 30.
now when we run our program
we see 30. so as you can see our program
gets executed from top to bottom
so this is how we can declare and use a
variable now let's look at a few more examples
examples
so i'm going to declare
another variable called price
and set it to 19.95
so in python we can use numbers with a
decimal point or
whole numbers we can also declare a
variable and assign it a string value so
let's say first underline name so if you
want to use multiple words in the name
of a variable we should separate them
using an underscore this makes our code
more readable see what would happen if i
didn't use this underline
this is not easily readable so we always
separate multiple words by an underscore
now we set this to a string so we can
use single quotes or
double quotes let's say march
we also have a special type of value
called a boolean value which can be true
or false that is like yes or no in
english let me show you so i'm going to
declare another variable called is
online and set it to true
we could also set it to false what we
have here is called a boolean value
now note that python is a case sensitive
language so it's sensitive to lowercase
and uppercase letters in this case if i
use a lowercase f
we can see an error over here because
this is not recognized in python
so false with a capital f is a special
keyword in python that represents the
boolean false value
so this is how we can declare and use
variables in python
all right now here's a little exercise
for you
imagine we want to write a program for a
hospital so we're going to check in a
patient named john smith he's 20 years
old and is a new patient i want you to
declare a few variables to store these
values use the comment box below to
in this tutorial i'm going to show you
how to receive input from the user so in
python we have another built-in function
called input we use this to read a value
from the terminal window let me show you
so we add parenthesis
then we type in a string here we can
type a message like what is your name we
had a question mark followed by a space
you will see why we need this space in a
second so let's run this program
we get this message now we have to enter
a value so we click over here
now you can see that the carrot is
separated from the question mark this is
because of the white space that we added
over here
so now we have to type a value let's say
john when we press enter
this function will return the value that
we entered in the terminal window so we
can get that value and store it in a
variable so let's declare a variable
called name and set it to the return
value of the input function
now we can print a greeting message for
this user so we use the print function
we say hello
we had a space
now after the string we want to add the
value of the name variable so we use a
plus sign
and then type name
what we are doing here is called string
concatenation so we're combining this
string with another string
now let's run our program and see what
happens so what is your name mosh
mosh now
now
we get this message hello mosh
so this is how we can use the input
you'll learn about the three types of
data in python we have numbers
strings and booleans now there are times
you want to convert the value of a
variable from one type to another let me
show you so we're going to use our input function
function
to read the user's birth year so enter
your birth here
now this input function is going to
return a value so we can store it in a
variable called
birth underline year okay
okay
now let's write code to calculate the
age of this user so we write an
expression like this currently we are in
the year 2020 so 2020 minus
birth year
this expression or piece of code is
going to produce a value so once again
we can store that value in a variable
let's call that variable age
now let's print age
on the terminal
let's run our program and see what happens
happens so
so
my birth year is 1982.
enter oops our program crashed so
whenever you see this red message that
indicates an error so this error
occurred in this file
that is our app.pi on line two
right below that you can see the piece
of code that generated this error
so that is this expression 2020 minus
birth year now below that you can see
the type of error so here we have
unsupported types for subtraction
we have int
and stir what are these well this end is
short for integer and that represents a
whole number in programming so
2020 is an example of an integer
now birth year is an example of a string
because whenever we call the input
function this function would return a
value as a string even if we enter a
number in other words when i entered
1982 this input function returned a
string with these characters 1982 so
this string is different from the number
1982 they're completely different types
so in this case
let me delete these lines
the reason we got this error is that we
try to subtract a string from an integer
so our code looks like this 1982
1982
now python doesn't know how to subtract
a string from an integer
so to solve this problem we need to
convert this string to an integer
now in python we have a bunch of
built-in functions for converting the
types of our variables so
we have this end function
we can pass our burst here to it
and this will return the new numeric
representation of the birth year
so to solve this problem
we need to replace
the string
with the end function
so let's see what's going on here on the
first line we call the input function
this returns a string
on the second line we pass the string to
our end function the in function will
return the numeric representation of the
burst year then
then
we subtract it from 2020 we get the age
and store it in the age variable now
let's run our program so
so 1982
1982
and there you go i'm 38 years old
so this is how the in function works now
we also have another built-in function
called float that is for converting a
value to a floating point number a
floating point number in python and
other programming languages is a number
with a decimal point so
10 is an integer and 10.1 is a float
so we have int we have float and we also
have bool for converting a value to a boolean
boolean
and finally we have stir for converting
a value to a string so these are the
built-in functions for converting the
type of our variables
now here's a little exercise for you i
want you to write a basic calculator
program so here we have to enter two
numbers we can type a whole number or a
number with a decimal point
and then our program will print the sum
of these two numbers
so pause the video spend two minutes on
all right first we're going to call our
input function to read the first number
we get the result and store it in a
variable called first
now let's declare
another variable called second and read
the second number now
now
we calculate the sum
so that is first plus second
now let's see what happens when we print
sum on the terminal
so i enter 10 and 20
but instead of 30 we get 10 20. this is
because we're combining or concatenating
two strings so
as i told you before the input function
returns a string so
this line will be equivalent to first
equals 10. we're dealing with a string
not an integer similarly
similarly
second is going to be
20 as a string so when we combine two
strings 10 plus 20 will get
10 20 because we're dealing with textual
data okay so to solve this problem
we need to convert the values we read to
their numeric representation so over here
here
we're going to pass
first to our int function
and here as well
now let's run our program
so we enter 10 and 20 we get 30. what if
we enter a floating point number so 10.1
and 20.
we got an error
so to solve this problem
we need to treat both these values as
floats so instead of the in function
we're going to use the float function
now let's run our program one more time
we enter a whole number and a floating
point number so the result is correct
now let's add a label over here so sum is
is
plus sum
let's run our program
one more time 10 and 20.
once again we got an error the error is
saying that python can only concatenate
strings not floats to strings
so on line four we have a string we're
concatenating this with a float because
the result of this expression is a
floating point number we're adding two
floats so the result is a float as well
so python doesn't know how to evaluate
code like this
it doesn't know how to concatenate a float
float
to a string to solve this problem
we need to convert sum to your string so
this is where
we use the stir function
now let's run the program again so
10 plus 20.1 and here's the result
and one last thing
in this example i'm calling the float
function at the time we want to
calculate the sum of these two numbers
but this is not a requirement we can
call the float function
over here so this input function returns
a string we can pass that string to our
float function take a look so float
parenthesis like this
so the value that we're passing to the
float function is the value that is
returned from the input function similarly
similarly
we call the float function over here now
now
we can change this expression to first
plus second that is another way to write
this piece of code
so type conversion is important in
python and other programming languages
there are times you need to convert the
in this tutorial i'm going to show you a
bunch of cool things you can do with
strings in python so let's start by
declaring a variable called course
and set it to python for beginners
beginners
now this string that we have over here
is technically an object an object in
python is like an object in the real
world as a metaphor think of the remote
control of your tv this remote control
is an object and it has a bunch of
capabilities it has a bunch of buttons
for turning your tv on turning it off
changing the volume and so on now in
this program this course variable is
storing a string object this string
object has a bunch of capabilities so if
we type
course dot you can see all the
capabilities available in a string
object these are basically functions
that you can call just like the print or
input functions the difference is that
the print and input functions are
general purpose functions they don't
belong to a particular object but the
functions you see over here are specific
to strings now more accurately we refer
to these as methods so when a function
is part of an object we refer to that
function as a method
so let's look at a few examples here we
have a function or a method called upper
and we use that to convert a string to
uppercase so if we print course.upper
course.upper
and run this program they can see our
course in uppercase
pretty useful
now what you need to understand here is
that this upper method does not change
our original string it will return a new
string so right after this if we print course
course
you can see that our course variable is
not affected so the upper method returns
a new string
now similarly we have another method
called lower for converting a string to
lowercase we have a method called find
to see if our string contains a
character or a sequence of characters
for example here we can pass y
y
and this will return the index of the
first occurrence of y in our string so
in python the index of the first
character in a string is 0. so here we
have 0 1 2 3 4 and so on so when we run
this program you're going to see one on
the terminal because the index of y is
1. take a look first i'm going to delete
this line we don't need it anymore also
let's do this line let's run the program
now as i told you before python is
sensitive to lowercase and uppercase
letters so if i pass an uppercase y here
this find method returns negative 1
because we don't have an uppercase y in
this string we can also pass a sequence
of characters for example 4. so this
will return the index of the word 4.
take a look
so it's 7.
now there are times we want to replace
something in a string with something else
else
to do that we use the replace method replace
replace
so we can replace 4 with
with
a string containing the number 4. take a look
look
so python for beginners obviously
obviously
if you look for a character or a
sequence of characters that don't exist
in our string nothing is going to happen
for example if we try to replace x with
4 obviously we don't have x here so
nothing is going to happen
also just like the upper method the
replace method is not going to modify
our original string so it's going to
return a new string this is because
strings in python and many other
programming languages are immutable we
cannot change them once we create them
whenever we want to change your string
we'll end up with a new string object in memory
memory
now one last thing i want to cover in
this tutorial there are times you want
to see if your string contains a
character or a sequence of characters
one way to do that is using the find
method that we talked about so let's see
if our string
contains python
now when we run this program
that is the index of the first
occurrence of the word python in our
string now in python we can also use the
in operator so we can write an
expression like this
we type a string
python then we type in this is a special
keyword in python this is what we call
the in operator
so after that we type the name of our variable
variable
so with this expression we're checking
to see if we have python in course as
you can see python code is very readable
it's like plain english so when we run
this program
instead of seeing the index of the first
occurrence of python we see a boolean
value this is more desirable in a lot of cases
cases
next we're going to look at arithmetic operations
in this tutorial i'm going to show you
the arithmetic operators that we have in
python these are the same arithmetic
operators that we have in math for
example we can add numbers we can
subtract them multiply them and so on so
let's print
10 plus 3. let me run this program we
have 13.
so this is the addition operator we also
have subtraction
we have multiplication
and division now technically we have two
different types of division operators we
have a division with one slash and
another with two slashes let's look at
the differences if you use a single slash
slash
we get a floating point number that is a
number with a decimal point but if we
use double slashes
we get
an integer a whole number we also have
the modulus operator that is indicated
by a percent sign and this returns the
remainder of the division of ten by
three so
that is one and finally we have the
exponent operator that is indicated by
two asterisks so this is 10 to the power
of three so when we run this we get a thousand
thousand
now for all these operators that you saw
we have an augmented assignment operator
let me explain what it means so let's
say we have a variable called x
and we set it to 10.
now we want to increment the value of x
by 3. so we have to write code like this x
x
equals x plus 3. when python executes
this code it's going to evaluate this
expression or this piece of code the
result of this expression is 10 plus 3
which is 13. then it will store 13 in
the x
now there is another way to achieve the
same result using less code we can type
x plus equal three
what we have on line three is exactly
identical to what we have on line two so
what we have here is called the
augmented assignment operator so we have
this assignment operator but we have
augmented or enhanced it
now here we can also use
subtraction to decrease the value of x
by 3 we can use multiplication and so on
so these are the arithmetic operators in python
all right let me ask you a question i'm
going to declare a variable called x and
set it to 10 plus 3 times 2. what do you
think is the result of this expression
this is a basic math question that
unfortunately a lot of people fail to answer
answer
the answer is 16. here's the reason in
math we have this concept called
operator precedence and that determines
the order in which these operators are
applied so multiplication and division
have a higher order so this part of the
expression gets evaluated first so 2
times 3 is 6
and then the result is added to 10. that
is why the result of this expression is
16. now in python operator precedence is
exactly like math but we can always
change it using parenthesis for example
in this expression if you want 10 plus 3
to be evaluated first we can wrap it in parenthesis