Learn Python in Only 30 Minutes (Beginner Tutorial)
AI Summary
This video provides a beginner-friendly crash course on Python programming, covering fundamental concepts such as variables, data types, functions, loops, conditionals, exception handling, and imports. The tutorial concludes with building a simple chatbot project to apply the learned concepts.
Use print('hello world') to display text. Quotation marks are needed for strings.
Create a variable name = 'Bob', then use print('Hello ' + name + '!') to concatenate strings.
Python data types include string, integer, float, boolean, tuple (immutable), list (mutable), set (no duplicates), and dictionary (key-value pairs).
Use type constructors like int(), float(), str() to convert between data types. Example: int('100') converts string to integer.
Optional but recommended: age: int = 10. Helps code editors catch errors, but Python ignores them at runtime.
Use f"Name: {name}, Age: {age}" to embed variables directly in strings, avoiding messy concatenation.
Define with def add(a: float, b: float) -> float: return a + b. Functions promote reusability and can have default parameters.
Use for i in range(3): print('hello') for finite loops. Often used with lists: for name in names: print(name).
Use while condition: for infinite loops. Must have a condition that eventually becomes false to avoid infinite execution.
Operators include >, >=, <, <=, ==, !=. Used to compare values and return boolean results.
Control flow: if condition: ... elif condition: ... else: ... . Can be used to create a simple chatbot with user input.
Use try: ... except TypeError as e: ... to handle errors gracefully. Avoid bare except: as it hides all errors.
Import modules like math: import math; math.sqrt(3). Use aliases (import math as m) or specific imports (from math import sqrt).
Build a chatbot that responds to greetings, farewells, and can add two numbers. Uses while loop, if/elif/else, and exception handling.
This crash course covers Python basics and encourages building small projects to solidify learning. The key takeaway is to start coding and use Google to solve problems as they arise.
Clickbait Check
85% Legit"The video delivers a solid beginner tutorial in about 30 minutes, covering core Python concepts as promised."
Mentioned in this Video
Tutorial Checklist
Study Flashcards (14)
What is the correct syntax to print 'Hello World' in Python?
easy
Click to reveal answer
What is the correct syntax to print 'Hello World' in Python?
print('Hello World')
00:43
What are the three main numeric data types in Python?
easy
Click to reveal answer
What are the three main numeric data types in Python?
int (integer), float (decimal), and complex (not covered but exists).
02:33
What is the difference between a list and a tuple?
medium
Click to reveal answer
What is the difference between a list and a tuple?
A list is mutable (can be changed), while a tuple is immutable (cannot be changed after creation).
03:25
How do you convert a string to an integer in Python?
easy
Click to reveal answer
How do you convert a string to an integer in Python?
Use the int() constructor: int('100')
05:03
What is the purpose of type annotations in Python?
medium
Click to reveal answer
What is the purpose of type annotations in Python?
They help code editors and static type checkers catch errors, but Python ignores them at runtime.
06:41
How do you create an f-string that includes a variable named 'name'?
easy
Click to reveal answer
How do you create an f-string that includes a variable named 'name'?
f"Hello {name}"
08:00
What keyword is used to define a function in Python?
easy
Click to reveal answer
What keyword is used to define a function in Python?
def
09:39
How do you create a for loop that iterates 5 times?
easy
Click to reveal answer
How do you create a for loop that iterates 5 times?
for i in range(5):
12:57
What is the main difference between a for loop and a while loop?
medium
Click to reveal answer
What is the main difference between a for loop and a while loop?
For loops are used for finite iteration (over a sequence), while loops are used for indefinite iteration based on a condition.
12:57
Which comparison operator checks if two values are not equal?
easy
Click to reveal answer
Which comparison operator checks if two values are not equal?
!=
16:51
What does the 'elif' keyword stand for?
easy
Click to reveal answer
What does the 'elif' keyword stand for?
Else if
17:18
Why is it bad practice to use a bare 'except:' clause?
hard
Click to reveal answer
Why is it bad practice to use a bare 'except:' clause?
It catches all exceptions silently, hiding errors that should be fixed during development.
21:55
How do you import only the sqrt function from the math module?
medium
Click to reveal answer
How do you import only the sqrt function from the math module?
from math import sqrt
24:16
What method converts a string to lowercase?
easy
Click to reveal answer
What method converts a string to lowercase?
.lower()
25:22
🔥 Best Moments
Infinite While Loop Demo
The presenter demonstrates an infinite while True loop that prints 'hello' forever, then force-stops it, humorously showing the danger of infinite loops.
14:42First Chatbot Creation
In just a few lines, the presenter transforms a simple if/elif/else into a working chatbot using a while loop and input(), making the concept tangible and exciting.
19:00Graceful Error Handling in Chatbot
When a user enters an invalid number for addition, the chatbot catches the ValueError and displays a friendly message instead of crashing, illustrating robust design.
29:08Full Transcript
Download .txt[00:00] This video was brought to you by Indently.io. Learning Python made simple. How's it going everyone? About four years ago I made a Python Clash course which recently blew up and I thought four years is a lot of time so it's time to make an updated course
[00:16] and this video will be useful if you are a beginner. The only two requirements I have for this video is that you have Python installed and you have your own code editor installed. For this tutorial I'm going to be using PyCharm.
[00:28] It's completely free, and you can find it on the JetBrains website. Anyway, let's get started. The very first thing we're going to learn is how to run our very first script. So here we're going to type in one of the most popular commands in any programming language,
[00:43] and that is print hello world. As you can see, I'm using some quotation marks followed by some text, and this is important when you want to insert text into Python. And with print, we can finally run this script by tapping on the green arrow,
[00:57] and you'll see that inside the console we're going to get this as an output. So we essentially told Python to display this information. And we can change this to anything we like. We can even type in hello Bob.
[01:09] And I'm using a shortcut which is command plus R to run my script. If that doesn't work for you, you're going to have to go to settings, then go to the key map and type in run. Then inside here, you'll find a section called run and debug
[01:25] and what you want to select is the run feature and assign it your own shortcut. Anyway, I'm going to be using that shortcut from now on. So here we can change the text to whatever we like. But it would be so much nicer if we could edit this in another place.
[01:39] So what we're going to do next is create a variable. And here we're going to call this variable name. And the name is going to be set to Bob. So now all we need to do is replace this section here with this variable. And to do that, I'm going to remove Bob.
[01:52] I'm going to type in plus name plus exclamation mark. And this is going to perform a string concatenation, which means name is going to be added to hello, and the exclamation mark will be added to name,
[02:04] which means now when we run this, we're going to get hello Bob. And it would be nice if we had a space there. But what's nice about this approach is that if we were to duplicate this line, and we were to change this to James, name will always stay up to date with the name that we defined here.
[02:19] So now it's going to print hello James, hello James, without us having to type in James twice. Variables are great for reusability, and it makes writing codes so much easier. Anyway, moving on, I want to talk about the data types we have in Python,
[02:33] because up until now we've been working with strings, and strings are just text. But we have many other data types, so let's take a look at all of them. So as I mentioned, the first data type we encountered was a string,
[02:45] which is just some text. So for example, here we can have some text, which contains the value of Apple, then a number is referred to as an integer in Python. And that can be any whole number. So 10 or negative 10, that is an integer.
[02:59] If you want to have a decimal, this is referred to as a float. And that's any decimal number. So 10.5 or negative 10.123, that's going to be considered a float.
[03:11] Then we have something called a boolean, which is either true or false. So for example, hasMoney can be set to false with a capital F. And again, this only has two states, false and true.
[03:25] Next, we have something called a tuple. And a tuple looks like this, 2.5, 1.5. It's just a list-like structure, which you cannot change after you create it. So this contains two coordinates, but you can even add more coordinates.
[03:39] You can add, I don't know, 1.0. Now it will contain three coordinates. Then we have a list. And here we're going to create a variable called names, and that's going to contain Agnesa, Bjorn, Benning, and Annie Fried.
[03:51] And to create a list, you just need to use square brackets. And this data is mutable, which means we can remove elements and add elements, unlike with twofolds, where the data is immutable, which means, once again, once we create this, we cannot change it.
[04:05] Next, we have something that's called a set. So here we're going to type in unique. We're going to add 1, 2, 3, 4, 4, 5. Now the reason I call this unique is because a set cannot contain duplicates.
[04:20] I mean, you can insert duplicates, but as soon as you print this to the console, you'll notice that the duplicate of 4 will disappear. So it's another list-like structure that cannot contain duplicates.
[04:32] And finally, we have something that's called a dictionary. And a dictionary is a list-like structure that holds key value pairs. For example, here we might have a user called Bob with the value of 1 or the ID of 1 and James with the ID of 2.
[04:49] As you can see, each element contains a key and a value. So both of these are associated with each other. And that's one element in the dictionary. Sometimes in Python, you're going to be presented with one data type, which you're going to want to convert into another data type.
[05:03] For example, sometimes you're going to try to scrape some information from the internet, and what you're going to get back is, let's say, a number in the form of a string. Now, the problem with this is that you can't use it as a regular integer.
[05:18] With a regular integer, you can do 10 plus 10, for example, and that will give us back 20. But if we were to do 10 plus number, we're going to get an error, because this type can only be added with other strings.
[05:32] So in Python, you can attempt to convert any data type into another data type by using the type constructor. And a type constructor is literally just the type you want to turn it into, followed by a pair of parentheses.
[05:45] So here we're attempting to convert number into an integer. And that's going to work perfectly fine because 100 is actually a number. If we were to type in 10, it's going to give us a value error because 10 is not an integer.
[05:59] It is text that represents the number 10 that only a human can understand. So for this to work, we need to add an actual number. And as I mentioned earlier, you just need to pass in the type you want to convert it to.
[06:13] So it can be a string, or a float, or a set. Whatever data type you want to convert this variable into, you just put the data type in front of it, followed by parentheses. And I mean, you don't have to actually use a variable.
[06:27] you can absolutely type it in directly here, 123.456. And this will convert this string into a float. Now, a very good practice for writing code is using type annotations.
[06:41] This is something I want to teach you as early on as possible, because it's going to save you a lot of trouble in the future. But in Python, it's not required. You can type in something such as age is equal to the integer value of 10.
[06:54] But something you'll see me doing in all of my lessons is annotating it with the data type. Here I'm explicitly telling Python that I want this to be of type integer. And we might even have something called first name, which will be a type string, and that's going to equal Bob.
[07:09] These are type annotations. And they tell the code editor or the static type checker what we're trying to do here. Python is going to ignore these, which means that if we were to type in age of type string equals 10,
[07:22] we're still going to be able to run this code with no problems. But what you're going to notice is that the code editor is going to tell us that we messed up. So this is a tool used for the developer. It tells us when we're making mistakes.
[07:34] Without this type annotation we can add 10 here we can add a string here we can add whatever we want And we not going to get any errors because age can be literally anything But by providing the information that age should be of type integer
[07:48] we will get some warnings that we're doing something silly here, so that we'll have the chance to actually correct it. So you're going to see me using type annotations everywhere. It's not required, but I prefer to do this because I think it's professional
[08:00] and it saves me a lot of effort. Anyway, here I'm going to remove the first part of name, because for the next example, I want to teach you a very convenient concept called fStrings. Because earlier, I showed you that we could do something such as name plus name plus age plus age,
[08:18] and we need to make sure that this is actually a string. So what we have to do here is convert that to a string, and with that, when we run this, we will get some nice output such as this one, name of Bob, age of 10.
[08:31] But this took a lot of effort and is not intuitive. We want to be able to write this in a much more fluent way. And luckily, Python provides us with the opportunity to use F-strings in these situations,
[08:43] which makes creating complex strings such as this one a lot easier. So instead of doing all of that, what we're going to do is print F and quotation marks. And the F here stands for format. An F-string is a formatted string.
[08:57] Also, just to digress real quickly, You're not required to only use single quotation marks, that's just a simple preference of mine. You can also use double quotation marks if you want. That's up to you. I just got accustomed to using single quotation marks because I think it looks cleaner.
[09:11] But now with that, we can type in name, and using curly brackets, we can insert variables directly. So name, and then age, age. This was just so convenient to type compared to the other string that we tried to concatenate.
[09:25] And watch what happens when we run it. it's going to run exactly the same way. Or I mean the output is going to be exactly the same. So I recommend using S-strings whenever you can because this is just hard to keep track of. You won't see many professional Python developers using this.
[09:39] Moving on, it's time we learn about functions. And functions are used to make our code much more reusable, just like with variables. And to create a function in Python, we use the dev keyword,
[09:52] which stands for definition or define. And immediately after that, you add a function. So here we're going to create a function that takes two inputs or two arguments, A and B, and then it's going to add those together.
[10:05] So A is going to be of type float and B is going to be of type float as well. Now using this arrow, we can tell Python what we expect to return. And here we expect to return a float because adding A to B will return a float.
[10:19] Then inside here we can return A plus B. And with that thing done, we can print, add 10 to 15, and we can duplicate this and say 15 to 30.
[10:31] And when we run that, we're going to get the sum of both of those operations. And what's great about this is that we can reuse this function anywhere in our script as many times as we like, and it is as simple as that.
[10:44] Now you might be asking, why don't we just do 10 plus 15 and 15 plus 30? Well, this was perfectly fine, but imagine we want to change something in the implementation of the function. If we want to add some other code in here, we're going to have to do it manually for each one of these.
[11:00] But now let's go back to what we had earlier, and inside the function, what we're going to do is print that we are adding. And I want to make this an fString, a plus b. Here we added a line of code, and this change will be added to each function call.
[11:16] As you can see now we have adding 10 plus 15 and adding 15 plus 30. So any change we bring to the function will be added to each function call, which is very convenient. So let's take a look at another example.
[11:29] And in this example we're going to create a function called greet, which will take a name of type string and a greeting of type string. And this will return none this time because we are only executing code. So print the f string of greetings, name.
[11:45] And what's great about this is that we can type in greet, followed by the name, and the greeting. And now when we run this, we're going to get our simple greeting back. But something else I want to show you is that we can also define default value by using the equal sign directly on the parameter.
[12:01] And this will make it so we don't have to define a greeting each time we use the function. Which means the next time we can type in James, and it won't require us to actually supply a greeting. As you can see, it's going to say hi, James, because hi was the default for greeting.
[12:15] And we can also do that with Bob once again. The default will be used once again because we did not include greeting as an argument. And I don't know if this was obvious or not, but I'm just going to go over it anyway. When you're creating a function, you're not required to add any parameters.
[12:30] You can just say it's a function that executes some code, such as hello. Then you can just call that function as many times as you want, and it will execute that code each time. And once again, type annotations are optional.
[12:43] They do not affect how your code is run, but help the code editor with understanding what you're trying to do. Now that we understand how functions work, let's move on to looping in Python. And in Python, we have two different kinds of loops.
[12:57] One is the for loop, and one is the while loop. For loops are used for finite looping, while while loops are used for infinite looping. So let's take a look at a couple of examples to see the difference and how they actually work.
[13:11] And first, I'm going to start with the for loop. So to create a for loop, we use the for keyword, and here we can add a verbal name, which in general is going to be set to i, if you're just going through a range. So here we're going to type in for i, in range 3, print hello.
[13:28] And range is going to create a range of three numbers, which means it's going to loop three times. And once we run this, you'll notice that it's going to say hello three times. If we change this to five, it's going to loop through the range of five numbers.
[13:42] And it's going to print hello five times. And usually you'll see for loops being used a lot with lists. For example, earlier we had this list of names. With a for loop, we can say for name in names, print hello,
[13:58] and we'll change that to an f string, followed by the name. So this is going to grab each name from that list and use it for each iteration. Which means now when we actually run this, we're going to get hello Agneta, hello Bjorn, hello Benny, and hello Anna Fried.
[14:13] So as you can see, for loops are always used with a finite list of elements. And it doesn't have to be one statement, you can even add two statements, you can say dot dot dot. As you can see now we have two lines of code inside this for loop.
[14:28] And it will be executed four times because this list contains four elements. Anyway, moving on, we have the while loop. As I mentioned earlier, the while loop is infinite, which means if we were to type in while true, print hello,
[14:42] this will be executed for as long as your computer exists. As you can see, there is no end to this condition. True is true forever, which means Python will execute this code over and over again until the end of all things.
[14:55] So right there I force-stopped the script because there's no point in having that run forever. Usually with while loops you're going to have some sort of condition, such as while i is less than 3. And this is a condition which will eventually turn false.
[15:09] We actually need to create that above, so i of type integer will equal 0. Now here we can print i and for each iteration we going to type in i plus 1 or i plus equals 1 So I is going to increment 1 on each loop meaning that one day this is going to become 3 which is not less than 3
[15:28] And once this evaluates to false, it's going to exit out of this while loop. As you can see now when we run this, we're going to have 0 printed, 1 printed, 2 printed, but as soon as i contains the value of 3, this statement, or this expression, evaluates to false,
[15:42] which means the while loop will no longer continue with its loops. Now up next, we're going to talk about these comparison operations, because there are quite a few that you're going to have to memorize.
[15:54] And in this example, I'm going to have two integers, one called a and one called b, and one will contain the value of 1 and the other one the value of 2. And I'm going to type these out real quick because they are self-explanatory. The first check we're going to do is whether a is more than b or greater than b,
[16:09] and we do that using the right arrow. We can also check that A is greater than or equal to B. So if A contains the value of B, it's also going to evaluate to true. Right now, if we were to run this, we're going to get false for both of them because A is not greater than B.
[16:24] But if we add 2 here, the first expression is going to evaluate to false because A is not greater than B. It's exactly the same as B. But with greater than or equal to, this will evaluate to true because A is equal to B.
[16:38] Anyway, this also works in the opposite sense. So you can check that a is less than b, or whether a is less than or equal to b. And that's going to work exactly the same way. Something else you can do is check whether a is equal to b,
[16:51] whether these two contain the exact same value. Right now, if we run this, we're going to get false, because 1 is not equal to 2. But if we insert 2, we will get true as a return. And we can also do the opposite here.
[17:03] We can check that a is not equal to b by using the exclamation mark. And just like that, we're going to get true as an output because A is not equal to B. So these are the comparison operations that you should memorize because you'll be using them a lot throughout your programming career.
[17:18] Up next, we're going to be talking about if, elif, and else, which is used for control and flow logic. Now, for this example, we're going to simulate that we're getting some user input. So here we'll type in user input of type string is equal to hello.
[17:32] Now, if the user input is equal to hello, we will print that the bot says hello. L if the user input is equal to how are you, we will say that the bot says good, how about you?
[17:47] And in every other situation, we're going to use the else block. So if what we type in doesn't match any of these conditions, the else block is going to be executed. And here we can print that the bot says sorry, I did not understand that.
[18:02] And just like that, we can try running this script. And what we should get as an output is that the bot says hello. Otherwise, if we change this to how are you, the bot should respond good, how about you?
[18:14] And if we type in something random, you'll see that the bot will not understand what we wrote. So with if, you can add any expression that you want to check for, and this code will only be executed if this evaluates to true.
[18:28] ELIF stands for ELSE IF, which means if this doesn't pass, it's going to try to check whether this will pass. And if it does pass, it will execute this code. ELSE will be executed in any other situation. And what's important to note is that you can have as many ELIF statements as you want.
[18:44] You can also check that userInput is equal to BY, then the bot can say goodbye. Now when we actually enter BY as an input, the bot's going to be able to respond with goodbye. Now if you want to see something really cool, all we need to do here is type in inputs followed
[19:00] by new, which is the prompt we want the user to see, and add a while true loop here. Then we need to indent all of this inside the while true loop to make it a block of code. And with those two SQL changes, we now have our very first chatbot.
[19:16] We can type in hello, the bot's going to respond if it finds that. If we type in bye, it'll say goodbye. when you say something random, the bot's going to say, sorry, I did not understand that. It was that simple to create a chatbot in Python.
[19:30] Moving on, it's time we talk about exceptions in Python. What to do when you encounter one, and how you can handle it properly. Because sometimes you're going to type in something weird, such as print, whatever that is,
[19:42] and you're going to end up with an exception, or an error, such as a name error. Now, in recent versions of Python, you're going to get very descriptive error messages. which help you understand what you did wrong.
[19:54] But of course it would be nice to learn how to handle these exceptions, because the one that we just encountered was caused by the developer. But there are going to be some that might be caused by the user. For example, imagine we have two variables, a and b,
[20:07] and I'm going to be using the multiple assignment syntax, which means we can type in 10, comma, and the second variable. And this will assign 10 to a and 15 to b on one line. But for whatever reason, let's pretend the user entered 15.
[20:21] Now, if we were to add a plus b, we're going to end up with an exception because you cannot add a string to an integer. It is an unsupported operant type. Int and string just do not go together.
[20:33] But for whatever reason, that's what the user input. Now, if your user ever sees an exception message, it's probably a bad thing. And that can even lead them to uninstalling your app if it happens too frequently, even if they're in the wrong.
[20:47] It's important to act as an adult when you are programming and to give the user a chance to fix their behavior. For example, instead of just printing A plus B, what we're going to do is type in try, which means we're going to try this dangerous code.
[21:00] And if it doesn't work, we're going to accept exception as E. And here we're going to print that something went wrong. And we're going to insert E, which is the error. Now, the next time we run this, we're not going to get an exception anymore.
[21:14] we're going to get an error message instead, which means we can actually run more code under this try and accept block. Here we can type in continue linked with the program. As you can see, when we run this, it's going to be able to continue with the program,
[21:27] even if we encounter an exception. Without this, the program is just going to crash, and we're never going to reach the rest of the program. So let's go back to what we had earlier. Here we can type in something else, such as please enter a valid number,
[21:42] So that the next time the user inserts the text of 15 and tries to add it to 10, the exception is going to tell them exactly what they need to do to fix it. Anyway, that is the basic concept of handling an exception.
[21:55] Now what I did here is considered a bad practice because exception handles all of the exceptions. When what we really wanted to do is handle the type error. So as you can see, we can be much more specific with our exceptions.
[22:08] And here we can type in something such as, please enter a number in the form of an integer or a float. So now we're catching the correct error and giving them the correct message for that error,
[22:23] which means that once they run the program and they add this to the input, it's going to ask them to please enter the number in the form of an integer or a float, which means now we can change this to 10.5, or actually to stay consistent we'll just type in 15,
[22:35] and it's going to work properly the next time they use that as an input. And one last thing to note is that you can add multiple except blocks. So you can actually handle all the other errors just by adding another except block.
[22:48] Something else went wrong. Because sometimes certain operations are going to lead to multiple errors. But once again I never recommend using exception as E unless it really just a last resort Because this exception will absorb all of the errors And one thing you need to learn in programming is that encountering errors and exceptions is not a bad thing when you are developing
[23:10] In fact, you want to encounter as many as possible because this will lead to more consistent code when you're actually publishing your application. It's going to help you understand everything that can actually go wrong. go wrong. And this approach just silently absorbs that error and makes it disappear. So once again,
[23:28] use this as a last resort. Now very quickly, I want to talk about imports in Python because sometimes you're going to want to import some external functionality into your script so that you can use it. For example, imagine you want to calculate the square root of a number. Now personally,
[23:43] I'm no mathematician, so I prefer to use pre-made functionality to perform that calculation. And in Python, we can import a module called math. And what's good about this is that we can use a lot of its functionality for free just by importing it. And coincidentally, it has a square root
[23:59] function. So here we can calculate the square root of three using the square root function. And once we run it, we're going to get the square root of three. We can also import math using an alias. So for example, import math as m. Now we can print m.square roots of four. And it's going
[24:16] to work exactly the same way, except we're using the alias this time. And finally, if you really want to be specific with your imports, you can import from math the square root function. And this time, we just type in square root and 5, and it will work out of the box just
[24:30] by referring to the function name. And just so you know, with the third approach, you can add as many as you want. You can even add, let's say, tan, which will return the tangent. So we can type in tan of, let's say, 2, and that will work just fine, just by referring
[24:43] to the name. Now to end this crash course, we're going to be creating a simple project. And this is actually one of my favorite projects to make. And this is a chatbot, a very simple chatbot.
[24:55] So first we're going to create a bot name of type string, which is going to equal Bob. Then the first message is going to be a print statement that says, Hello, I'm bot name. How can I assist you today?
[25:08] And once we see that message, we will start our while true loop, which is an infinite loop. And the very first thing we want to do is take some user input. It should be a type string, and that's going to equal input with u.
[25:22] And since this returns a string, we're going to want to lower whatever the user enters. And the reason we're doing that is because if the user types in hello with an uppercase h, and we're trying to compare that to hello with a lowercase h,
[25:35] this is going to return false, because typing is case sensitive, and a capital H and a lowercase h are two different things. Now when you use the dot lower method on hello,
[25:47] it turns all of the letters inside this string to lowercase, which makes it much easier to compare the two. Anyway, if user input is in the list of hi or hello,
[25:59] then we will print that the dot name says hi there, how can I help you? L if the user input is in by or see you, little print that the bot name says goodbye, have a great day.
[26:15] And as you can see here, I'm checking that the user input is inside this list of strings, which I find to be more convenient than checking for each one separately, because it's nice to be able to understand multiple forms of input.
[26:29] But let's also add some functionality. So, L is userInputInPlus or add. And let's make it so our bot can actually perform some mathematical operations.
[26:41] Print botName says, sure, let's do some addition. Please enter two numbers. And now comes the fun part.
[26:53] What we need to do here is try to first get the first number, which will be number one, of type float. and that's going to equal the input of the first number. And as you can see, the code editor is telling us immediately that we got a string,
[27:07] but we were expecting a float. And thanks to this type annotation, we can fix that by surrounding this with the float constructor. Now we're going to duplicate this and say number 2 and type in second number.
[27:19] And if that works, we're going to print the f-string with the bot name that says the sum is number 1 plus number 2. And if it doesn't work, we're going to add the except block.
[27:31] And the main exception we can encounter here is the user adding or importing a letter or some sort of symbol that is not a number. That's going to give us a value error. So here we can print that the bot name says, oops, that doesn't seem like a valid number.
[27:50] Try again. Then we're just going to get out of this except block and go to the outermost layer where we have the if. and we're going to add the else block. And this is going to cover all of the cases
[28:03] that we did not cover above. So, else prints up the bot name says, I'm sorry, I don't understand that. Please try again.
[28:15] And in only 20 lines of code, we created our very first chatbot. So now it's actually time to test how it works. So here I'm going to run the script and I'm probably gonna make that bigger so we can see.
[28:27] And I'm just going to type in hi, as I mentioned earlier, the bot is going to be able to respond to that, even if we have an uppercase H, it's going to understand that. We can type in goodbye, and it doesn't understand that, obviously because we did not code that, we only coded by and cu.
[28:44] But if we type in by, it will be able to respond to that. But now let's try to add some numbers. So we'll type in add, we'll add 10 to 20, and the bot should give us the sum, which is 30.
[28:56] We will type in something random, the bot won't understand that. And if we try to get the sum using the plus, that's going to work too. But let's pretend we add something that doesn't work, such as...
[29:08] Whoa! The bot is going to tell us immediately that it doesn't seem like a valid number. And instead of crashing our program, it's going to tell us immediately, which provides us with a much more smooth user experience.
[29:21] Anyway, I'm just going to say bye. And the bot will tell us goodbye, have a great day. So as you can see, it was a very simple chatbot that has some very simple functionality. But what's cool about this is that you can edit this as much as you like.
[29:35] And you can add all sorts of functionality and so on. But yeah, that just about sums up the basics of Python. There's still a lot more to learn, but the best way to learn is to build apps.
[29:47] So decide on something you want to build and start coding it. As you try to build your app, you're going to have to start Googling to learn new things. That's all part of being a programmer. Even after five years of programming, I Google constantly.
[30:01] Although, I recommend you start small, otherwise it might be a bit overwhelming. So search on how you can improve your chatbots, search on how you can scrape information from the internet, but don't try to build Facebook from day one.
[30:15] That's going to be incredibly overwhelming, and it might even end with you giving up on programming. Because creating apps like Facebook isn't just one concept, but hundreds of different concepts combined. Anyway, I hope you enjoyed this video. Do let me know in the comment section down below
[30:30] whether you have any other questions or whether a certain topic needed more explanation. But otherwise, with all that being said, as always, thanks for watching and I'll see you in the next video.