DEV Community

Cover image for Data Types in Python

Data Types in Python

Suleiman Ibrahim on December 05, 2021

Have you ever stored some piece of information in the computer and wondered how the computer represents these information? Well, for you it might s...
Collapse
 
michaelcurrin profile image
Michael Currin • Edited

Some notes

This is very uncommon. Possibly I've never seen this

# singleton tuple
single = 2,
print(single)
Enter fullscreen mode Exit fullscreen mode

Rather add the brackets.

single = (2,)
Enter fullscreen mode Exit fullscreen mode

This is incorrect and needs comma.

# tuple with strings
string_tuple = ('Hello')
Enter fullscreen mode Exit fullscreen mode

Simply adding brackets is meaningless there.

Make sure to add the comma:

# string
a = ("Hello")
# "Hello"

# tuple
b = ("Hello",)
# ("Hello",)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
princeibs profile image
Suleiman Ibrahim

Thank you for the review on the string_tuple variable. It was a typo. I'll effect the change immediately.

Collapse
 
michaelcurrin profile image
Michael Currin

I wrote a cheatsheet for Python data types and how to use them in case you are interested. I keep the focus on syntax and examples

michaelcurrin.github.io/dev-cheats...

Collapse
 
princeibs profile image
Suleiman Ibrahim

Great cheatsheet over here @michaelcurrin . Is it open for contribution? I want to see how I can add to it.

Collapse
 
michaelcurrin profile image
Michael Currin • Edited

Sure. Check the github link in the corner.

I'm accepting new content for the languages and tools covered.

Make a PR for a small change or an issue to discuss a bigger change first

Collapse
 
michaelcurrin profile image
Michael Currin • Edited

You may be interested in the raw string.

Useful if you want literal text instead of special characters. Or for regex.

x = r"abc\n\\def"
print(x)
# abc\n\\def

y = "abc\n\\def"
print(y)
# abc
# \def
Enter fullscreen mode Exit fullscreen mode
Collapse
 
michaelcurrin profile image
Michael Currin • Edited

Yes, indeed True and False are just aliases for 1 and 0.

Python is implemented in C language which actually has no boolean type so 1 and 0 are used instead.

I've never seen anyone do True + True though, lol.

Collapse
 
princeibs profile image
Suleiman Ibrahim

Yeah, true. You might not see someone use True+ True in their codes because of it's less practical use. But it is necessary for a beginner to know how these things work under the hood.
Well, when I was learning Python, I didn't know that True + True will evaluate to 2. So I think including it in an example will go a long way to help others who want to learn.

Anyways, thanks for your reviews. There are absolutely on point 🚀🚀🚀

Collapse
 
michaelcurrin profile image
Michael Currin

What I mean is for a teaching a beginner syntax, get them to learn code that is best practice and common i.e. followed by most Python coders. So they pick up good habits

And then as a side note you can mention the alternative syntax without brackets as valid but not encouraged, and like you said something for den

In this case it is covered in the official style guide for Python in PEP8, which will be enforced by style and lint tools too.

Brackets around comma are recommended and leaving it out is less readable it says.

pep8.org/#when-to-use-trailing-commas

Collapse
 
princeibs profile image
Suleiman Ibrahim • Edited

Yeah. Great point here @keonigarner . I think stating personal experience may sound biased.

Thread Thread
 
michaelcurrin profile image
Michael Currin

My experience may be indeed be different from others. That's why I backed my comment with a link to the standard style Python programmers are recommended to follow. Not everyone follows it or follow it completely but it encourages quality (readable and debuggable code) and consistency (if you change codebases or change jobs you probably won't have to change styles because you alread follow the prevalent style which is documented and agreed upon).

Thread Thread
 
michaelcurrin profile image
Michael Currin • Edited

I also recommend using a tool like pylint and flake8 ans black to give warnings and automated fixes for code style.

They are going to default to a prevalent standard to avoid debate between coders on what is right, as the tools enforce a standard for you.

You can also override with a config if you want 100 characters long instead of 79 or want to change how code is wrapped or variables are named, if you want to deviate from the style.

github.com/MichaelCurrin/py-projec...