Data Types
Data Types
TOPICS
Python introduction
History
Installation and download
Getting started
Comments and statements
Identifiers and keywords
Python variables
LAB EXPERIMENTS
1. Python Variables, Executing Python from
the Command Line, Editing Python Files ,
Python Reserved Words.
2. Comments, Strings and Numeric Data
Types, Simple Input and Output
Completed
PYTHON CONSTANTS
Constants are usually declared and assigned
in a module
Module: New file consists variables, functions
Constants are written in all capital letters
and underscores separating the words
Hands-on
LITERALS
Literal is a raw data given in a variable or
constant.
1. Numerical
2. String
3. Boolean
4. Special
NUMERICAL
Numeric Literals are immutable
3 types
Integer(0b,0o,0x)
Float(1.5,1.5e2)
Complex(3.14j)
Hands-on
STRING LITERALS
A string literal is a sequence of characters
surrounded by quotes. We can use both
single, double, or triple quotes for a string
String ,char and multiline string (Doc string)
Hands-on
BOOLEAN AND SPECIAL
A Boolean literal can have any of the two
values
True
False
Special
Field has not been created(None)
Hands-on
DATA TYPES
Data type defines the type of the variable, whether
it is an integer variable, string variable, tuple,
dictionary, list etc
DATA TYPES
Immutable and mutable
Immutable(cant change)
Numeric
String
Tuple
Mutable(changeable)
List, dictionary and set
IMMUTABLE
Number and string
Tuple is immutable data type in Python
which means it cannot be changed.
an ordered collection of elements enclosed
in round brackets( ) and separated by
commas
Index range starting from “0”
Ex:sno=(1,2,3,4)
OPERATIONS ON TUPLE
Printing
Slicing
Concatenation(+)
Repetition (*)
Other :type and add value
Advanced :Nested Tuple(tuple in side other
tuple)
MUTABLE
We can modify the data type
List,set,Dictonary
List: The items stored in the list are
separated with a comma (,) and enclosed
within square brackets [].
Ex:list=[1,2,3,4]
Operations:
concatenation operator (+) and repetition
operator (*)
Hands-on
LIST
Tuple
SET
Set is an unordered collection of unique items.
Set is defined by values separated by comma
inside braces { }. Items in a set are not
ordered.
Ex: set={1,2,3}
Since, set are unordered collection, indexing
has no meaning. Hence, the slicing
operator [] does not work.
ADD and remove operations
Hands-on
DICTIONARY
It is generally used when we have a huge
amount of data.
Dictionaries are optimized for retrieving data
Dictionary is an unordered set of a key-value
pair of items
It is like an associative array or a hash table
where each key stores a specific value.
Key can hold any primitive data type,
whereas value is an arbitrary Python object
DECLARATION
In Python, dictionaries are defined within
braces {} with each item being a pair in the
form key: value.
Key and value can be of any type
Ex: dict ={key:'value‘}
Hands-on
print("1st name is "+d[1])
print("2nd name is "+ d[4])
print (d.keys())
print (d.values())
INDENTATION IN PYTHON
Many popular languages such as C, and Java
uses braces ({ }) to define a block of code.
Block scope
A code block (body of a function, loop, etc.)
starts with indentation and ends with the
first unindented line.
The amount of indentation is up to you, but
it must be consistent throughout that block.
Incorrect indentation will result
in ”IndentationError”
RULES
We can’t split indentation into multiple lines using
backslash.
The first line of Python code can’t have indentation,
it will throw IndentationError.
You should avoid mixing tabs and whitespaces to
create indentation. It’s because text editors in Non-
Unix systems behave differently and mixing them
can cause wrong indentation.
It is preferred to use whitespaces for indentation
than the tab character.
The best practice is to use 4 whitespaces for first
indentation and then keep adding additional 4
whitespaces to increase the indentation.
BENFITS
In most of the programming language,
indentation is used to properly structure the
code. In Python, it’s used for grouping,
making the code automatically beautiful.
Python indentation rules are very simple.
Most of the Python IDEs automatically indent
the code for you, so it’s very easy to write
the properly indented code.
DRAWBACKS
Since whitespaces are used for indentation,
if the code is large and indentation is
corrupted then it’s really tedious to fix it. It
happens mostly in copying the code from
online sources, Word document, or PDF files.
Most of the popular programming languages
use braces for indentation, so anybody
coming from other side of the development
world finds it hard at first to adjust to the
idea of using whitespaces for the
indentation.