Python Data Types and Variables
Published: 2024-04-23 10:51:20

Python has several built-in data types such as integers, floats, strings, and booleans. Variables in Python are used to store these data types and manipulate them in our programs.

Understanding these concepts is fundamental to programming in Python. In this guide, we'll explore different Python data types and variables.

## Basic Data Types in Python

Basic data types in Python include Numeric Data Types, Text Data Type, and Boolean. 

### Numeric Data Types

There are two main numeric data types, including `float`, integer (`int`) in Python. They are used to represent numerical values and perform mathematical operations. 

### 1. Integers (int)

Integers are data types used to represent both negative and positive whole numbers (without decimals) in Python and other programming languages.

You can use them for different mathematical operations, including multiplication, division, subtraction , and addition. The following code is an example of two variables(`x` and `y`)  containing integers:

```python
x= 50
y= 2

print(x*y)
print(x + y)
```

In the code above, we have multiplied as well as added the two variables using the `print` function. So, if you run the code on your IDE or terminal, you’ll get the following output:

```python
100
52
```

### 2. Floats

`Floats` are used to represent both positive and negative numbers with decimals or fractions in Python and other programming languages.

Just like `integers`, you can also perform numeric operations like addition, multiplication, division, and subtraction with floating numbers. The following code multiplies and add two floating numbers.

```python
x= 20.5
y= 2.2

print(x*y)
print(x + y)
```

Output:

```python
45.1
22.7
```
However, you can also use `integers` and `floats` together or interchangeably if it's suitable for your Python program. Let’s take another example where you can perform numerical operations on `floats` and `int`:

```python
x = 20.5
y = 2

print(x + y)
print(x*y)
```

Output:

```python
22.5
41.0
```

### 3. Text Data Type

`Str`(string) is the primary text data type in Python. Strings are used to represent textual data and can contain any sequence of characters, including letters, numbers, symbols, and whitespace. The following is an example of a variable containing strings.

```python
myString = "Hello World"
```

### 4. Booleans

Booleans are data types in Python and other programming languages used to represent truth values. They are represented by keywords such as `True` and `False`.

`Booleans` are fundamental in decision making and flow control within programs, as they allow for conditional statements and logical operations.

Let’s see how you can use `booleans` in a Python program with the following example:

```python
raining = True
sunny = False
if raining:
   print("Don't forget to take your umbrella along")
if sunny:
   print("It's sunny outside")

```

In the code above, we assigned the two `boolean` values `True` and `False` to two different variables (`raining` and `sunny`). The `if` block will print any of the conditions that is `true`.

So, in this code, the `raining` variable contains the boolean value, `True` and will print the conditions attached. If you print the code in your terminal or IDE, you’ll get the following output:

```python
Don't forget your umbrella
```

## Variables in Python

Variables are considered containers where you can save different kinds of data in a Python program. Unlike Javascript, you don’t need to write any command before declaring a variable.

It's declared the moment you assign a value to it. Now, let’s declare two variables and assign values to them:

```python
A= 47
a= 20
print(A)
print(a)
```

It’s very crucial to note that variables in Python are case sensitive. The fact that the both variables are of the same letter doesn’t mean they are the same. they are considered two different variables.