Python Data Types — Numeric with Integers and Float. | RajCoding

Ankit Raj
6 min readOct 29, 2020

Hi and welcome at RajCoding specialize in computer programming, we publish Post weekly so you can master indispensable skills for free.

Python Data types

Alright In this post we understand the concept of numeric data types in Python or Integers and Floats. Let’s get started when programming in Python if we say that a variable in python has a numeric type value that numbers can be integers or floating points also called floats for instance integers are positive or negative whole numbers without a decimal point.

Now let’s create x1 and bind it to the value of five, now x1 is an integer a specific function in Python can prove this is correct it is called type within the brackets we must put the name of the variable whose value type we want to verify.So in this case I will type x1 okay and then Shift + Enter and the result we get is int which indicates the value is an integer{int}.

The type of function can also be applied directly to a value instead of a variable for instance if I write type open parentheses minus six close parentheses Python will correctly point out that minus six is an integer good now let’s assign the value of four point seven five to a new variable X two I would like to check its type hints.

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 hints they have a decimal point four point seven five 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 four float instead we’ll add a decimal point to the integer value.

And we’ll turn it into a float not all variables should be assumed as numeric value, basically all variables are not similar to float lets take an example of such type of values that is the boolean type in Python which means a true or false value corresponding to the machines logic of understanding ones and zeros on or off right or wrong true or false, which is either true or false.

Let’s provide an example with a new variable X 3 which is equal to true right the output of the type function is bool which simply means X 3 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 Try a magic trick that will correct the mistake or just 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

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 we did not follow the rules of programming or coding in Python.

We can convert Y into a string string STR 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 Python ik 1 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 backslash before the apostrophe within the phrase and will 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 obtained 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 ‘ 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 tin dollar example a minute ago.

Okay 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 read 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 + 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 Reading if you found this Post interesting and want to gain an edge in your career make sure to comment and subscribe and don’t forget to check out some of our other Post for another quick win in the data science skills department or in any Programming Languages.

Originally published at https://www.rajcode.com.

--

--

Ankit Raj

I am Ankit, a software programmer with a passion for coding, web development, and the founder of RajCoding as "therajcode.blogspot.com".