0% found this document useful (0 votes)
1 views27 pages

3 Python Data Representation

Presenting Data in Python

Uploaded by

sshamshawa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views27 pages

3 Python Data Representation

Presenting Data in Python

Uploaded by

sshamshawa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ADVANCED PROGRAMMING TECHNIQUES

PYTHON LANGUAGE
[Link] TIZIANA D’ALESSANDRO (PHD), UNIVERSITÀ DEGLI STUDI DI CASSINO E DEL LAZIO MERIDIONALE
[Link]@[Link]
CONTENTS

 Python Data Types


 Python Built in and functions
 Examples and use cases

note from the next slide:


if both variables have the same value,
meaning they are equal and have the same memory register,

then it is dangerous, bc changing one may change the other.


if(x is y) ….

2
PYTHON DATA TYPES

3
PYTHON – DATA TYPES

Type Type name

Text Type: str


 In Python, types are dynamic (you
don't declare them) and objects Numeric Types int, float, complex
(they have built-in methods). Sequence Types list, tuple, range

Mapping Type dict


 In Python, every value has a type,
Set Types set, frozenset
but you don't need to state it
explicitly. The interpreter infers the Boolean Type bool
type at runtime.
Binary Types bytes, bytearray, memoryview

None Type NoneType


PYTHON – NUMERIC TYPES

Unlike C++, Python integers have


arbitrary precision (they can be as
large as your memory allows).
 Integer (int): Whole numbers.
 Floating Point (float): Decimal
numbers (64-bit precision, like
double in C++).
PYTHON – STRINGS
Strings in Python are immutable
sequences of Unicode
characters.
 You can use single ' or double
" quotes. You can use quotes
inside a string, as long as they
don't match the quotes
surrounding the string.
 Key Features:
o Slicing: Extracting parts of a
string easily.
o f-Strings: The modern way to
format output.
PYTHON – LISTS

A list is a mutable, ordered collection of items. It is the closest thing to a C++ vector, but it can hold
different data types.
PYTHON – STRINGS

 Strings in Python are arrays of


unicode characters.
 Python does not have a
character data type, a single
character is simply a string
with a length of 1.
 Square brackets can be used
to access elements of the
string.
PYTHON – TUPLES

Tuples are like lists, but they are immutable (cannot be changed after creation).
 They are used for data that should stay constant.
 They are faster than lists and safer for fixed configurations.
PYTHON – DICTIONARIES

Dictionaries are unordered


collections of Key: Value pairs.
They are extremely fast for
looking up data.
 Analogy: A real dictionary
where the "Key" is the word
and the "Value" is the
definition.
PYTHON – SETS

A set is an unordered collection of unique elements. It automatically removes duplicates.


PYTHON – BOOLEAN

 In programming you often need to know if an expression is True or False.


 You can evaluate any expression or compare two values
 Functions can return a bool value
PYTHON – CASTING

Sometimes you need to force a type change, especially when dealing with user input (which is always a
string).
PYTHON – BUILT IN & FUNCTIONS

14
NUMERIC & GENERAL MATH FUNCTIONS
file name: [Link] [Link]

def f(x) import functions

return functions.f(x)
These functions work with int, float, and complex numbers to perform common engineering calculations.
 abs(x): Returns the absolute value (or magnitude for complex numbers).
 round(x, n): Rounds a number to n decimal places.
 pow(base, exp): Returns base^{exp} (equivalent to base ** exp).
 divmod(a, b): Returns a tuple containing the quotient and the remainder $(a // b, a % b)$.
 sum(iterable): Adds all items in a list or tuple.
 import random; print([Link](1, 10)): picks a random number between one range
SEQUENCE FUNCTIONS

These are essential for handling sensor logs, data arrays, and text processing.
 len(s): Returns the number of items (length) of the sequence.
 max(s) / min(s): Returns the largest or smallest item in the sequence.
 sorted(iterable): Returns a new sorted list from the items in the sequence.
 enumerate(iterable): Returns an enumerate object. It yields pairs of (index, item), which is perfect for
loops where you need the counter.
 zip(*iterables): "Zips" two or more sequences together. Useful for pairing timestamps with sensor
readings.
MAPPING AND SET FUNCTIONS

Functions used to manage collections of unique data or key-value pairs.


 [Link]() / [Link](): Returns a view of all keys or all values in a dictionary.
 [Link](): Returns a view of (key, value) pairs for easy iteration.
 [Link]() / [Link](): Performs mathematical set operations (can also be done with & and |).
TYPE CONVERSION AND INSPECTION FUNCTIONS

Crucial for "defensive programming"—checking what kind of data you are dealing with before processing
it.
 type(object): Returns the type of the object (e.g., <class 'int’>).
 isinstance(object, type): Returns True if the object is an instance of the specified type. Preferred over
type() for OOP.
 dir(object): Returns a list of all valid attributes and methods for that object (great for "inspecting" an
object in the terminal).
 help(object): Invokes the built-in help system for the object or function.
STRING - ESCAPE CHARACTERS
ESCAPE
Code
CHARACTERS
Result Description

\' Single Quote Allows a single quote inside a single-quoted string.

\\ Backslash Inserts an actual backslash character.

\n New Line Moves the cursor to the beginning of the next line.

\r Carriage Return Moves the cursor to the beginning of the current line.

\t Tab Inserts a horizontal tab (white space).

\b Backspace Moves the cursor back one space (can delete the previous char).

\f Form Feed Used primarily for printers to eject a page; rarely used in modern UI.

\ooo Octal value Represents a character using an octal number (e.g., \110 is 'H').

\xhh Hex value Represents a character using a hexadecimal number (e.g., \x48 is 'H').
STRING- RAW

The Problem:
 Standard strings interpret the backslash (\) as an escape character. This causes errors in file paths or
Regex:
"C:\Users\name" → \n becomes a New Line!

The Solution:
 Add a small r (prefix) before your opening quote to create a Raw String. This tells Python to treat every
backslash as a literal character.
EXAMPLES AND USE CASES

21
EXAMPLE AND USE CASES – STRING
EXAMPLE AND USE CASES - STRING

Python_String_Functions
EXAMPLE AND USE CASES - STRING
EXAMPLE AND USE CASES

two decimal points


CONCLUSIONS – Q&A

Questions?
THANK YOU
[Link]@[Link]

You might also like