DEV Community

Cover image for Python - Data types
Flavio Campelo
Flavio Campelo

Posted on • Updated on

Python - Data types

📮 Contact 🇧🇷 🇺🇸 🇫🇷

Twitter
LinkedIn


Data types

Python infers data types of variables. This means that you don't need to write string, boolean, int or others to explicit declarations to work with variables. You can specify the type of a variable using cast for that.

The basics for write a variable and its value is variable name + equals sign + variable value. That will create a new variable and its type will be infered by the value type.

myVariableName = myValue
Enter fullscreen mode Exit fullscreen mode

I'll show you some data types below, but you can learn more about other ones on this site

Integer

myInteger = 3 # infered data type
Enter fullscreen mode Exit fullscreen mode

or

myInteger = int(3) # explicitly
Enter fullscreen mode Exit fullscreen mode

Float

myFloat = 3.5 # infered data type
Enter fullscreen mode Exit fullscreen mode

or

myFloat = float(3.5) # explicitly
Enter fullscreen mode Exit fullscreen mode

String

myString = 'Have a nice day!' # infered data type
Enter fullscreen mode Exit fullscreen mode

or

myString = str('Have a nice day!') # explicitly
Enter fullscreen mode Exit fullscreen mode

You can use double quotes " or single quotes ' to delimit the string. When you're using the first one, it'll be easier to write single quotes inside your value string.

myString = "Jhon's house"
Enter fullscreen mode Exit fullscreen mode

Using single quotes everywhere, you should to scape *\* all of the single quotes in the value.

myString = 'Jhon\'s house'
Enter fullscreen mode Exit fullscreen mode

Boolean

myBool = True
Enter fullscreen mode Exit fullscreen mode

or

myBool = bool(True)
Enter fullscreen mode Exit fullscreen mode
Notes

You can access this code on github.

Typos or suggestions?

If you've found a typo, a sentence that could be improved or anything else that should be updated on this blog post, you can access it through a git repository and make a pull request. If you feel comfortable with github, instead of posting a comment, please go directly to https://github.com/campelo/documentation and open a new pull request with your changes.

Discussion (0)