Day 2: Python Newbie?

Variable :

Adnan Khan
2 min readNov 26, 2019

A python variable is a reserved memory location to store values.

OR

A container for storing data values.

How to declare a variable?

var = "a"

That’s how we can simply declare variable of type String. Similarly you can declare variable of other types as well like integer or float.

var1= 12.56

The process of linking two or more strings together is called concatenation.

You can concatenate variables as well by simply putting + sign among them. However you can’t concatenate variables of different type like string and int.if you want to concatenate them then you have to change the type of variable.Lets see how

a = "He's monthly salary is"
b = 80000
result = a+ " " +str(b)

If you’re not convert the type then it would give a “Traceback error”.However you can add values of float and integer types because both store the numeric values.

Rules for declaring variable:

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0–9, and _ )
  • Variable names are case-sensitive (age, Age and AGE are three different variables)
  • A variable name should not contain reserve words.

Local & Global Variable:

let’s simply understand through code

var_A=10
def someFunction():
var_B="Hello"
print(var_B)

The variable var_A which is outside the function is “Global variable” and it has global scope. Which is accessible inside as well as outside the function.

The variable var_B which is inside function is “Local variable” and it has only local scope.It’s accessible only inside the function not from outside.

Note:

As python is a case sensitive language.So variables are also case sensitive it means that variable result is different from Result.

Type():

If you declare a variable and you forget later the what was the type of variable A? so you can simply check by using the built-in type function.like this,

type(A)

So that was all about variable. More about python we will discuss tomorrow we will go step by step and clear each and every concept.This article was very limited one but i know that going from less to more produces more interest for beginners.Don’t forget to share your valuable feedback.

--

--

Adnan Khan

Data Scientist who loves to teach machine through data.