Getting Started with Python — Datatypes

TechwithJulles
3 min readFeb 20, 2023

--

If you’re new to Python, one of the first things you’ll need to learn is how to work with different data types. In this article, we’ll cover the basic datatypes in Python and provide code examples for each one.

Integer

Integers are whole numbers, such as 1, 2, 3, and so on. In Python, you can define an integer by simply assigning a value to a variable:

x = 5

This creates a variable named x and assigns the value 5 to it. You can perform arithmetic operations on integers, such as addition, subtraction, multiplication, and division:

x = 5
y = 3
z = x + y
print(z)

Floats

Floats are decimal numbers, such as 1.5, 3.14, and so on. In Python, you can define a float by adding a decimal point to a number:

x = 3.14

You can also perform arithmetic operations on floats:

x = 3.14
y = 2.0
z = x * y
print(z)

Strings

Strings are sequences of characters, such as “hello” or “world”. In Python, you can define a string by enclosing the characters in either single or double quotes:

x = "hello"
y = 'world'

You can concatenate strings using the + operator:

x = "hello"
y = "world"
z = x + " " + y
print(z)

Lists

Lists are ordered collections of elements, such as [1, 2, 3] or [“one”, “two”, “three”]. In Python, you can define a list by enclosing the elements in square brackets:

x = [1, 2, 3]
y = ["one", "two", "three"]

You can access individual elements of a list using indexing:

x = [1, 2, 3]
print(x[0])

You can also modify elements of a list:

x = [1, 2, 3]
x[0] = 4
print(x)

Dictionaries

Dictionaries are unordered collections of key-value pairs, such as {“name”: “Julles”, “age”: 29}. In Python, you can define a dictionary by enclosing the key-value pairs in curly braces:

x = {"name": "Julles", "age": 29}

You can access individual values of a dictionary using indexing:

x = {"name": "Julles", "age": 29}
print(x["name"])

You can also modify values of a dictionary:

x = {"name": "Julles", "age": 29}
x["age"] = 40
print(x)

Tuples

Tuples are ordered collections of elements, similar to lists, but are immutable. In Python, you can define a tuple by enclosing the elements in parentheses:

x = (1, 2, 3)
print(x)

Because tuples are immutable, you cannot modify their elements.

Sets

Sets are unordered collections of unique elements, such as {1, 2, 3} or {“one”, “two”, “three”}. In Python, you can define a set by enclosing the elements in curly braces:

x = {1, 2, 3}
y = {"one", "two", "three"}
print(x)
print(y)

You can perform set operations on sets, such as union, intersection, and difference:

x = {1, 2, 3}
y = {2, 3, 4}
z = x.union(y)
print(z)

Boolean

Boolean values are either `True` or `False`. In Python, you can define a boolean value by simply assigning it to a variable:

x = True
y = False
print(x)
print(y)

You can also use boolean operators, such as `and`, `or`, and `not`, to perform logical operations:

x = True
y = False
z = x and y
print(z)

Conclusion

In this article, we covered the basic datatypes in Python, including integers, floats, strings, lists, dictionaries, tuples, sets, and booleans. By understanding these datatypes, you’ll be well on your way to writing powerful Python programs. Don’t forget to experiment with these datatypes on your own and explore their capabilities in more depth.

Part 2: Getting Started with Python: Google Colab — Lesson 1 | by TechwithJulles | Feb, 2023 | Medium

Part 4: Getting Started with Python: Operators | by TechwithJulles | Medium | Medium

Python for Beginners — Datatype — YouTube

Article Notebook — Google Drive

If you enjoyed this article and would like to show your support, feel free to buy me a coffee! Your support is greatly appreciated and it helps me continue creating content that you love. You can make a contribution by following this link: Buy Me a Coffee. Thank you for your generosity and for being a part of this journey!

--

--

TechwithJulles

I'm a software developer who enjoys teaching people about programming and the tech world. #TechWithJulles