Data Types in Python~ ʕ•́ᴥ•̀ʔ

Data Types in Python~ ʕ•́ᴥ•̀ʔ

·

4 min read

SOOOOOO, university exams just got over and I'm excited to be back at writing daily blogs where I'm learning about python from scratch and noting it down in the most straightforward way.

So firstly, we'll be starting again with a relatively simple topic, which is Data types, and then tomorrow, we are gonna move forward with one of the most important aspects of coding in general, Conditional Statements (this one's simple as well tbh)

But for Now~!


Data Types

Data type basically refers to the type of value which we have assigned to any variable, for example, is it a text, or a number, to put it simply.

There are different types of Data types, such as :-

  • Integers

  • Float

  • Boolean

  • String

Now knowing these data types is important to avoid errors. For instance, you can divide two floats, but you cannot divide two strings, or, 15/5 makes sense, but Yash/cat does not.

So firstly,


Integers

These are the numbers WITHOUT the decimals. It includes Zero And they can be negative.

....-4, -3, -2, -1, 0, 1, 2, 3, 4....

x = 14
print(x)
print(type(x))

14

<class 'int'>

Therefore, the variable x is of integer data type.


Floats

Now, these are the numbers that CAN INCLUDE decimals. They also can be defined in fractions and we can even round the decimal places from 1 to 15 decimal places. (15 is the max)

pi = 3.141592653589793238462643383279502884197169399375105820974944

print(pi)

3.141592653589793

round(pi,3)

3.142

How rounding off works is that the digit after the one which we're rounding up is less than 5, it'll stay the same. But if the next digit is equal to, or more than 5, then the digit will get 1 added to it. This example is gonna explain it better.

if I got a variable

a = 4.2069

and if I do

round(a,3)

The result will be

4.207

because the number next to the 3rd digit is more than 5, its a 9.

On the other hand, if the variable we got is

a = 4.2064

and we round it off again,

round(a,3)

the result will now be

4.206

Because, well, you get it.

Now its a good thing to remember that 1.0 is also considered a float, even if there's practically nothing after the decimal.

1 = integer

1.000 = float (doesn't matter how many zeroes)


Booleans

Booleans simply have 2 values

TRUE aka 1

or

FALSE aka 0

Examples :-

a = True
print(a)
print(type(a))

True

<class 'bool'>

b = (1 > 2)
print(b)
print(type(b))

False

<class 'bool'>

We can also switch the vales of a boolean using "not". In the above example, b is false, now what we can do is

c = not b
print(c)

True

because "c" is NOT "b", and b was false, so "c" is true.


Strings

These are basically collections of characters like letters, numbers, symbols, or punctuation marks. They are defined inside quotation marks.

a = "Hello World!"
print(a)
print(type(a))

Hello World!

<class 'str'>

We can find the length of a string using the extension len() with print, as in :-

print(len(a))

12

We can also have a string with zero length.... yeah.... Literally just double quotes and baam, its a string.

b = " "

There we go :p

Now doesn't matter what we put inside the quotation marks, it'll be a string

For example,

c = "1.5152"

That's a string

But

c = 1.5152

This is a Float.

We can also change a string into a float if we have the conditions. Above, c is a string but what we can do is,

d = float(c)

which will change the same number into a float from a string.

We are also able to add two strings similar to how we add two integers

x = 45 + 50

print(x)

95

Similarly,

z = "hyper" + "link"

print(z)

hyperlink

We can even multiply strings.

multiply = "yash" * 3 

print(multiply)

yashyashyash

But we can not add different Data Types together, for example, Float and String cant be added or multiplied together.


Alright, That should be enough after our 2-week break, although it wasn't actually a break since I was busy with exams but anywaysss, It's fun to be back and I GOT 8 FOLLOWERS ALREADY WHICH IS HUGE!

So that's it for today, Keep learning and enjoying everyone~!

Peace~🕊️

Yash K.