Subtitles section Play video Print subtitles All right, great! Warm up your fingers; in this lesson, we will start coding! One of the main concepts in programming is variables. They are your best friends. You will deal with them all the time. You will use them to store information. They will represent your data input. Let’s say you want to have a variable x that is equal to the value of 5 and then ask the computer to tell you the value of that variable. So, we must tell the machine that x equals 5. And this is how you could do this in Python. Type x equals 5. To go through the process of programming, the line that says x equals 5 is called a command or a program. This is just a line of text. To make something out of it, we must execute it. Only then will the computer carry out operations with it. Press Shift and Enter (not just Enter), and a variable called x will be created and assigned with a value of 5. To be more precise, “equality” in Python and in programming means “assign” or “bind to”. Ok, we carried this operation, but we see nothing right now. How can we ask the computer to show us the output of what we just did? It would be sufficient to write x and then press Shift and Enter. And here’s the result – 5. Great. As you can see, typing in a single line of code entails a few concepts of programming simultaneously. Now, let’s assign the value 8 to a variable we call y. … Alright, Shift plus Enter… and we can check y. However, I’ll type capital Y. Oh! An error! This shows us that Python is case sensitive, so pay attention to that. It matters if you use lowercase or uppercase letters. An alternative way to execute the instruction that will provide the value we assigned to y would be to use the print command. At first sight, it seems redundant as we showed we can just type “y”. Nevertheless, this command is applied often; you’ll see it in most of the code produced by professionals. It complements the logical flow of your instructions. For instance, if we say “print y”, the machine will simply execute this command and provide the value of y as a statement, and this is all a programmer must see sometimes. “Print” exists in Python 3 as well. Its functionality is practically identical to the one just described for Python 2 with the sole difference that here you must place the name of the variable within parentheses, in this case Y. Then, you can press “Shift and Enter” to execute the code in the cell and see that you’ll obtain an identical output – the number 8. The difference stems from the fact that Python 3 treats “Print” as a function, while Python 2 – rather as a command. Therefore, from now on, if you are using Python 3, and you see “Print” followed by a name of a variable, please just add parentheses around the variable name and you’ll be good to go. Great! We hope you found this comparison useful. Now let’s continue by talking about Python variables. The last thing I’d like to share with you in this lecture is you can assign a certain number of values to the same number of variables. To create the variables x and y, we have to assign two values – say, 1 and 2. We must separate each of the variables and each of the values with a comma. The parentheses here are not obligatory, but we use them to improve the readability of our code. Now, if I call x or y separately, the computer will correctly give me their respective values. It is very important that the number of variables on that line equals the number of values; otherwise, you will get an error message. See? Great! This is a great start to our journey in Python! When programming, not only in Python, if you say that a variable has a numeric value, you are being ambiguous. The reason is that numbers can be integers or floating points, also called floats, for instance. Integers are positive or negative whole numbers without a decimal point. Let’s create x 1 and bind to it the value of 5. Now, x1 is an integer. Do you agree? A specific function in Python can prove this is correct. It is called “type”. Within the brackets, we must place the name of the variable whose type of value we want to verify. So, in this case, I’ll type x1. Ok. Shift plus Enter and the result we obtained is “int”, which indicates the value is an integer. The “type” function can also be applied directly to a value instead of a variable. For instance, if I write “type, open parentheses, minus 6, close parentheses”, Python will correctly point out that minus six is an integer. Good. Now, let’s assign the value of 4.75 to a new variable, x 2. I would like to check its type; hence, I will use the type function again. This is a float. Great! Floating points, or as you’ll more frequently hear, floats, are real numbers. Hence, they have a decimal point. 4.75 is such a number; therefore, Python reads it as a float. Let’s look at two other built-in functions. “Int” transforms the variable into an integer. That’s why 4.75 turns into 4. “Float”, instead, will add a decimal point to the integer value and will turn it into a float. Not all variables should assume numeric values. An example of such type of values is the Boolean type. In Python, this means a True or False value, corresponding to the machine’s logic of understanding 1s and 0s, on or off, right or wrong, true or false. Let’s provide an example with a new variable, x3, which is equal to True. Right. The output of the “type” function is ‘bool’, which simply means x3 is a Boolean. An important detail you should remember is you have to type True or False with capital letters! Otherwise, Python won’t recognize your variable as a Boolean and will display an error message. So, to wrap it up, the two Boolean values a variable can have are True or False, and they must be written with a capital letter. Thank you for watching! Strings are text values composed of a sequence of characters. Let’s see how we can create a string in practice. If we ask the machine to display the name George this way, we’ll obtain an error message. Why? Because Python assumes George is the name of a variable to which we have assigned no value. Here’s the magic trick that will correct this mistake. Let’s type single quotation marks around the name George, first. And now, let’s type double quotation marks around it. You see the output values of these two inputs are the same. This is how Python displays text results if you don’t use the print command. Should you use print, the output will be shown with no quotes – you’ll be able to see plain text. If we assign this value to a new variable, let’s say x4, we can obtain its output as we did with the integers and floats. All right, so that’s it. If the values you’d like to assign are not numerical, the quotes can come into play! Assume the variable y is supposed to represent the number of dollars you have in your pocket. In addition, you would like to ask the machine to print out a statement that says “Y dollars”, where y is a number. The proper way to combine the value of y and the string “Dollars” is to use a “plus” sign, as shown here. Let’s execute this cell to check if we are missing something. Apparently, we did not respect the rules of coding in Python. We cannot put different types of variables in the same expression. Y is an integer, and “Dollars” is a string. We can convert y into a string. “String”, s.t.r., is the built-in function we need. Analogically to integers and floats, “string” will convert our number into text, and that will unlock our result. To summarize what we said so far, Python can automatically guess the type of data you are entering. It is within its capabilities to know for sure whether you have assigned an integer, a float, a Boolean, or a string. You need not declare the types of variables explicitly, as you must do in some other programming languages. Python always knows the type of variable. What will happen if you type something like… “I’m fine”? You’ll need the apostrophe in the English syntax, not for the Pythonic one. Observe, if you execute the command like this, you will make a mistake. To avoid that, in such situations, you can distinguish between the two symbols – put the text within double quotes and leave the apostrophe, which technically coincides with the single quote between I and M. Now, you are fine. An alternative way to do that would be to leave the quotes on the sides and place a back slash before the apostrophe within the phrase, and we’ll still obtain the same correct result. This backslash is called an escape character, as it changes the interpretation of characters immediately after it. And what if we wanted to state “press “Enter””, where we put Enter within inverted commas? Same logic – the outer symbols must differ from the inner ones. Put single quotes on the sides. And you obtain the desired result! Let’s go through a few ways of stating values. Say you wish to print “Red car” on the same line. If I write it like this – two words next to each other, separated by a blank space, I’ll see them attached. One trick would be to put a blank space before the second apostrophe of the first word. Let’s see… nice, that looks like the desired result! Another technique would be to sort of “add” one of the strings to the other by typing in a plus sign between the two, just as we did with the “10-dollar” example a minute ago. Ok. As your intuition probably tells you, if you print this combination instead, you’ll obtain the same outcome, but it won’t have the quotes on the two sides. And… here’s a new trick. I’ll type “print ‘Red’", and then I’ll put a comma, which is called a trailing comma, and Python will print the next word, ‘car’, on the same line, separating the two words with a blank space. Shift plus Enter… great! Let’s print the number 3 next to the number 5. Boom – fantastic! Here it is. What will happen if I don’t use the print command and just list a few integers, floats, and strings separating them with commas? Python will execute the command as expected but will place the values within parentheses. Strictly amazing! Great! We are making excellent progress! The next topic on our agenda are arithmetic operators. The addition and subtraction signs are straightforward to use. Technically speaking, in the equation you see here, 1 and 2 are called operands, while in the next one, the operands are 3 and 5. The plus and minus signs are called operators, and given they also represent arithmetic operations, they can be called arithmetic operators. Division is more interesting. If we want to divide 15 by 3, we will need to use the forward slash sign. What will happen if we try to divide 16 by 3? 5 again? Yes, the output was 5 because in Python 2, the quotient is an integer by default. Mathematically, if we divide the integer 16 by 3, we will obtain a quotient of 5 and a remainder of 1. If we use real numbers, or floats, the float 16 divided by 3 will result in a float value of 5.33. Therefore, we obtained as a quotient the integer 5 and no information regarding the remainder of the division of 16 by 3. This is one of the few substantial differences between Python 2 and 3. In Python 3, you would immediately get 5.33 as an answer, or a float, because the software will understand your first number was a float value. To avoid this, when we use Python 2, we should look for the quotient of the float 16 divided by 3 and not of the integer 16 divided by 3. So, we should either transform the number into a float or type it as a float directly. See? This is the correct answer. Now, let’s obtain the remainder of the division of 16 by 3. How can we make Python produce “1” as an output in this cell? The operator that can help us is the percentage sign. I’ll type 16, percentage sign, 3, and when I execute the command, I will obtain the remainder. The answer we received is “1”. Good. Multiplication. As usual, we can use the star sign when we want to multiply. For example, 5 star 3 will lead us to an output of 15. For the record, you can assign any arithmetic operation to a variable. If we assign 5 times 3 to the variable x, and then we call x, we will obtain 15 again. Great! How could you calculate 5 to the three? By using the double star operator. Type 5, two stars, 3, and … here you go – 125! Easy, right? Ok. Basically, this covers arithmetic operators. You know the right way to interpret the equals sign when programming is “assign” or “bind to”. For instance, “assign 5 to the power of 3 to the variable y”; “bind 5 to the power of 3 to y”. This means from that moment for the computer, y will be equal to 125. Here is what will happen when you double the equality sign. Let me type “y, double equality sign, 125”. The correct way to read this code is “y equals 125”. When you run this command, the computer will assume you have requested an answer to the question, “Is y really equal to 125?” This is why, after the execution of this cell, the machine will respond with a Boolean value – it will either return “True” or “False”. Let’s check our output when we state y is equal to 126. Great! The machine replied with “False” because 125 and 126 are different numbers. Wonderful! Remember – when you mean equality between values and not assignment of values in Python, you’ll need the double equality sign. Anytime you use it, you will obtain one of the two possible outcomes – “True” or “False”. Ok, let me explain a programming concept that is valid for other programming languages, as well. If I assign the value of 1 to a variable z, my output after executing z will be 1. After that, if I assign 3 to the same variable z, z will be equal to 3, not 1 anymore. How come? Well, the order of the commands matters. Initially, we said z will be equal to 1, and that was true until we changed the value to 3. For the computer, from that moment on, z is not equal to 1, and it will continue to be 3. As proof, see this - if we add 5 to z, we will get 8, not 1 plus 5, which is equal to 6. Then, if we suddenly decide z is equal to 7, z will not be equal to 1 or 3 anymore. Python reassigns values to its objects. Therefore, remember the last command is valid, and older commands are overwritten. Try to be careful when performing calculations. We should remember we can only combine numbers and not strings. If you put 5 in quotes here, Python won’t be able to carry on the calculation, and you will be advised to correct the variables you have used as operands. Operands must be of the same data type, in this case – numbers, as integers, floats, or both. Especially when your codes become longer, and by longer, I mean containing tens or hundreds of rows, it becomes difficult to understand how your work has been structured, because there are too many lines. What you could do in these situations is leave a comment. Comments are sentences not executed by the computer; it doesn’t read them as instructions. The trick is to put a hash sign at the beginning of each line you would like to insert as a comment. I’ll improvise with a random sentence… “It is just a comment and not code”. Execute with Shift and Enter. When you run this cell, there will be no output, because the comment does not count as code. Let’s add code – print 7 and 2 on the same line. Execute with Shift and Enter. Yes, precisely – we got 7 and 2, and the comment row marked with a hashtag produced no output. It remained visible only to the programmer. The computer executed the print command only. If we would like to leave a comment on two lines, don’t forget to place the hash sign at the beginning of each line. These two will be comments, print 1… and we obtained 1. Everything seems to work. Ok, perfect! I’d like to show you a neat trick that will be extremely valuable when you become an advanced Python programmer and work with large amounts of code. This is a very handy feature, so please pay attention. Sometimes, the length of the cell will not suffice for you to finish your line. Lines of code could get long. Or, just for the matter of organizing your code, you might prefer to send part of the code to the next line. So, 2.0 times 1.5 plus 5 could be written in two lines, and the machine could still read it as one command. This could be achieved by putting a back slash where you would like the end of the first line to be. It indicates you will continue the same command on a new line. Cool, right? 0.3.6 All right, great! Let’s look at another important concept that will help us a great deal when working in Python - indexing. This is a technique we’ll use frequently later in the course, especially when we focus on Python’s application in the world of finance. So, here’s an example of how indexing works. The word “Friday” is written here, right? Is it possible to extract the letter “d”? Yes, we can do that by using square brackets. And within them, we should specify the position of the letter we would like to be extracted. A very important thing you should remember is that, in Python, we count from 0, not from 1! 0, 1, 2, 3, 4, and so on. That’s why I’ll ask for the 4th letter, ‘d’, by writing 3 here. See? And we obtained the letter “d”. Had we put 4, we would have obtained the letter ‘a’. This is the syntax in this occasion – square brackets right after the word, or the string of characters, if you wish, and a number indicating the position of interest. This is how indexing works in Python. The next concept for programming in Python we will see here is fundamental – it is called indentation. The way you apply it in practice is important, as this will be the only way to communicate your ideas to the machine properly. Here is what I mean. Let’s define a function “five” that takes as an argument an unknown x. It will be a simple one – x will be reassigned the value of 5, and the function will return the value of 5 for us. Please note that I am using an indent. Now, we can print the result of five with an argument of 3. Nothing happened. Why? Because printing five of 3 is within a function, so it will be executed only when the function is applied. If we place print on a new line, instead, aligned to the def command, the output will be different. How come? The print command is executed on its own, not as part of the five function. Def and Print form two separate and, written in this way, clearly distinguishable blocks of code or blocks of commands. And it makes sense to use indentation, doesn’t it? Everything that regards the function is written with one indentation to the inside. Once you decide to code something else, start on a new line with no indentation. The blocks of code are more visible, and this clarifies the logic you are applying to solve your problem. Working with functions is interesting, right? We will learn more about the operators that will help us in our work in Python. We will start with comparison operators. We said, if we type the equality sign twice, we can verify the left and right side of an equality are equal. Well, if we use an exclamation mark and an equality sign, then we could verify if two sides are not equal. So, it would be false to say 10 is not equal to 10, and it will be True that 10 is not equal to 15. If we use an exclamation mark and equal, and the two sides we are comparing are 10 and 15, we’ll obtain True, as we wanted to verify they are not equal. Good. What is next? “Greater than” and “less than”. We can use the well-known symbols to test if a value is greater or smaller than another value. Is 100 greater than 50? Yes, it is. Is it smaller? No, it is not. And that’s why we get “False”. That’s great! The logic behind checking whether an operand is greater than or equal to, and less than or equal to, is the same. Don’t forget that, on the right side of the operand, we are not limited to providing only one number, like ten. We could insert an expression, like 10 + 10. So, is 15 greater than or equal to 10 + 10? No. Is 15 less than or equal to 10 + 5? Well, that is true. Great! This covers everything we can possibly say about comparison operators. Now, let’s see what is intended with the expression logical operators, also known as Boolean operators. Briefly, the logical operators in Python are the words “not”, “and”, and “or”. They compare a certain amount of statements and return Boolean values – “True” or “False” – hence their second name, Boolean operators. Let’s start by providing an example with “and”. “And” checks whether the two statements around it are “True”. Let’s use only the Boolean values “True” and “False” for now. “True” and “True” will result in “True”, while “True” and “False” gives as an answer “False”. “False” and “False” will naturally bring us to “False”. Ok. “Or” checks whether at least one of the two statements is “True”. Hence, “False” or “False” will come back as a “False”, whilst “True” or “True” will return “True”. In this cell, “True’ or “False” will return “True”. The order in which we have the two statements does not matter, so “False” or “True” will still result in “True’, as well. The way “Not” functions is it leads to the opposite of the given statement. “Not True” leads to “False”, “Not False” leads to “True”. Let’s see a slightly different example. The idea is to show you in this cell that the Boolean operators can be applied not only to Boolean values. The statement “3 greater than 5” is “False”, while “10 less than or equal to 20” is “True”. “False and True” entails “False”, and this is what we obtained. Good. The fun starts when you combine these logical operators. In these occasions, you must respect the order of importance of these three operators. It is: “not” comes first, then we have “and”, and finally “or”. The examples in these 3 cells will help you get the right intuition. In the command “True and not True”, we should first consider what the operator “not” will do. It will be applied to the value “True”. And “not True” means “False”. Therefore, what’s written in this cell must be interpreted as “True and False”. Now, the remaining operator “And” can be applied. “True and False” leads us to False. Let’s run this cell. The answer is correct – “False”. Let’s do an example with all three Boolean operators. “False or not True and True” logically is the same as “False or False and True”, because before anything else, “not True” must be read as “False”. Then, “and” has an advantage over “or”. This is why we will concentrate on the phrase “False and True”. Its outcome is “False”. We are now left with “False” or “False”. Both values are “False”, which always leads to “False”. Execute with “Shift and Enter” and… as expected – “False”! To solidify the concept, let’s go through another similar example. “True and not True or True” is the same as “True and False or True” because initially, “not” mattered most. Now, “And” will convert “True and False” to “False”. Now, we can think about the remaining “False or True”. Because the “or” operator needs at least one “True” statement to return “True”, our result after running this cell will be… “True”! Great! Let’s see what identity operators are about. The identity operators are the words “is” and “is not”. They function similar to the double equality sign and the exclamation mark and equality sign we saw earlier. Let’s illustrate their application with the following examples. If we say 5 is 6, we’ll immediately understand it is false, the same as if we wrote it like this, with the double equality sign. If we said “5 is not 6”, that’d be True, and it will be the same as if we wrote “5, exclamation mark, equals 6”. Great, you have learned a lot about operators in Python and about its syntax. Thanks for watching!
B1 variable print boolean equal assign float Introduction to Python for Absolute Beginners (2020) 3 0 林宜悉 posted on 2020/03/09 More Share Save Report Video vocabulary