More about Variable in Python

Now before going to discuss what a variable is in Python in the context of programming language. We should know what the variable word means in the context of Programming language.

Variable

Variables in General Programming language

You can say a Variable is a particular container with a datatype and name, and a value that can be stored in it is called a variable. We can do multiple operations on it depending on their datatypes. Now, what is a data type? We shall discuss this in the next post. Now consider a Variable as a container or a thing that can store values means it can be of any datatype, i.e., it can be alphabets, it can be a string, it can be an integer, it can be a floating point number or complex number okay look here below the syntax of variable in general programming languages:

Syntax of Variables in General Programming language

I hope you are aware of the word Syntax. If not so, look, Syntax is a structure or way of writing particular code in a particular programming language. So for more confirmation and knowledge, you can check our detailed post on its Syntax in C#, C++, JAVA, and Python. Now here below is the Syntax of a Variable okay:

[varaible data type] + [varaible name] = [value of variable]

I also make visual representation of a variable hope you like it.

Variable

For example : making integers variable age and assigning it value 18 okay using C# syntax.

int age =18;

Variable in Python Programming language

But we are going to discuss Variables in Python now; why I have to tell you the extra information of syntax of making a variable in a general others programming languages except Python programming language. Because these are not dynamic, Python is a dynamic language means the datatype of a variable is detected at runtime means when programs run according to the value given.

For example we look over the variable that i declare above okay:

In C# we write int age =18; which means age is a integer type variable because we declare it datatype at compile time so and assign it value 18.

Now in Python we have to do like this age = 18 so when program runs it automatically detect that datatype of variable is integers so i hope you got it look below the syntax of making variable in Python programming language:

Syntax of Variable in Python Programming language

[varaible name] = [value of variable]

For example : making integers variable age and assigning it value 18 okay using Python syntax.

age =18;

I also make visual representation of a variables hope you like it.

Variable

Casting Of Variable in Python

If you want to specify the data type of a variable, this can be done with casting.

For Example:

x = str(3)    # x will be '3'
y = int(3)    # y will be 3
z = float(3)  # z will be 3.0

print(x)
print(y)
print(z)

Output:

3
3
3.0

How Can we Get Type of Variable or Python Variable??

We can get the type of any variable declared of any datatype using type() function or method okay:

x = 10
y = "Codelikechamp"
print(type(x))
print(type(y))

Output:

<class 'int'>
<class 'str'>

Single or Double Quotes String?

We can make string variable by using both Single & Double Quotes:

x = "Codelikechamp"
# is the same as
x = 'Codelikechamp'
print(x)

Output:

Codelikechamp

Python is Case-Sensitive Language

a = 40
A = "Codelikechamp"
#A will not overwrite a they are different
print(a)
print(A)

Output:

40
Codelikechamp

Global Variables

Variables that are created outside of a function/methods are known as global variables.

Global variables can be used by everyone, both inside of functions and outside.

For example:

x = "awesome"

def myfunc():
  print("Codelikechamp is " + x)

myfunc()

Output:

Codelikechamp is awesome

If you create a variable with the same name inside a function/method, this variable will be local not global and can only be used inside the function/method. The global variable whcih we have created with the same name will remain as it was, global and with the original or real value.

For example:

x = "awesome"

def myfunc():
  x = "fantastic"
  print("Codelikechamp is " + x)

myfunc()

print("Codelikechamp is " + x)

Output:

Codelikechamp is fantastic
Codelikechamp is awesome

Link: https://Codelikechamp.com

Medium Link: Follow me on Medium

Linkedin Link: Follow me on Linkedin

🤞 Don’t miss any latest posts!

Please subscribe by joining our community for free and stay updated!!!

IF YOU HAVE ALREADY SUBSCRIBED JUST CLOSE THIS FORM !

Leave a Comment

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

Scroll to Top