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

Week 03 Variables and Data Types

This document provides an overview of variables and data types in Python for week 3 of a Python course. It discusses Python's dynamic typing, the different data types like integers, floats, strings, lists and tuples. It also covers type casting, memory organization and garbage collection in Python. The next week will cover control flow statements, conditions and loops.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Week 03 Variables and Data Types

This document provides an overview of variables and data types in Python for week 3 of a Python course. It discusses Python's dynamic typing, the different data types like integers, floats, strings, lists and tuples. It also covers type casting, memory organization and garbage collection in Python. The next week will cover control flow statements, conditions and loops.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Python for Beginners

ෙනාහැක්කක් ෙනාමැත

Week -03 : Variables & Data Types

www.heladevs.org
Where we are ??

What is Programming
Intro Control flow Functions
Computing Languages

Variables &
Data Types

week-01 week-14
Previous This Week Integer 23
Week Python type system
Programming languages and the Complex 1+3j
importance of programming ● Data types number
○ Explain what is a data type and Numeric
● Binary world types available in python Float 32.15
● Dynamic typing
● Programming Language , Grammar ○ Briefly discuss what is dynamic Dictionary {1:q’, 2:’k’}
● Compiled vs Interpreted languages typing and how
○ Show the difference of each type ● Casting
and how the instructions to the
Python Boolean True, False
○ Type casting in python
computer are carried out. Also Data
discuss pros & cons of each type. Types {2,3,6,4’}
Set
● Brief history
○ Discuss the history of String “heladevs”
programming languages and how
they evolved Sequence
● Evolution of programming languages Lists [2,’er’9,2]
Type
○ Explain how programming
languages evolved over time
● Levels of abstractions
Tuple (3,7,’pen’)

○ Discuss different level of


abstractions in programming
languages
Mutable / Immutable
New sticky note for keeping the status Modify the list for keeping the status

VS
int - Integers
● Integers are whole numbers without any fractional or decimal parts

● Integers are immutable in Python, Means once created it can’t be changed

● Supports basic arithmetic operations ( + , -, / , * )


float - Floating point numbers
● Floating point numbers contain a fractional part, separated by decimal point

● Integers are immutable in Python, Means once created it can’t be changed

● Supports basic arithmetic operations ( + , -, / , * )

● Python uses IEEE 754 for floats, Python has a precision of approximately 15 to 17 decimal digits

● Can represent numbers ranging from approximately 2.23e-308 to 1.79e+308.


complex - Complex numbers
● Complex numbers contain real and imaginary parts

● Complex numbers are immutable

● Written as a + bj (a : real part, b : imaginary part)

● Supports basic arithmetic operations ( + , -, / , * ), and logarithms


Boolean Data ?

● A switch (on / off )


bool - Boolean
● Represents values that are either true or false. Uses True, False keywords in python

● Commonly used in conditional statements such as if statements , loops

● Supports boolean operators such as and, not, or

● Boolean values can be created using ==, !=, >, <, >=, and <= operators
str - Strings
● String is a sequence of characters enclosed within single quotes (' '), double quotes (" "), or

triple quotes (''' ''')

● Strings are immutable!

● Built in methods to manipulate strings, upper(), lower(), strip(), replace(),

split()

Photo credit https://brain-mentors.com/how-to-index-and-slice-strings-in-python/


List Type
● List is a collection of, ordered, changeable elements.

● Can have duplicates

● Lists can hold any type of data (ie. int, str, float.. even other lists! )

● List elements can be accessed using indexing starts at 0

● Lists are mutable, So they can be changed using built-in methods

● Few built in methods, append(), remove(), insert()

Photo credit https://railsware.com/blog/python-for-machine-learning-indexing-and-slicing-for-lists-tuples-strings-and-other-sequential-types/


List Type Contd..
Integer 23

Complex 1+3j
number
Numeric
Float 32.15

Dictionary {1:q’, 2:’k’}

Python Boolean True, False


Data
Types
{2,3,6,4’}
Set
String “heladevs”

Sequence Type Lists [2,’er’9,2]

Tuple (3,7,’pen’)
Type Conversion
float() Returns a floating-point object constructed
from a number or string
int() Returns an integer object constructed from
a number or string
str() Returns string version of an object
bool() Converts an argument to a Boolean value
chr() Returns string representation of character
given by integer argument
Memory Organization
Memory
● Heap - Dynamic memory
● Stack - Functions, local variables
● Static / Global - Global variables
● Code - Instructions
Python Names
Python Names
Understanding Python Variables
Understanding Python Variables
Understanding Python Variables

Eligible for garbage collection


Garbage collector ??
● Automatically reclaim memory that is no longer in use by the
program.
● Garbage collector works by periodically running in the
background and identifying objects that are no longer being
referenced by the program.
● Once these objects are identified, the garbage collector frees
up the memory they were using so that it can be used by
other parts of the program.
● Based on a reference counting algorithm, where every object
in memory has a reference count that tracks how many
references point to it.
● When an object's reference count drops to zero, it means that Reference Counting
no part of the program is using the object, and the garbage
collector can safely deallocate its memory.
● Uses a cycle detection algorithm to identify and collect cyclic
references, where two or more objects reference each other
in a circular chain. These circular references can cause
memory leaks if they are not properly handled, but Python's
garbage collector can detect and break these cycles to free
up the memory they were using.
● Garbage collector helps to automate memory management in
Python programs and reduce the likelihood of memory leaks
and other memory-related errors.

Generational GC
https://en.wikiversity.org/wiki/Introduction_to_Programming/About_Programming
So What’s Dynamic Typing?
● Dynamic typing is a feature of Python that allows a variable to be
bound to objects of different types during the execution of a
program.
So What’s Dynamic Typing? Cont..
So What’s Dynamic Typing? Cont..
Next Week – Week 03

Week 14

Week 04

• Control flow
○ statements
Week 03
○ conditions
• Data types
○ loops
• Dynamic typing
• Casting
To think
THANK YOU
ෙනාහැක්කක් ෙනාමැත.

You might also like