Docsity

Learn Python from Scratch for Data Science, ML, and Web Dev: Python Tutorial, Summaries of Computer science

Toggle sidebar
bg1
In this Python tutorial, you're gonna 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
much Hamadani. And I've talked millions of people have to go 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.
Alright, 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 multipurpose 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 section below.
Alright, 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 latest version of Python. 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, one more time, I agree with the terms and install the latest version of Python. Now
here you need to enter the username and password of your computer. So let's do that real quick.
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 pie chart, you can get it from jetbrains.com/pie chart. 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
you're going to download the Community Edition. Now in your downloads folder, you should have
this package. Let's double click it. If you're on Windows, you're going to see an installation wizard. So
simply click on the Next button until you install PI charm. If you're on a Mac, you need to drag this pi
charm and drop it onto the Applications folder. Now, let's open it. The first time you open pi charm,
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. And this window you can see
the content of our project. So here's our Hello word project. Currently, we have only one folder
inside this project that is Vi n, 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. It real
application can consist of 10s, or hundreds or even 1000s of Python files. So let's right click on the
project name and go to new Python file. We're going to call this file app.
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 parenthesis that
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. In Python, we can
use single or double quotes. Now this print you'll 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
pf3
pf4
pf5

Partial preview of the text

Download Learn Python from Scratch for Data Science, ML, and Web Dev: Python Tutorial and more Summaries Computer science in PDF only on Docsity!

In this Python tutorial, you're gonna 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 much Hamadani. And I've talked millions of people have to go 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. Alright, 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 multipurpose 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 section below. Alright, 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 latest version of Python. 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, one more time, I agree with the terms and install the latest version of Python. Now here you need to enter the username and password of your computer. So let's do that real quick. 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 pie chart, you can get it from jetbrains.com/pie chart. 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 you're going to download the Community Edition. Now in your downloads folder, you should have this package. Let's double click it. If you're on Windows, you're going to see an installation wizard. So simply click on the Next button until you install PI charm. If you're on a Mac, you need to drag this pi charm and drop it onto the Applications folder. Now, let's open it. The first time you open pi charm, 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. And this window you can see the content of our project. So here's our Hello word project. Currently, we have only one folder inside this project that is Vi n, 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. It real application can consist of 10s, or hundreds or even 1000s of Python files. So let's right click on the project name and go to new Python file. We're going to call this file app. 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 parenthesis that 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. In Python, we can use single or double quotes. Now this print you'll 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 so 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 worry about it. Now let's talk about variables will 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. Notice I'm not adding quotes, because if I run this program, we'll see the text age on the terminal, we don't want that we want the value of the age variable. So let's remove the quotes 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 N r. So there you go. Now you can see the value of the age variable. Now we can also change the value of a variable. For example, on line two, we can set eight to 30. Now when we run our program, C 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. So I'm going to declare another variable called price and set it to 19. Point 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. That's a mash. 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. 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, you 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 represent the Boolean false value. So this is how we can declare and use variables in Python. Alright, 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 as a new patient, I want you to declare a few variables. To store these values. Use the comment box below to share your code with others. 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 a string. Here we can type a message like What is your name, we add 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 caret is separated from the question mark. This is because of the whitespace 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 term Hello 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

So I enter 10, and 20. But instead of 30, we get 1020. 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, you're dealing with a string, not an integer. Similarly, second is going to be 20 as a string. So when we combine two strings, 10 plus 20, will get 1020. Because we're dealing with textual data, okay, so to solve this problem, we need to convert the values parade to their numeric representation. So over here, we're going to pass first to our int function. And here as well, now let's run our program. So reinter, 10, and 20, we get 30. What if you enter a floating point number, so 10.1 and 20, you got an error. So to solve this problem, we need to treat both these values as floats. So instead of int function, we're going to use the float function. Now, let's run our program one more time, we'll 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 plus some, let's run our program. One more time, 10 and 20. Once again, we've got an error. The error is saying that Python can only concatenate strings, not floats to strength. 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 you're adding to float. 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 to string. To solve this problem, we need to convert some to a 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, of calling the float function at a 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, we call the float function over here. 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 type of variable to a different type. 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. 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 this program, this course variable is storing a string object. This string object has a bunch of capabilities. So if we type course.we 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, the upper and run this program, you 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, you can see that our course variable is not affected. So the upper method returns a new string. A 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. 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 zero. So here we have 01234, and so on. So when we run this program, you're going to see one on the terminal, because the index of y is one, take a look. First, I'm going to delete this line, you won't need it anymore. Also, let's do this line. Let's

run a program. There you go. As I told you before, Python is sensitive to lowercase and uppercase letters. So if I pass an uppercase Y here, this fine method returns negative one. Because we don't have an uppercase Y in this string, we can also pass a sequence of characters for example four, so this will return the index of the word four, take a look. So it's seven. Now there are times we want to replace something in a string with something else. To do that, we use the replace method, replace. So we can replace four with a string containing the number four, take a look. So Python, for beginners. 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 four, 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 a string, we'll end up with a new string object in 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. So in this expression, you're checking to see if you 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. Next, we're gonna 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 three. When we run this program, we have 13. So this is the addition operator. We also have subtraction, we have multiplication, and division. But 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, 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 10 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 1000. Now, for all these operators that you saw, we have an augmented assignment operator. Let me explain what admits. 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 three. So we have to write code like this, x equals x plus three, when Python executes this code is going to evaluate this expression or this piece of code, the result of this expression is 10, plus three, 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. Here, we can also use subtraction. To decrease the value of x by three, we can use multiplication, and so on. So these are the arithmetic operators in python. Let me ask you a question. I'm going to declare a variable called x and set it to 10 plus three times two. 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, 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

of code. So the code that we write over here will be executed if this condition is true. Otherwise, it's not going to be executed. Let me show you.