Python 1: Variables and Tuples

python-logoWelcome to first Python post. I am planning to prepare a series tutorial on Python. I will post new articles time to time from basics. In this post, I’ll be covering on variables and tuples. I’ll be covering these topics in this post:

  1. Variable
  2. Tuple

Variable

Variable is a name that is used to store any value we want to use. In any programming language, values do not matter in most of the cases if they cannot be used or accessed. For example: if you are writing a function to calculate the square of a number then you need to use the value the user inputs. To use that value, we need to have it stored by name and that name is called a variable. Try this in Python.

number = 5

Here 5 is the value and number is the variable in which we want to store the value.  In the machine we are using, variable number will be storing value 5 and it will reserve memory. After entering this line in some IDE or Python shell, we can access the value of number.

Variable Type

Variables have different data types. For eg: int, str etc. Unlike some other Programming languages, in Python, we do not need to specify the type of variable during its creation. Python will automatically detect the type of the variable and this feature of Python is called dynamic typing. In the above example, try the following code.

type(number)

output: <class ‘int’>

Here we can see, Python automatically detected the type of variable number to be int. Same is the case for other types of variables.

Rules for naming a variable

    • Python variables should start with _ or alphabet
      • example:- _first_name, _last_name, dob, gender, etc
    • Python allows you to assign single value to several variables simultaneously
      • example:- _sum = _total = _average = 97
    • You can assign a variable with different data types like integer, float, string, etc. without mentioning the data type
        example:-
	_first_name = 'Ram' #Assigning string
	_last_name = "Sth"  #Assigning string
	_age = 25           #Assigning integer
	_salary = 9000.00   #Assigning float

Tuple

Tuple is a data structure available in Python. It is used to store data like in arrays with indexes. Tuple is an immutable data structure, so cannot be updated once created. Tuple is written by enclosing data in ().

In below example, names is a variable of type tuple that stores 3 values. Tuple values can be accessed by using 0 index(i.e. index of first element is 0). To access data from end position, indexing starts from -1 and progresses to -2, -3 and so on.

names = ("Ram", "Harry", "Steve")
print(names[0])  #prints Ram
print(names[1])  #prints Harry
print(names[2])  #prints Steve
print(names[-1]) #prints Steve
print(names[-2]) #prints Harry
print(names[-3]) #prints Ram

Tuples are used as substitute of Arrays in Python since Python doesn’t have Arrays. But Tuples can store data of different types.

names = ("Ram", "Harry", "Steve", 9)

This is valid definition in Python.
We can use Tuples in a multidimensional way as well. One application would be matrix implementation.

matA = ((1, 2, 3),(4, 5, 6),(7, 8, 9)) #A 3X3 matrix definition 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			&amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;
print(matA[0][2]) #prints 3
print(matA[1][2]) #prints 6
print(matA[2][2]) #prints 9

Similar is the case for tuple of any higher dimension. Python provides various methods to operate on tuple. Some of them are max, min. These are worth taking a look at and will be helpful while working with tuples.

This much for this post. I hope this post is useful in getting started with Variables and Tuples. If you have any queries or need more explanation, please leave a comment. I’ll be happy to help.

See you in the next one. Cheers 🙂

85 total views, 1 views today

2 comments on “Python 1: Variables and Tuples

  1. Pingback: Python 2: List and Dictionary – something about a lot of things

Leave a Reply

Your email address will not be published. Required fields are marked *