Fandom

Python Wiki

Variables

43pages on
this wiki
Add New Page
Talk0 Share

Variables are an essential thing in all programming languages. They help us reuse multiple values more than just once.
One special thing about Python is the implicit type-inference. This means, that we do not have to assign a type to a variable by it's declaration as in Java or C. That means that the compiler does the typing by itself.

Types in Python Edit

In Python we can assing variables of many different types:

This ones are called primitive data types. More complicated types are dictionaries, ...
To view them, follow this link.

Usage of variables Edit

To assign a variable, we use this general Syntax:
variable_name = value

Now see some examples:

# declaring two strings
foo = 'foo'  
bar = 'bar'
 
# declaring an integer
# note: automatically inferred as integers
i1 = 7
i2 = 42
 
# declaring two floating point numbers
d1 = 42.7
d2 = 7.42
 
# declaring two complex numbers
c1 = 7 + 42j
c2 = 42 + 7j

Reuse of variables Edit

The main thing about variables is that we can use them more than just one time by calling them. This is done as in the following example:

# defining a variable
foo = 'awesome'
# ... and reuse it by 
# printing it's value
print(foo)    #in Python 3.X
# print foo   #in Python 2.X

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.