Advertisement

Responsive Advertisement

Python variables and Types

Python is completely object oriented, and not "statically typed". You do not need to declare variables before using them, or declare their type. Every variable in Python is an object.A Python variable is a reserved memory location to store values.

Source:Python-Variables-Tutorial-Types-with-example.png

A variable can be seen as a container (or some say a pigeonhole) to store certain values. While the program is running, variables are accessed and sometimes changed, i.e. a new value will be assigned to the variable. 

This Article  will go over a few basic types of variables.Python having different datatypes  are like below Numbers, List, Tuple, Strings, Dictionary, etc.Unlike other programming languages, Python has no command for declaring a variable.
Let see an example. We will declare variable "Name" and print it.

Declare variable :
Name=“Gowtham Rajamanickam” 
print (name)

Re-Declare the Variable

Variables do not need to be declared with any particular type and can even change type after they have been set.
Name=“Gowtham Rajamanickam” 
print (name)

Basic rules to create Python Variables,


  • Python Variables are case sensitive and needs to be carefully create while doing a program
  • Python variables must start with a letter, underscore  (a-Z,_)
Python String

String literals in python are surrounded by either single quotation marks, or double quotation marks.
'hello' is the same as “hello”.In the interactive interpreter, the output string is enclosed in quotes and special characters are escaped with backslashes. 
Example 

mystring = 'hello Gowtham'
print(mystring)
mystring = "hello gowtham"
print(mystring)

Python having default methods to simply the string operations .

strip() -This method removes any whitespace from the beginning or the end.

print(mystring.strip()) 

len() -This method returns the length of a string.

print(len(mystring))

lower() -This method returns the string in lower case

print(mystring.lower())

replace() -This method replaces a string with another string.
print(mystring.replace("l, “m “))

split() -This splits the string into substrings if it finds instances of the separator.

print(a.split(","))


Numbers

Python supports two types of numbers - integers and floating point numbers, complex numbers.

To define an integer, use the following syntax:
x = 1    # int

Example:
x = 1
y = 36741949
z = -997979696

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

Output:

/Users/gowthamrajamanickam/PycharmProjects/PNPDemo/venv/bin/python /Users/gowthamrajamanickam/PycharmProjects/PNPDemo/PythonDemo.py




Process finished with exit code 0

Float

y = 5.8  # float

Complex

z = 1j   # complex
Local/Global Variables
In Python when you want to use the same variable for rest of your program or module you declare it a global variable, while if you want to use the variable in a specific function or method, you use a local variable.


# Declare a variable and initialize it
f = 505
print(f)
# Global / local variables in functions
def TestFunction():

    f = 'I am Gowtham and learning  Python'
    print(f)
TestFunction()
print(f)

Output

In this example f=505 is global variable and it can be accessed after the test function and the values remain same .
Inside the function the value has changed but it is not affecting the global function.

But some cases if you want to change the global variable inside the function use the variable name called ‘global’.

# Declare a variable and initialize it
f = 505
print(f)
# Global / local variables in functions
def TestFunction():
global f
    f = 'I am Gowtham and learning  Python'
    print(f)
TestFunction()
print(f)

Output

/Users/gowthamrajamanickam/PycharmProjects/PNPDemo/venv/bin/python /Users/gowthamrajamanickam/PycharmProjects/PNPDemo/PythonDemo.py
505
I am Gowtham and learning  Python
I am Gowtham and learning  Python
Process finished with exit code 0




Delete a variable You can also delete variable using the command del "variable name".
f = 505;
print(f)
del f
print(f)

output
/Users/gowthamrajamanickam/PycharmProjects/PNPDemo/venv/bin/python /Users/gowthamrajamanickam/PycharmProjects/PNPDemo/PythonDemo.py
Traceback (most recent call last):
  File "/Users/gowthamrajamanickam/PycharmProjects/PNPDemo/PythonDemo.py", line 4, in 
    print(f)
NameError: name 'f' is not defined
505

Process finished with exit code 1


.....to be Continued


Post a Comment

0 Comments