Getting started in python variables
Hi friends welcome back to another tutorial in python programming language, in our previous tutorial in python we learnt the origin of python and how to install python on a windows computer. now in this tutorial we will be going further in learning about variables in python.
Here’s what you’ll learn in this tutorial: You will learn how every item of data in a Python program can be described by the abstract term object, and you’ll learn how to manipulate objects using symbolic names called variables.
TABLE OF CONTENTS
Introduction
A variable is a general and fundamental concept in every programming language. It is a reserved memory location that stores and manipulates a given set of data. This tutorial on Python variables will help you learn more about what they are, the different data types of variables, and the rules for naming variables in Python.
Variables are entities of a program that holds a value. Here is an example of a variable:
variable1 = 'pyduino.blogspot.com'variable2 = 3044 If you’re writing a complex code, your program will need data that can change as program execution proceeds.Think of a variable as a name attached to a particular object. In
Python, you don't need to declared or defined a variable in advance, as is the
case in many other programming languages. To create a variable, you just
assign it a value and then start using it. Assignment is done with a
single equals sign (=). Python allows the use of chained assignment, which makes it possible to assign the same value to several variables simultaneously, e.g;
>>> c = f = z = 590the chained assignment above assigns 590 to the variables c, f Types of variables in python programming language
In many programming languages, variables are statically typed. That means a variable is initially declared to have a specific data type, and any value assigned to it during its lifetime must always have that type.
Variables in Python are not subject to this restriction. In Python, a variable may be assigned a value of one type and then later re-assigned a value of a different type:
>>> var = 2366
>>> print(var)
2366
>>> var = "pyduino.blogspot.com"
>>> print(var)
pyduino.blogspot.comWe can even store a variable to our variable, or an operation with our
variables to a variable. Something like var3 = (var2/var1) would work.
You can store other things, like functions, as well to variables. In all of the examples above, we only assigned a single value to the
variables. Python has specific data types or objects that hold a
collection of values, too. A Python List is one such example. var = [15,16,17] You can extract the values from the list using the index position method. In lists, the first element index position starts at zero, the second element at one, the third element at two, and so on.
To extract the first element from the list x:
print(x[0])
>>> 15
To extract the second element from the list x:
print(x[1])
>>> 16
To extract the third element from the list x:
print(x[2])
>>> 17
Suppose you want to assign values to multiple variables. Instead of having multiple lines of code for each variable, you can assign it in a single line of code just like below.
(x, y, z)=5, 10, 5
print(x,y,z)
>> 5 10 5.
Rules in naming variables
1. A variable name must begin with a letter of the alphabet or an underscore(_)
Example: abc=100 #valid syntax
_abc=100 #valid syntax
3a=10 #invalid syntax
@abc=10 #invalid syntax
2. The first character can be followed by letters, numbers or underscores.
Example: a100=100 #valid
_a984_=100 #valid
a9967$=100 #invalid
xyz-2=100 #invalid
3. Python variable names are case sensitive.
Example: a100 is different from A100.
a100=100
A100=200
4. Reserved words cannot be used as variable names.
Example: break, class, try, continue, while, if
break=10
class=5
try=100

0 Comments