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

Python Program Element - Variable

Python variables do not require declaration and are dynamically typed. Variables reserve memory and their type is determined by the data assigned. The main data types in Python are numbers, strings, lists, dictionaries, tuples and sets. Numbers store numeric values, strings are sequences of characters, lists are ordered mutable sequences, dictionaries store items in key-value pairs, tuples are ordered immutable sequences, and sets are unordered collections of unique items. Data types like lists, dictionaries and sets are mutable and can be changed, while numbers, strings, tuples and ranges are immutable and cannot be modified after creation. Exercises are provided to work with each of the main data types in Python.

Uploaded by

Mobile Easy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Python Program Element - Variable

Python variables do not require declaration and are dynamically typed. Variables reserve memory and their type is determined by the data assigned. The main data types in Python are numbers, strings, lists, dictionaries, tuples and sets. Numbers store numeric values, strings are sequences of characters, lists are ordered mutable sequences, dictionaries store items in key-value pairs, tuples are ordered immutable sequences, and sets are unordered collections of unique items. Data types like lists, dictionaries and sets are mutable and can be changed, while numbers, strings, tuples and ranges are immutable and cannot be modified after creation. Exercises are provided to work with each of the main data types in Python.

Uploaded by

Mobile Easy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

2.

0 PYTHON PROGRAM
ELEMENT
DFP4022 PYTHON PROGRAMMING
Variables and Data Types
in Python
2.1 Discover common
programming element using
Python
Variables in Python
 Python is completely object oriented, and not "statically typed".
 Python variable do not need to declare the variables and data type.
 The declaration happens automatically when assigning a value to a
variable. The equal sign (=) is used to assign values to variables.
Examples : y = 1+2
name = “Aliah”
 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
 Every variable in Python is an object.
 Variables reserved memory locations to store values. Some space in
memory is reserved when creating a variable.
 Based on the data type of a variable, the interpreter allocates memory
and decides what can be stored in the reserved memory.
Data Types in Python
 Numbers 
 Strings 
 Lists 
 Dictionaries 
 Tuples 
 Sets
Data Types in Python
Data Description
Type
Numbers   Number data types store numeric values. Integers, floating
point numbers and complex numbers fall under python
numbers category. They are defined as int , float and
complex classes in python.
Strings  Strings is sequence of Unicode characters. We can use single
quotes or double quotes to represent strings. Multi-line
strings can be denoted using triple quotes
Lists  Lists is an ordered sequence of items. It is one of the most
used data type in Python and is very flexible. All the items in
a list do not need to be of the same type. Lists are defined
zero-based indexed by declared using brackets [].
Dictionari Dictionaries are the most flexible built-in data type in
es python. Dictionaries items are stored and fetched by using
Data Types in Python
Data Description
Type
Tuples  A tuple is a sequence of Python objects separated by
commas. Tuples are immutable, which means tuples once
created cannot be modified. Tuples are defined using
parentheses ().
Sets A set is an unordered collection of items. Set is defined by
values separated by a comma inside braces { }.
Mutable and Immutable
Data Types in Python
 Mutable object can be changed after it is
created, and an immutable object can’t be
changed.
 Mutable data types in Python are list,

dictionary, set and user-defined classes.


 Immutable data types are int, float, decimal,

bool, string, tuple, and range.


Exercise 1: Numbers
Open Python 3.7 IDE and type as below:
Exercise 2: Strings
Open Python 3.7 IDE and type as below:
Exercise 3: Lists 
Open Python 3.7 IDE and type as below:
Exercise 4 : Dictionaries
Open Python 3.7 IDE and type as below:
Exercise 5 : Tuples 
Open Python 3.7 IDE and type as below:
Exercise 6 : Sets
Open Python 3.7 IDE and type as below:
Summary

You might also like