DEV Community

Cover image for Day 3: python data type.
Rebecca-254
Rebecca-254

Posted on

Day 3: python data type.

Today i went through something every programmer must understand deeply "data types". Not just in theory, but to use them practically with arrays (lists) and simple logic.


so what are data types in python

In Python, I learnt that everything is an object and every object has a type.
Data types are just the “kind” of information your code is working with.

Whether it’s a name, a number, a yes/no response, or even a list of items Python wants to know: “What type of data is this?”

common data types in python

  1. Integer 'int'

This are digits. They are whole numbers either negative or positive. For example

Points=[7][5][8][6][9] #list of integers. 
Enter fullscreen mode Exit fullscreen mode
  1. Float 'float'

This are used to show numbers with decimal points. For example

Temperature= 36.7 
Enter fullscreen mode Exit fullscreen mode

3.string 'str'

This is used in any text inside a quote. Can be a number, name or even emojis. For example

Name= "Rebecca" 
Enter fullscreen mode Exit fullscreen mode

4.Boolean 'bool'

This used in logic values. Either true or false. Suitable for conditions. For example

is-coding-cool=true 
Enter fullscreen mode Exit fullscreen mode
  1. List 'list'

This is just a collection of multiple items in a single variable. One can mix items too. For example

my_list = [10, "apple", True, 3.5] print(type(my_list)) # <class 'list'> 
Enter fullscreen mode Exit fullscreen mode
  1. Tuples 'tuple' This one is the same as list but in this case can not be changed. For example
my_birthdate = (2004, 6, 23) 
Enter fullscreen mode Exit fullscreen mode
  1. Dictionary 'doct' Is just collection of key-value pairs — like mini-databases. For example
student = {"name": "Rebecca", "age": 21} 
Enter fullscreen mode Exit fullscreen mode
  1. Set 'set'

Is an unordered collection of unique values where duplicates are ignored. For example

unique_numbers = {1, 2, 2, 3} 
Enter fullscreen mode Exit fullscreen mode

It gives 1,2,3 as the results.
For better understanding.
https://www.geeksforgeeks.org/python/python-data-types/

summary

a summary block diagram on data tupes


What are arrays?

Basically , it is like a box with many compartments, where you can store multiple values without creating separate variables. For example

students = ["Alice", "Brian", "Clare"] 
Enter fullscreen mode Exit fullscreen mode

While using python to deal with arrays I learned that one can use three formats.

  1. Python list
  2. Array modules
  3. Numpy arrays.

1. Using Python Lists

Python can use lists like arrays. This is how it works step by by step

Create a list eg

fruits = ["apple", "banana", "orange"]

Access items eg

print(fruits[0]) # The first item is apple.

Add items eg

fruits.append("mango")

Change items eg

fruits[1] = "coffee" # Replaces banana with coffee

Remove items eg

fruits.remove("apple")

Loop through items

for fruit in fruits:
print(fruit)

2. Using the array Module

When one is dealing with numbers only, can use the array module.this is the way.

import array example

marks = array.array('i', [60, 70, 80]) marks.append(90) print(marks[2]) 
Enter fullscreen mode Exit fullscreen mode

3. Using NumPy Arrays

When doing math, statistics, or big data, one should use NumPy arrays. Example

import numpy as np nums = np.array([1, 2, 3, 4]) print(nums * 2) 
Enter fullscreen mode Exit fullscreen mode

What I tried

I opened my VS Code and wrote simple examples for each type.
Then printed the type using type() and played with changing values. Like

  1. Adding integers and floats
  2. Joining strings
  3. Creating a list of my favorite colors 4.Creating a dictionary for a student profile

It felt great seeing Python understand what I meant just by using the right type.


Thanks for reading
If you’ve just started learning Python, feel free to reach out or comment below. Let’s grow together

GitHub: Rebecca-254

Top comments (0)