Learn Python With Dhawal - 3 - Chapter


CHAPTER 3

In this chapter we are going to learn more about variables and their types. I'll be only covering the things that you need to know while coding whereas you can always look up the internet for more detailed information.

What is a Variable?

A Variable is simply an element which varies i.e. It changes it's value depending upon the circumstances. You can put anything inside a variable, but a variable is always of one single data type. There are many types of variables and they might slightly differ from one programming language to another. Ex String, It is an array in C, but you can have a string variable in Python and Java.

Scope of a Variable

Think of it as life of a variable. Each variable has its life and during that life it'll exist and hold a value. A variable's life can depend upon where it's declared. If it is declared within a function or a loop then it's life is as long as that function or loop is running or is into existence. If it's declared within a class then it's life will be as along as the class exists.

It is very important to know the scope of the variable so as to know how to declare and assign its values. You can only declare a variable once but can assign its values as many times as you wish throughout the scope of that variable.

You can't use one same for two different variables, as you might have learned in earlier chapters. Each variable name should be unique within its scopes. In generally, you don't use a same variable within one program to avoid confusion as well as other errors as many compilers work in a different manner.

In Python, it is very easy to create a variable as you just have one step. Declaring a variable is very easy as you don't have to specify a datatype as python does that for you. Still you need to know what type of variable it'll be so you can operate on it.

Data Types In Python


  1. Int : Integer is often represented by int in many programming languages. In python whenever you assign a number without decimal point values or floating point values as we say it technically, it is automatically classified as an integer.

    There are many types of Integer such as short Int, long Int, each of these have their specific importance and hold a certain type of values. Your integer value can also depend upon whether its a signed Integer or an Unsigned Integer.

    For Ex :
    a= 1, unsigned integer
    b= -3, signed integer
  2. Float : Floating point Integer is something which is quite same as the integer but only difference being it accounts values after the decimal point as well.

    For Ex:
    f= 10.34 ,Unsigned float with values after decimal point until two places
    r=12.00 , Unsigned Integer represented as float
    e= 13.4 , Unsigned float with a value after a decimal point.
    rt=-12.99 , Signed float with values after decimal point until two places 
  3. Complex : It is used to hold complex numbers as the name suggests.
    For Ex :
    g = 3i , complex number
  4. String : Generally strings in python are represented using double quotes but single quotation marks can also be used.
    For Ex:
    a="It is a very sunny day."
    b='It was a very cold night.'
  5. List: As fore-mentioned in the previous chapter, List is a very versatile data type. You have lists in JAVA but that list is a little bit different than the one you have in python. Here you can add numbers, floating numbers, characters, strings together in one single list. It's like an array of multiple data types clubbed together.
    For Ex:
    p=[1,2,3,4,5,6]
    pts=['hello','how are you','?']
    th=[1,2,'hey']
    To print or access any item in the list you have to specify its position in the list. Always remember in computers, the number list begins from 0 and not 1 unlike the real world. So be careful while counting the position number.
    Also, printing lists is very similar to printing your normal variable.
  6. Tuple : It is very similar to your list but the only thing which makes it unique is that list can be edited as many times as you want but you can't edit a tuple once it is created.
  7. Dictionary : It is one unique and fun feature in python where you can create your own dictionary which can be used to get data easily based on the key you assigned to the value.
    For Ex:
    a={1:"firstname",2:"last name","age":33}
    In lists you used to remember the position of the item but what if you don't know the item position? That's where dictionary comes in handy. Instead of using the position you can call the values using the key that you had assigned to them.
Important Note : While naming the variables keep in mind not to use keywords (reserved words that are used in language) or the duplicate name. Also, you can use numbers while naming a variable but don't use only number to name a variable or begin naming with a number, whereas you can use only character to name a variable. Also, ensure to make some sensible names for the variable. Here, I am just using a,b,c, etc.. as of now to keep it easy for you and for me. as we go ahead I'll start naming and using the variables as we do it in the industry.

Working with variables

Code 1
Input:
1. a= 3
2. b= 4.98
3. print(a+b)
4. c= b-a
5. print (c)
Output:
7.98
1.9800000000000004
As you'd notice here, any arithmetic operation on integer and float gives result as a floating number.


Code 2 
Input:
1. a= 1
2. c= 3+1j
3. print (a+c)
Output:
(4+1j)

As you'd notice here, just like operations on integer and float resulted in floating number as output, similarly operation on integer and complex resulted as complex number in output.


Code 3 
Input:
1. a= 1.99
2. c= 3+1j
3. print (a+c)
Output:
(4.99+1j)
As you'd notice here, just like the operations above, any operations on floating numbers and complex numbers would result in a complex number with a floating point value as answer.



Code 4
Input:
1. a= 1.99
2. c= 3+1j
3. d= (a+c)
4. print(d)
Output:
(4.99+1j)

Now in a new input box, type each line one by one in new box the next few lines and see what do you get.

Input:
1. type(a)
Output:
float


Input:
1. type(c)
Output:
complex

Input:
1. type(d)
Output:
complex

As you'd see, you can use the type() command to know the type of the variable in python.


Code 5
Input:
1. a=[1,2,3,"hello"]
2. print (a)
3. print (a[3])
Output:
[1, 2, 3, 'hello']
hello

This is how you create and call upon an element in list. a[3] will bring the last element of the list and you are now well versed with our print statement right? When you use square brackets it'll create a list. Keep it in mind reason you'd know with our next few coding examples.



Code 6 
Input:
1. a=(1,2,"this is a tuple")
2. print (a)
3. print(a[2])
Output:
(1, 2, 'this is a tuple')
this is a tuple


Code 7 
Input:
1. a={1:'apple',2:'ball','tiger':'animal','Age':12}
2. print (a[1])
3. print (a[2])
4. print (a['tiger'])
5. print (a['Age'])
Output:
apple
ball
animal
12


Code 8
Input:
1.a='this is a string'
2. print(a)
Output:
this is a string

In Next chapter we will learn about conditional statements and how to use them.

<< PREVIOUS CHAPTER || NEXT CHAPTER >>

~*~*~*~

This series is totally authored by me (Dhawal Joshi). Any similarities found on the text, or codes or anything is purely accidental. All the sources of reference will be mentioned, linked and will be given the proper credits. If I miss anything or there's anything wrong, feel free to comment or send me an email and I'll try to edit it out. I am not a Python expert, I am sharing whatever I have learnt on my own and with a few sources around to refer from which will be mentioned. Also feel free to share this series with others so most can benefit out of it.

~*~*~*~


Do comment and share your thoughts about it! I'd love to know what do you think. Also, I'd keep updating it quite often so do follow the Website to get all the updates by clicking here.

Also, a minor headsup.... Obsessed is free to read on Kindle Unlimited! Do check it out. I'd be glad to read your reviews!
Share :

Are you getting regular fresh content updates? If not click on the button below.

0 comments:

Post a Comment

Do comment and share your thoughts.