Full transcript
0:00hello everyone in this video i want to
0:02provide an introduction to the python
0:05programming language
0:06this video assumes that you have python
0:09installed on your machine and you have a
0:11text editor of your choice that you can
0:13use to edit that python code
0:16for this video i'll be using visual
0:18studio code or vs code
0:21this is what vs code looks like
0:24i'll show you how you can run your
0:25python code in bs code and also how you
0:28can run your python code from command
0:30line
0:30so for that you'll need a terminal or a
0:34power shell or whatever your favorite
0:36version of command line is
0:38so let's get started by first making a
0:41directory in which we can put our python
0:44scripts
0:45i'm going to put this directory on my
0:47desktop you can put it wherever you
0:49would like
0:50so i'm going to make a new folder called
0:52python basics fun
0:55in which we'll start going through some
0:57basics of python
1:00the basics that we'll cover in this
1:01video include
1:03comments variables and operators
1:08throughout the demo i'll be using the
1:09print statement but stay tuned for a
1:12future video where i'll go through some
1:14more details on how to get the most out
1:16of using print and also how to get input
1:18from the user so that'll be a input
1:21output video
1:23now let's head over to vs code
1:27and open this folder so a few ways to do
1:30it
1:30in the file explorer on the left side
1:32here you can do open folder
1:35under get started if you see this kind
1:37of welcome page you can click open or
1:40you can go file open folder
1:44i'm going to navigate to my desktop open
1:47python basics fun
1:49this will reload bs code
1:52now the current working directory also
1:55known as the cwd
1:56is python basics fun in vs code so if
1:59you look in my file explorer on the
2:01left-hand panel of vs code the top level
2:04directory that i can see is python
2:05basics fun anything that i create in
2:09this instance of vs code will be created
2:12in python basics fun
2:14so let's create our first python file
2:16python files have the dot py extension
2:19for the base file name we can name it
2:22really whatever we want
2:24i'll use main as the name of our first
2:26script
2:27so let's do
2:28new file little new file icon right here
2:32or new file right here and name our new
2:34file main.py
2:37this will open main.py in the text
2:40editor
2:43and now we can start writing some python
2:45code
2:46sticking with tradition
2:48let's write print hello world
2:52often when getting started with a new
2:54programming language or a new
2:55environment you try to get what's called
2:56a hello world up and running which is a
2:58very simple program that just prints
3:00hello world to the console or the
3:03command line
3:05in bs code whenever you have unsaved
3:07changes you'll see a black circle where
3:11the x normally is
3:13so let's make sure that
3:14we save this file
3:16use your keyboard shortcut
3:18ctrl s or command s for windows or mac
3:21or you can always go file save
3:25now we're ready to try running our
3:27simple main dot py
3:30so let me show you two ways to do this
3:32one is in vs code
3:35if you have main.py open and you see a
3:38play button here which says run python
3:40file on the tooltip when i hover over it
3:43you can simply
3:44run this python file by clicking the
3:46play button
3:48and you should see hello rolled in the
3:49output
3:51if you don't see the play button here
3:53i recommend you head over to the
3:55extensions side panel and search for the
3:58python extension and install the one
4:01from microsoft
4:03then reload vs code and try again
4:06another way that you can run main.py
4:10in vs code is using the integrated
4:12terminal if you don't see the integrated
4:15terminal an easy way to open it up
4:18is to go view
4:20terminal
4:21and that will hide or show your terminal
4:28to run main.py from command line
4:31at your terminal simply type python and
4:34then the name of your script that you
4:37want to run
4:38so for us that's python main.py and you
4:41should see hello world
4:43if you installed python and you're
4:45getting an error here saying that python
4:48is not recognized perhaps you need to
4:51add python to your environment variable
4:54which would be your path environment
4:56variable
4:57please take a look at another video or
5:00do some research online to find a nice
5:02tutorial to show you how to do this
5:06now that we can run
5:07our python file in bs code
5:10let me state that if you're not using vs
5:12code and you want to run your python
5:14file you could simply type this same
5:16command at your mac terminal or at your
5:20windows powershell or your windows
5:22command prompt or whatever you like to
5:25use for your command line interface
5:30now that we can run python code
5:32let's get started learning the basics of
5:34python
5:38so let's start on the first line of
5:40main.py
5:42with a pound sign aka a hashtag symbol
5:48this is a one line
5:50comment
5:52so a comment
5:54is code which i'm putting in double
5:56quotes and i'll explain why
5:58is code
6:00that python ignores this means it's code
6:04because it's part of a coding source
6:06file but it's not code in that it has to
6:08adhere strictly to the python syntax
6:10because python ignores comments we can
6:13write english sentence structure code
6:16whatever we want to write here
6:19so therefore comments are ignored by
6:21python but they're still part of our
6:22code base they just don't execute
6:26the purpose of comments is to
6:28document our code
6:33we might use them to describe uh
6:36something key that we're doing in the
6:38code something
6:39not obvious or non-trivial we might use
6:41a comment to cite a source by pasting a
6:44url
6:45etc
6:46what i'll use comments for in my videos
6:49is to
6:50document or explain what i'm doing and
6:53why i'm doing it
6:54so now that we've learned about comments
6:56i can start using them to describe other
6:58aspects of python that we're going to
7:00learn
7:02there's another type of comment that i'd
7:03like to show you
7:06which is called a multi-line comment or
7:09a block comment
7:11so if we have a pair of triple double
7:14quotes so double quote double quote
7:16double quote and then enter and then
7:18double quote double quote double quote
7:21this is a multi
7:24line
7:26comment aka a block comment
7:29so with the pound sign
7:31we only have a comment that spans one
7:33row but if our comment is long as is the
7:36case often when we're documenting say
7:38something like a function or a class
7:41then we'll use a multi-line comment aka
7:45block comment so that our comment can
7:47span multiple lines more easily
7:53all right
7:55let's start with our first topic
7:59section header
8:00variables
8:02variables are fundamental when getting
8:03started with programming languages
8:07a variable stores a value
8:11a variable has a name that is convenient
8:13for us as the programmer to use when we
8:15want to refer to that value
8:17so let's start with a simple variable
8:19declaration let's say with variable x
8:22which is assigned 5.
8:26i highly recommend you train your brain
8:28to read this as x is assigned five
8:33or x stores five
8:36often times when we see this
8:40equal sign here we think equality x
8:42equals five that's not what this python
8:45statement is saying
8:51there actually is an equality operator
8:53in python which is two equal signs
8:58and this is what we use to actually test
9:00if x equals five
9:02this here is a storage operation we're
9:04saying put this value five in this
9:08variable and if i ever want to refer to
9:105 again or overwrite it i'll use x to do
9:13so
9:14so x is assigned 5.
9:16now we can access
9:18x by simply say
9:21printing it out we just use x
9:24so i'll run this
9:26get a little bit of room down here
9:28and i see
9:305 displayed from line 16 and i still see
9:33hello world displayed from line 18.
9:36let's talk some more about
9:39variables
9:42a variable has a
9:44data type
9:47the data type defines the range of
9:50values which can be stored in the
9:53variable
9:54so let's take a look at an example
10:00the into data type
10:01so into short for integer
10:05which means that an end variable
10:11can store
10:14whole numbers positive negative or zero
10:19since we assigned x
10:215 5 is a whole number
10:24x is currently storing an integer value
10:26we would say x is an int variable
10:28we can confirm the type of x by using a
10:32built-in function called type it's built
10:34in which means we don't need to import
10:37another python module
10:39in order to use it
10:40so let's see what the type of x is
10:43if i print this out i see that
10:46x
10:47is class end and int is short for
10:50integer
10:53i'm going to move my hello world back up
10:56here to the top
10:58just so that my most recent output is
11:00always going to be at the bottom
11:04now
11:05let's overwrite
11:06x with a new value
11:09let's say that we don't want x to store
11:115 anymore we want it to store 5.5 which
11:15is not a whole number and therefore is
11:17not an integer
11:18if you're coming from another
11:19programming language where you declare
11:21the type at the time you declare a
11:23variable you're probably like what i can
11:26change the type of my variable i can say
11:29it's no longer storing an end it's now
11:30going to store a floating point number
11:33and yes you can in python so let's see
11:35it in action let's say x is now a sine
11:405.7 i'll print out x
11:43i'll print out the type of x
11:46and we'll see
11:48that x is now a float
11:51so now we're seeing another data type in
11:53python
12:01so float is short
12:03for
12:04floating point number
12:08so this would be
12:11a data type that defines values that
12:14have
12:15a fractional part or set in other way
12:17decimal numbers
12:21so a float variable
12:23can store
12:24decimal numbers
12:26or numbers with a fractional part
12:31in this case 0.7 is the fractional part
12:38let's assign x again this time i'm going
12:40to assign x what's called a string
12:43so a string is a sequence of characters
12:50in python the string data type is
12:53represented by str or stir
12:59and a string variable
13:02can store a sequence of characters
13:10so let's print out x and then let's
13:12print out the type of x
13:17and there we see hello printed to the
13:18console and we see stir short for string
13:23now let's put this all together and
13:25let's say we assign to x
13:285 but not the int 5
13:31the string 5.
13:33when i print out x
13:36i see five and this looks just like the
13:39output i had earlier from line number
13:4317 when i print out x the integer
13:46so the output looks the same
13:48but we know now
13:50that the value is being stored as a
13:52different type
13:54previously on line number 17 we're
13:56printing out x where x is storing five
13:58as an integer and now on line 38 x is
14:01storing five the character as a string
14:04so this is why type can come in handy
14:08if you're ever unsure of what the data
14:10type of your variable or a value is
14:16all right
14:17let's move on to our last big topic for
14:19this video which is operators we've
14:21covered comments we've covered variables
14:24now let's cover operators
14:27hopefully you recall
14:29pemdas
14:30this is an acronym for remembering the
14:32order of operations
14:34this order of operations parentheses
14:37exponents multiplication division
14:39addition and subtraction applies in
14:41python as well plus there are some
14:43additional operators in python beyond
14:46pemdas but let's start with the basics
14:48here
14:49so parentheses
14:51exponents
14:53multiplication
14:54division
14:56addition and subtraction
15:00so parentheses have the highest order of
15:03precedence in this list of
15:06six different operators
15:08so parentheses in python
15:11are the same as you're used to
15:13from say
15:14typing them in word or typing them in
15:16another language or writing them in
15:18algebra
15:21now let's do
15:22exponentiation
15:25so a double star is the exponentiation
15:28operator
15:31in python
15:32so let's say i want to raise
15:352 to
15:37the third which would be
15:40two times two times two
15:44two times two is four times two is eight
15:47so i run this
15:49and i see eight
15:52this is a good opportunity here to see
15:54another way to do exponentiation that
15:57uses
15:59the math module
16:01so a python file
16:04is also
16:06known
16:08as
16:09a module
16:11and we can use
16:15standard modules
16:17that are included
16:19with python
16:21without having to install any additional
16:24libraries or packages to do so one of
16:26the standard modules or libraries that
16:29we can use in python is the math module
16:33so let's use the math module
16:36to see another way
16:38to compute
16:41an exponent
16:43we'll need to import the math module in
16:46order to gain access to code written in
16:49the math module
16:50so let's scroll all the way up to the
16:52top of our file
16:56and let's write import math
17:00so by convention
17:02you put all of your import statements
17:04at the top
17:10you don't have to do this because python
17:12executes a module or a python file from
17:15top to bottom so as long as your import
17:18statement is before the line of code
17:21that uses the module you're importing
17:24your code will compile and run just fine
17:26but by convention it's good to put all
17:28your import statements at the top so
17:30that it's really easy to find what
17:32dependencies or what modules are being
17:35used by this file
17:37so the very top i'm going to import math
17:42this is going to allow my main dot py
17:45module to access code written
17:47by awesome python developers in the math
17:50module
17:52so let's do the same thing we did on
17:54line 47
17:55but using the math module
17:57so i can do math dot and i can see all
18:01of the functions and variables that we
18:03have access to from the math module
18:06so some fun ones like
18:08seal
18:09or cosine or degrees
18:13or absolute value floor et cetera
18:16what i'm interested in is math.pow which
18:20will compute x to the power of y
18:22so i'm going to pass in 2 comma 3
18:26and we should see the same result
18:28which we do
18:308.0 coming from line 51 but it's
18:33slightly different because if you see
18:37our previous result from line number 47
18:39using the exponentiation operator it was
18:42an integer there's no point on the end
18:45but the result from line 51 using the
18:47math module is a floating point number
18:49because there is a point on the n
18:51so just something to note about the
18:53difference between these two ways of
18:56computing exponentiation
18:58when both your operands for the
19:00exponentiation operator or ins you'll
19:02get an int
19:03with math.pow even if both arguments to
19:06the pow function are integers you'll get
19:09a floating point number
19:12i'll mention too that what we're doing
19:13here with callingmath.pow
19:16involves knowledge of functions and
19:18arguments
19:19if you're not familiar with functions
19:20and arguments stay tuned for another
19:22video where i'll go through the basics
19:24of them in python
19:28all right let's move on now to
19:29multiplication
19:31so a single star
19:33is multiplication
19:36so maybe i'll print out four times five
19:39and i accept expect to see 20
19:41which i do
19:43let's move on to division
19:45there are two types of division in
19:47python that you should know about
19:49the first is a single forward slash
19:52which is floating point division
19:55and what we think of as
19:58the normal division
20:00so let me give you an example
20:02if i were to say print 2
20:04or let's do 5 divided by 2
20:07okay you would say 2 goes into 5 evenly
20:102 times with a remainder of 1
20:131 over 2 is a half so the result is 2
20:16and a half
20:18and i print this and i see two and a
20:19half i'm like okay that makes sense
20:22well there's also an operator that is
20:25two forward slashes
20:28which is integer division
20:30so integer division
20:33is
20:34the whole number result
20:37of floating point division
20:39so if we look at the result of floating
20:41point division the whole number is two
20:44two goes into five evenly two times so
20:48let's try printing out five integer
20:50divide two
20:52and we get the result of two
20:54another way to think about integer
20:56division is you take the result of
20:58floating point division and you just
21:00chop off or just truncate off the
21:02fractional part the 0.5 and what you're
21:05left with is the result
21:10there's one more operator here worth
21:12mentioning which is the mod operator
21:18so the mod operator
21:20is the remainder
21:24from integer
21:26division
21:27so if i do print five mod two
21:32two goes into five evenly two times with
21:34remainder one
21:37i will see one printed
21:40so floating point division integer
21:42division mod and multiplication all have
21:46the same order of precedence you would
21:48evaluate them from left to right
21:52i'll mention that exponents
21:54are evaluated from right to left so that
21:57is something a little different to keep
21:58in mind
22:00i won't do demos for addition and
22:01subtraction because i'm sure you're
22:04familiar with the plus and minus signs
22:06from your algebra days and whatnot
22:09they work as you would expect in python
22:11and they are truly the plus and minus
22:13signs
22:16all right
22:17that's a wrap up of this video we
22:19covered commons variables operators and
22:22how to run python code in vs code and
22:26how to run it in
22:28the terminal or at your command line
22:30stay tuned for the next few python
22:33videos which will continue with
22:35important topics when getting started
22:36with python like
22:38getting input from the user conditionals
22:41loops functions lists etc
22:44lots to learn python is a great language
22:46i hope you're enjoying it so far
22:49thanks for watching and please consider
22:51subscribing to my channel