Python: Variables and Types

in #python3 years ago

Python is completely object-oriented, and not "statically typed". You do not need to declare variables before using them or declare their type. Every variable in Python is an object.

This tutorial will go over a few basic types of variables.

Numbers
Python supports two types of numbers - integers(whole numbers) and floating-point numbers(decimals). (It also supports complex numbers, which will not be explained in this tutorial).

To define an integer, use the following syntax:

Variables and Ty.png

To define a floating-point number, you may use one of the following notations:

Variables and Ty (1).png

Strings
Strings are defined either with a single quote or a double quotes.

Variables and Ty (2).png

The difference between the two is that using double quotes makes it easy to include apostrophes (whereas these would terminate the string if using single quotes)

Variables and Ty (3).png

There are additional variations on defining strings that make it easier to include things such as carriage returns, backslashes, and Unicode characters. These are beyond the scope of this tutorial but are covered in the Python documentation.

Simple operators can be executed on numbers and strings:

Variables and Ty (4).png

Assignments can be done on more than one variable "simultaneously" on the same line like this

Variables and Ty (5).png

Mixing operators between numbers and strings are not supported:

Variables and Ty (6).png

Exercise
The target of this exercise is to create a string, an integer, and a floating-point number. The string should be named mystring and should contain the word "hello". The floating point number should be named myfloat and should contain the number 10.0, and the integer should be named myint and should contain the number 20.

Variables and Ty (7).png

I am happy you are giving this journey a try, I believe you can do better if you add some extra effort to learn more.
thank you so much for checking out my article, if you feel this is helpful just give me an upvote and I will be glad to do more useful content.

Coin Marketplace

STEEM 0.28
TRX 0.26
JST 0.039
BTC 94224.50
ETH 3330.99
USDT 1.00
SBD 3.51