0% found this document useful (0 votes)
5 views

3_Variables and Datatypes

The document provides an overview of variables and data types in Python, explaining what variables are, how to declare and assign them, and the concept of constants. It details various data types available in Python, including numeric, boolean, sequence types, dictionaries, sets, and their mutability. Additionally, it covers how to check data types and the differences between mutable and immutable collections.

Uploaded by

Arif Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

3_Variables and Datatypes

The document provides an overview of variables and data types in Python, explaining what variables are, how to declare and assign them, and the concept of constants. It details various data types available in Python, including numeric, boolean, sequence types, dictionaries, sets, and their mutability. Additionally, it covers how to check data types and the differences between mutable and immutable collections.

Uploaded by

Arif Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Variables and Data types

Learning objective
• What is a Variable?
• Declaration of variable
• Variable assignment
• Constants
• Data types in Python
• Checking Data type
• Mutable and Immutable Collections
What is a variable?
• Generally, while doing programming in any programming language,
you have to utilise different variables to store different data.

• Variables are memory areas to store values. This implies, when


you declare a variable, you hold some space in memory.

• Variables are nothing but reserved memory locations to store


values. This means when you create a variable, you reserve some
space in memory.
What is a variable?
• Variables are used to hold data in memory.

• In Python, variables are untyped, meaning you do not need to


specify the data type when creating the variable.

• You simply assign a value to a variable. Python determines the


type by the value assigned.
What is a variable?
• Variable names are case sensitive, meaning that age is different
from Age.

• By convention, variable names are written in all lowercase letters


and words in variable names are separated by underscores (e.g.,
home_address).

• Variable names must begin with a letter or an underscore


Declaring Variables
• Variables are containers for storing data values. Unlike other
programming languages, Python has no command for declaring a
variable.

• A variable is created the moment you first assign a value to it.


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.


Assigning values to a variable
• Python variables do not need explicit declaration to reserve
memory space.

• The declaration happens automatically when you assign a value to


a variable. The equal sign (=) is used to assign values to variables.

• The operand to the left of the = operator is the name of the


variable and the operand to the right of the = operator is the value
stored in the variable.
Assigning values to a variable
• There are three parts to variable assignment:
1. Variable name.
2. Assignment operator.
3. Value assigned. This could be any data type.

• Simultaneous Assignment
• A very cool feature of Python is that it allows for simultaneous assignment.
• The syntax is as follows:
• var_name1, var_name2 = value1, value2
Assigning values to a variable
• For example :
Age = 20 # An integer assignment
Percentage = 71.58 # A floating point
Name = “Rossum" # A string
print (Age)
print (Percentage)
print (Name)

Here 20, 71.58 and Rossum are the values assigned to Age, Percentage
and Name variable respectively.
Multiple Assignment
• Python allows you to assign a single value to several variables
simultaneously.

x = y = z = 10
• Here, an integer object is created with the value 10, and all three
variables are assigned to the same memory location. You can also
assign multiple objects to multiple variables. For example:

a, b, c = 1, 2, "john"
• Here, two integer objects with values 1 and 2 are assigned to variables
a and b respectively, and one string object with the value "john" is
assigned to the variable c.
Constants
• In programming, a constant is like a variable , but, unlike
variables, constants are not variable, they are constant.

• Python doesn’t really have constants, but as a convention,


variables that are meant to act like constants are written in all
capital letters. For example:

PI = 3.141592653589793
RED = "FF0000"
Data Types in Python
• In Python programming, objects have different data types.

• The data type determines both what an object can do and what
can be done to it.

• For example, an object of the data type integer can be subtracted


from another integer, but it cannot be subtracted from a string of
characters.
Data Types in Python
• Data types are the classification or categorization of data items.

• Data types represent a kind of value which determines what


operations can be performed on that data.

• Python provides various standard data types that define the


