Unit_1_ Variables and data types
Variable
A variable is a fundamental concept in any programming language.
It is a reserved memory location that stores and manipulates data
Python Variable is containers which store values.
We do not need to declare variables before using them or declare
their type.
A variable is created the moment we first assign a value to it.
A Python variable is a name given to a memory location.
It is the basic unit of storage in a program.
Variables are entities of a program that holds a value.
Here is an example of a variable:
x=100
the box holds a value of 100 and is named as x.
Therefore, the variable is x, and the data it holds is the value
Rules for creating variables in Python
•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 _ ).
•Identifier name must not contain any white-space, or special character (!, @, #, %, ^, &, *).
•Variable names are case-sensitive (name, Name and NAME are three different variables).
•The reserved words(keywords) cannot be used naming the variable.
The underscore character, _, can appear in a name. It is often used in names with multiple words, such as
my_name or airspeed_of_unladen_swallow.
•Examples of valid identifiers: a123, _n, n_9, etc.
•Examples of invalid identifiers: 1a, n%4, n 9, etc.
Declaring Variable and Assigning Values
Python does not bind us to declare a variable before using it in the application.
It allows us to create a variable at the required time.
We don't need to declare explicitly variable in Python.
When we assign any value to the variable, that variable is declared automatically.
The equal (=) operator is used to assign value to a variable.
Multi-word keywords can be created by the following method
•Camel Case - In the camel case, each word or abbreviation in the middle of begins with a capital letter.
There is no intervention of whitespace. For example - nameOfStudent, valueOfVaraible, etc.
•Pascal Case - It is the same as the Camel Case, but here the first word is also capital. For example -
NameOfStudent, etc.
•Snake Case - In the snake case, Words are separated by the underscore. For example -
name_of_student, etc.
Multiple Assignment
Python allows us to assign a value to multiple variables in a single statement, which is also known
as multiple assignments.
We can apply multiple assignments in two ways, either by assigning a single value to multiple
variables or assigning multiple values to multiple variables.
1. Assigning single value to multiple variables
Output:
x=y=z=50
print(x)
50
print(y)
50
print(z)
50
Re-declare the Variable
We can re-declare the python variable once we have declared the variable already.
# declaring the var
Number = 100
# display
print("Before declare: ", Number)
# re-declare the var
Number = 120.3
print("After re-declare:", Number)
Output:
Before declare: 100
After re-declare: 120.3
Assigning multiple values to multiple variables:
a,b,c=2,4,6 Output:
print (a) 2
print (b) 4
print (c) 6
a=2 Output:
b="genome“
print(b+str(a)) genome2
Python Variable Types: Local & Global
When you want to use the same variable for rest of your program or module you declare it as a global variable, while
if you want to use the variable in a specific function or method, you use a local variable while Python variable
declaration.
Local variables are the ones that are Global variables are the ones that are defined
defined and declared inside a function. and declared outside a function, and we need to
use them inside a function.
We can not call this variable outside the
function.
# This function has a variable with
# name same as s.
# This function uses global variable s
def f():
def f():
s = “Genome data science"
print(s)
print(s)
# Global scope
f()
s = “Genome informatics"
f()
Global Variables
Global variables can be used throughout the program, and its scope is in the entire program. We can use global variables
inside or outside the function.
A variable declared outside the function is the global variable by default. Python provides the global keyword to use
global variable inside the function.
If we don't use the global keyword, the function treats it as a local variable.
Declare a variable and initialize it
x = 101
# Global variable in function
def mainFunction():
# printing a global variable
global x
101
print(x)
'Welcome To Bioworld
# modifying a global variable
'Welcome To Bioworld
x = 'Welcome To Bioworld'
print(x)
mainFunction()
print(x)
a="Genome data scinece“
Print(a)
def somefunction():
a="Genome informatics"
print(a)
somefunction()
print(a)
Variable type in Python
Data types are the classification or categorization of data items.
It represents the kind of value that tells what operations can be performed on a particular data.
Since everything is an object in Python programming, data types are actually classes and variables are instance
(object) of these classes.
Following are the standard or built-in data type of Python:
•Numeric
•Sequence Type
•Boolean
•Set
•Dictionary
Python Data Types
Variables can hold values, and every value has a data-type.
Python is a dynamically typed language; hence we do not need to define the type of the variable while declaring it.
The interpreter implicitly binds the value with its type.
a=5
The variable a holds integer value five and we did not define its type. Python interpreter will automatically interpret
variables a as an integer type.
Python enables us to check the type of the variable used in the program. Python provides us the type() function, which
returns the type of the variable passed
a=10
b="Hi Python"
c = 10.5 <type 'int'>
print(type(a)) <type 'str'>
print(type(b)) <type 'float'>
print(type(c))
Standard data types
A variable can hold different types of values. For example, a person's name must be stored as a string whereas its id must
be stored as an integer.
Python provides various standard data types that define the storage method on each of them.
The data types defined in Python are given below.
1.Numbers
2.Sequence Type
3.Boolean
4.Set
5.Dictionary
Numbers
Number stores numeric values. The integer, float, and complex values belong to a Python Numbers data-type.
Python provides the type() function to know the data-type of the variable.
a=5
print("The type of a", type(a))
Output:
b = 40.5
The type of a <class 'int'>
print("The type of b", type(b))
The type of b <class 'float'>
The type of c <class 'complex'>
c = 1+3j
c is complex number: True
print("The type of c", type(c))
print(" c is a complex number", isinstance(1+3j,complex))
1.Int - Integer value can be any length such as integers 10, 2, 29, -20, -150 etc. Python has no restriction on the length of an
integer. Its value belongs to int
2.Float - Float is used to store floating-point numbers like 1.9, 9.902, 15.2, etc. It is accurate upto 15 decimal points.
3.complex - A complex number contains an ordered pair, i.e., x + iy where x and y denote the real and imaginary parts,
respectively. The complex numbers like 2.14j, 2.0 + 2.3j, etc.
Python supports four different numerical types −
•int (signed integers)
•long (long integers, they can also be represented in octal and hexadecimal)
•float (floating point real values)
•complex (complex numbers)
Example Data Type
x = "Hello World" str
x = 20 int
x = 20.5 float
x = 1j complex
x = ["apple", "banana", "cherry"] list
x = ("apple", "banana", "cherry") tuple
x = range(6) range
x = {"name" : "John", "age" : 36} dict
x = {"apple", "banana", "cherry"} set
x = True bool
Sequence Type
String
The string can be defined as the sequence of characters represented in the quotation marks. In Python, we
can use single, double, or triple quotes to define a string.
String handling in Python is a straightforward task since Python provides built-in functions and operators
to perform operations in the string.
In the case of string handling, the operator + is used to concatenate two strings as the operation "hello"+"
python" returns "hello python".