2.1 Python Variables PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Variables in Python Programming

Let's try to learn how to declare variables in Python.

Variables ve Decraling Variable


A variable is a location in memory used to store some data (value).

Let's create and declare, create and use a variable in Python.

In [1]: # A Variable Name and it's value


i = 5

That's it! We've created our variable. Perfect!

So, what if we want to print on the screen this variable?

In [2]: i

Out[2]: 5

When we print our variable to the screen, the value of our variable is 5.

What will be the result of this calculation below?

In [3]: i * i * i

Out[3]: 125

The result is third power of 5. Let's change value of variable.

To do this, we're assigning (with "=" this operator) new value to our variable.

In [4]: i = 6
i

Out[4]: 6

Multiple Variable Assignment


We can also assign a single value to more than one variables simultaneously.

In [5]: x = y = z = 1
We can also assign multiple values to multiple variables at the same time using with commas.

In [6]: a,b,c = 3,4,5


print(a)
print(b)
print(c)

3
4
5

Now let's try to create 3 different variables.

In [7]: a = 2
b = 3
c = a + 2 * b

What will be variable c? In this calculation, first on the right side of the = operator is being calculated,

and this calculated value is assigned to the variable c.

In [8]: c

Out[8]: 8

Python Variables Naming Rules


Let's talk about the important points that we should notice, when we're naming our variables.

Can not be no spaces between them, if the variable name consist of more than 1 word.
Can not be used reserved words.
Must begin with a letter (a - z, A - Z) or underscore (_)
A variable name **can not** **start** with a **digit**.
Variable names are **case sensitive**
Can not be used **special characters**!
Can be any (reasonable) length

Reserved Keywords in Python Programming Language

and as assert break class

continue def del elif else

except exec from finally for

from global global if import

in is lambda not or

pass print raise return try

while with yield raise


In [9]: #Failed
i? = 5

File "<ipython-input-9-d42a721f6760>", line 2


i? = 5
^
SyntaxError: invalid syntax

In [10]: #True Usage


_i = 15

Let's one more example and calculate the circumference and area of a circle

Guess the result of this calculation without looking at the results.

In [11]: pi = 3.14
radius = 2
circ = 2 * pi * radius

In [12]: circ

Out[12]: 12.56

Let's try to exchange values of two variables.

Let's say that we have a = 3, b = 5.

How can we do this?

In [13]: a = 3
b = 5
print(a,b)

3 5

In [14]: temp = a
a = b
b = temp
print(a,b)

5 3

But, there is a much practical way to exchange values of two variables in Python.

In [15]: a = 3
b = 5

In [16]: a,b = b,a

In [17]: a

Out[17]: 5

In [18]: b

Out[18]: 3

Even, we can do this operation with tripled one!


In [19]: a = 3
b = 4
c = 5

a,b,c = c,a,b
print(a)
print(b)
print(c)

5
3
4

Changing Value of Variables:


Now, let's look at changing value of a variable.

Let's look at the code below.

In [20]: a = 2
a = a + 1
a

Out[20]: 3

There is a practical method of changing the value of a variable in Python programming.

In here, we can write this "a = a + 1" as "a += 1" as well.

This operator is also called Compound Assignment Operator.

And this representation is more Pythonic!

In [21]: a = 5
a += 1 # same as a = a + 1.
a

Out[21]: 6

In [22]: b = 4

b -= 1 # b = b -1
b

Out[22]: 3

In [23]: b = 4
b *= 10
b

Out[23]: 40

How to use comments in Python


When we're writing our programs sometimes, we want to write an explanation what the code does or sometimes we need
to explain what we're trying to do anywhere in our code. But we don't want these lines to be run. In such situations, we use
comment lines.

If the comment lines are used in a program, these lines are not visible to Python and can not be executed by compiler.
In [24]: # Single Line Comment. This is a comment
print("omer") #This is also a single comment.

omer

In [25]: """
This is
Multiple Line Comment

"""
print("omer")

omer

In [ ]:

The Complete Python Programming Course (Beginner to Advanced)

You might also like