Subtitles section Play video Print subtitles Hi and welcome! We at 365 Data Science specialize in data science trainings. We post videos weekly, so you can master indispensable skills for free! Alright, let’s get started! 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 capital letters. 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! Finally, 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! Thanks for watching! If you found this video interesting and want to gain an edge in your career, make sure to like, comment, and subscribe! And don’t forget to check out some of our other videos for another quick win in the data science skills department!
B2 variable integer print string boolean obtain Python Tutorial for Beginners: Understand Python Data Types in 10 minutes 8 0 林宜悉 posted on 2020/03/09 More Share Save Report Video vocabulary