Python as we discussed before, can be used both procedurally and in a manner of OOP, hence the explanation for its multiple and various data types and supporting variables. One of the most important things to note in Python are the non-explicit nature of variables, they do not need to be specially declared for the interpreter to read them but they are automatically detected by the system and Python. 

Python Variables

You can think of variables like boxes that contain something, whenever you need access to a specific item, you will have to access its specific box. Variables in python, as mentioned earlier do not require explicit declaration and can be termed as intuitive for both user and interpreter. 

Declaring a variable goes as follows:

x=4   "This is defined as an integer"
x="4"  "This is defined as a string"
x= 4.01 "This is defined as a float"

print(x)

You can use the aforementioned steps to declare a variable implicitly. If you want to specify the type of variables you need for a specific task you can do so by using the following syntax.

x= int(4)  //This will declare it as an int value.
x= float(4) //This will declare it as a floating point value.
x= str(4) //This will declare it as a string value. 

The above process is also called Casting and will declare the value as a specific type of data that you will assign while declaring the variable.

Python is Case-Sensitive

It is common knowledge that Python is a case-sensitive language and so if you want to declare a variable with the same alphabet in upper and lower cases, it should work relatively fine. Check out the example:

A = "hi there"  //String
a = "hi there" //Both Strings contain the same value and the same alphabet but since python is case-sensitive, when 'a' is called it will return the values in lower-case a and then if 'A' is called, it will return the value in uppercase A.

Checking Out the Declared Type

If you are unsure about the type of data declared in your variable and you need your program to know about it and you did not use casting to have pre-hand knowledge of the data type, you can use pretty simple print syntax to call the type of the data stored in the variable.

A = "this is a string"
b= 5 

print(type(a))

print (type(b))

//These commands will output the type of the data in the variable containers 'A' and 'b' as a print statement.

Python Data Types 

You have already learned about variables in python, now let’s talk about the stuff that the variables contains, you learned about int, float and string but there are numerous data types available in python, take a look at this, these are some of the most visible data-types in python:

. Integer: x=3  

. Float: x=3.001 

. String: x=”your very own string”  

List:

List might seem like a newer concept to you, but it is simply an editable variable that can hold multiple values, just like an array in any other language, here is how you can declare them:

X = ["item1", "item2", "item3"]  // each of the items in this list/array is separated by a comma that defines the breakage between items, this is also an implicit declaration.

Tuple:

A tuple is completely similar to that of a list, the only difference between them is that a list can be altered and tinkered around with, while a tuple is non-mutable meaning that you can make changes in it once declared, use this syntax to declare a tuple implicitly. The difference in the syntax between list and tuple is that tuple uses round brackets instead of square.

X = ("item1", "item2")

Dictionary:

Dictionary or dict is a multi-level list which contains keys and their values, the easiest way to declare them implicitly is as follows:

X = [{"name" = "your name", "ID" = 4564}]

Set:

A set is similar to a dictionary but it only contains values and does not have a separate pointer or built-in container. Use the following syntax to declare it:

X = {"xyz", "123", "hey"}

Bool:

A boolean data type is either simply 0 or 1, false or true, it is usually used as a flag to determine a true and false state of any operation, it is declared as follows:

X = true
X= false

Type Conversion in Python

A type converter command or type conversion is simply the name of changing the data type one to another mid-operation or further on in the code. It is generally utilised on pre-declared variables that require a different type to function somewhere in the code. Use the following commands to initiate type conversion explicitly:

X = 3040

A = float(x)

print(A) //This will produce an output which will convert X from int type to float type and the resultant value would be ‘3040.0.

X = 560 
A = str(X)
print (A) //This will convert X into a string and show it on the screen.

X = 'heythere'

A = list(X)

print(A) // This will convert the string into separate characters and place those chars in a listed manner.

X = 'heythere'

A = tuple(X)

print(A) // This will convert the string into separate characters and place those chars in a tuple which cannot be later altered.

X = 'heythere'

A = set(X)

print(A) // This will convert the string into separate characters and place those chars in a set.

tp= (("Hey", "hi"), ("ID" , 3456) , ("BJ", 5))

Enough with the data types, these are the essentials you need to know, let’s move on the variable scopes

Scopes

Any variable that is declared and called inside the function which can only act inside that specific function is called a scope variable. It has inheritance properties and can be accessed from sub-functions in a main function. Check out the syntax.

Def func ():

x=7264

a= x + 15

print (A) // Here, the variable of x is created and it is only accessed through this function and not anywhere else.

A function in a function also works,

Def func():

x=7264

a= x + 15

Def func2(): 
print(a) 

// Here, the variable of x is created and it is only accessed through this function and not anywhere else but only is active in san fierro.

Any function declared in the global setting, cannot be denied by Kratos, you will allow them sukoon se!

Categorized in: