DEV Community

Angkon-Dutta-Joy
Angkon-Dutta-Joy

Posted on

Python (Data types, Values and Variables)

Data Types

Data types are like the different categories of data that exist in Python - think of them as different "flavors" of data! There are five main groups of data types in Python, but there are also many other types available. First let's know about them. Don't worry if you don't understand a single term. we will learn about them deeply in the upcoming vlogs.

Image description

The first group is numeric type, which includes integers (positive or negative whole numbers without a fractional part) and floating-point numbers (real numbers with a decimal point). Think of them like different kinds of numbers on a number line!

a = 5  #(here 5 is an integer we stored in a continer named a which will be stored in our memory)
b = 12.65 #(it is a float data type. any fractional number will be float data type.)
Enter fullscreen mode Exit fullscreen mode

Next up is boolean type, which includes just two values: True and False. This type is used for logical operations, like checking if something is true or false.

a=False
b=True
Enter fullscreen mode Exit fullscreen mode

The third group is sequence type, which is an ordered collection of similar or different data types. Strings are a type of sequence made up of ordered characters - think of them like a bunch of letters in a particular order! Lists are also a type of sequence, but they can have elements with more than one data type. Tuples are another type of sequence, but we'll cover those later on.

a='python'
b="let's learn python"
c=[1,2,3,4,5]
d=['I','Love','Doughnut']

#here a and b is string data type and c and d is list data type
Enter fullscreen mode Exit fullscreen mode

The fourth group is mapping type, which includes dictionaries. These are collections of key-value pairs, like a dictionary where each word has a definition.

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
Enter fullscreen mode Exit fullscreen mode

Finally, there's NoneType, which represents a null or no value at all. It's a special data type with a single value: None.

a = None (None have to start with capital letter N)
Enter fullscreen mode Exit fullscreen mode

Data type checking

The type() function is commonly used for debugging and code correction purposes as it helps identify the type of an object or argument passed as a parameter. By returning the type of the object, the function provides useful information about the data being used in the code, which can be used to diagnose and fix errors.
Code

type("Hello World") 
type(110) 
type(123.456)
type(True) 
type(None)
Enter fullscreen mode Exit fullscreen mode

Output

str
int
float
bool
NoneType
Enter fullscreen mode Exit fullscreen mode

print() Function and it's use

The print() function is employed to display the value of an expression enclosed in parentheses (a pair of opening and closing brackets). It is typically used to generate output on the screen when writing a program, unlike in a command-line interpreter where it may not be necessary. The print() function is essential for observing and analyzing text output generated by a set of code. Further information regarding functions in Python will be discussed at a later time.
code

print('Hello World')
Enter fullscreen mode Exit fullscreen mode

Output

Hello World
Enter fullscreen mode Exit fullscreen mode

Variable and value

Variables are containers for storing data values.
Suppose you need to store a value 5 for further calculation. So, where will you store it? In computer memory right? But the name for that memory address will be too long for you to remember. So, why not we give it a name?

let call that container that will store our 5 in it is gpa.
So, we have to write,
gpa=5

we right the variable name in the left side and value on the right.

lets see how variable works in a real code.

check = 123
print(check)
print(type(check))
print("======")
check = "Hi"
print(check)
print(type(check))
Enter fullscreen mode Exit fullscreen mode

Output

123
<class 'int'>
======
Hi
<class 'str'>
Enter fullscreen mode Exit fullscreen mode

We can change the value of same variable with another value.

color='Red'
color='Green'
print(color)
Enter fullscreen mode Exit fullscreen mode
Green
Enter fullscreen mode Exit fullscreen mode

see, here color was red but then we wrote green for which the value changed and green is stored, but red lost.

Variable naming conventions

While naming the variables we need to follow certain rules and restrictions. These are called “Variable
naming conventions”. By following these our codes become more understandable by the Human
Readers (us). Few significant rules have been mentioned below.
Variable names-
● can have letters (A-Z and a-z), digits(0-9), and underscores ().
● should maintain snake_casing. That means each word should be separated by underscores(
)
● cannot begin with digits
● cannot have whitespace and special signs (e.g.: +, -, !, @, $, #, %.)
● are case sensitive meaning ABC, Abc, abc are three different variables.
● have to be meaningful with the data stored in it.
● cannot be too lengthy and too general, need to have a balance
● should not be written with single alphabets. Minimum length should be 3.
● cannot be a reserved keyword for Python. There are a total of 35 keywords.they are keywords that you can't use as variable name which are :

Image description

That's all for today....keep reading, keep learning. Also don't forget to practice.

Top comments (0)