storage method on each of them.
• Numeric
• Dictionary
• Boolean
• Set
• Sequence Type
Data Types in Python
Numeric Data Types
• int: Integer, a whole number (positive or negative) without
decimals.
x = 10
• float: Floating-point number, a number with a decimal point, upto
15 decimal places
x = 10.5
• complex: Complex number with a real and imaginary part.
x = 3 + 5j
Boolean Data Type
• Data with one of two built-in values True or False.
• Notice that 'T' and 'F' are capital.
• ’true’ and ’false’ are not valid Booleans and Python will throw an
error for them.

• Example: x = True

• Please note that Python is a case sensitive programming


language. Thus, Manpower and manpower are two different
identifiers in Python.
Sequence Data Types
• A sequence is an ordered collection of similar or different data types.
• Python has the following built-in sequence data types:

• String: A string value is a collection of one or more characters put in


single, double or triple quotes.

• List : A list object is an ordered collection of one or more data items,


not necessarily of the same type, put in square brackets.

• Tuple: A Tuple object is an ordered collection of one or more data


items, not necessarily of the same type, put in parentheses.
Sequence Data Types
• str: String, a sequence of characters.
a = "Hello Python”
A=‘Arif’
M=‘’’Muscat’’’

• list: Ordered and mutable collection.


y = [10, 20, 30, “arif", 2000]

• tuple: Ordered but immutable collection.


z = (10, 20, 30, “arif", 3000)
Sequence Data Types
• A mutable collection refers to a data structure whose contents (elements)
can be changed after the collection has been created.
• This means that you can add, remove, or modify elements within the
collection without needing to create a new object.
y = [1, 2, 3, "arif", 2000]
print(y)
y[0]=100
print(y)
y.append(4)
print(y)
y.remove(3)
print(y)
Dictionary Data Type
• An unordered collection of data in a key:value pair form.
• Dictionary datatypes are mutable collection of key-value pairs.
• A collection of such pairs is enclosed in curly brackets.

D = {1:”Physics”, 2:“Chemistry", 3:“Maths“}


print(D)
D[1]=“arif”
print(D)
Set Data Type
• Set are Unordered and mutable collection of unique items.
• It is iterable, mutable (can modify after creation), and has unique
elements.
• The set is created by using a built-in function set(), or a sequence
of elements is passed in the curly braces and separated by the
comma.
• Example:
set1 = set() #Empty set
set2 = {'James', 2, 3,'Python'}
Frozenset Data Type
• Unordered and immutable collection of unique items.
• Example:

set3 = frozenset([1,”arif”])
print(set3)
print(type(set3))
set3.append(40)
set3[0]=90
None Type
• Represents the absence of a value or a null value

A=None
How to check the data type?
• Use the function type()
• Display which class a variable or a value belongs to

A=100
print(type(A))
B=100.3433
print(type(B))
Benefits of Mutable Collections
• List: You can modify elements, add new elements, or remove
elements.
• Set: You can add or remove elements from a set, though sets are
unordered.
• Dictionary: You can add, remove, or modify key-value pairs.

• You can make changes to the original object without needing to


create a new one, thus more EFFICIENT
• You can easily adjust their contents based on program logic, thus
more FLEXIBLE.
Immutable Collections
• Immutable collections (like tuples and strings) do not allow
changes after they are created.

• If you need to modify them, you will need to create a new


collection with the desired changes.
Datatypes – in brief
1. boolean (bool) – A True or False value.
2. integer (int) – A whole number.
3. float (float) – A decimal.
4. string (str) – A sequence of Unicode characters.
5. list (list) – An ordered sequence of objects, similar to an array in other
languages.
6. tuple (tuple) – A sequence of fixed length, in which an element’s
position is meaningful.
7. dictionary (dict) – An unordered grouping of key-value pairs.
8. set (set) – An unordered grouping of values.
You have Learnt:
• What is a Variable?
• Declaration of variable
• Variable assignment
• Constants
• Data types in Python
• Checking Data type
• Mutable and Immutable Collections

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy