Python has many built-in data types including numbers, strings, lists, tuples, and dictionaries. It also supports user-defined data structures like classes. Data types store values without semantics while data structures organize data to allow efficient operations. Python uses dynamic typing so variables can reference values of any type. Some key differences between mutable and immutable objects are that mutable objects like lists can be modified after creation while immutable objects like strings and tuples cannot be changed once set.
5. Data Structures
A data structure is an abstract description of a way of organizing data
to allow certain operations on it to be performed efficiently.
6. Data Structure vs Data Type
A data type stores certain information without any semantics. A
data type can be an integer, a floating point, a string (basic data
types).
In a data structure information is, well, structured, and there are
certain ways you can access the information stored. For example in
a First In First Out (FIFO) queue you can add information to the
back and retrieve information from the front.
8. Guido van Rossum
Now, it's my belief that Python is a lot easier than to teach
to students programming and teach them C or C++ or Java
at the same time because all the details of the languages
are so much harder. Other scripting languages really don't
work very well there either.
9. Data Types in Python
Built-in Data types
Sticking to the hierarchy scheme used in the official Python documentation
these are numeric types, sequences, sets and mappings (and a few more
not discussed further here).
10. Number Types
int long float complex
10 51924361L 0.0 3.14j
100 -0x19323L 15.20 45.j
-786 0122L -21.9 9.322e-36j
080 0xDEFABCECBDAECBFBAEL 32.3+e18 .876j
-0490 535633629843L -90. -.6545+0J
11. Variable & Variable Assignment
A variable is a location in memory used to store some data
(value).
We use the assignment operator (=) to assign values to a
variable. Any type of value can be assigned to any valid
variable.
15. Functions
A function is a block of organized, reusable code that is used to
perform a single, related action.
As you already know, Python gives you many built-in functions
like print(), etc. but you can also create your own functions.
These functions are called user-defined functions.
16. Data Structures
They are structures which can
hold some data together. In other
words, they are used to store a
collection of related data.
17. Sequence Types
list, dict, tuples, sets, strings
In computing, these types implement storage of data elements. Being
templates, they can be used to store arbitrary elements, such as
integers or custom classes. One common property of all sequential
types is that the elements can be accessed sequentially.
18. Accessing Values in
Strings
var1 = 'Hello World!'
var2 = "Python Programming"
print(var1[0]) # H
print(var2[1:5]) # ytho
Updating Strings
var1 = 'Hello World!'
var2 = var1[:6] + 'Python'
print(var2) # Hello Python
String Formatting Operator
print("My name is %s and weight is %d kg!" % ('Tofik', 120))
19. List Manipulation
colors = ['red', 'blue', 'green']
print(colors[0]) # red
print(colors[2]) # green
print(len(colors)) # 3
b = colors # Does not copy the list
21. Control Flow #2 - For Loop Statement
Lists as an iterable:
list = ['physics', 'chemistry', 1997, 2000]
for x in list:
¡.print(x)
Strings as an iterable:
string = 'chemistry'
for x in string:
print(x)
22. Flat Sequences
Elements which has that subtype
are more compact, but they are
limited to holding primitive values
like characters, bytes and numbers.
Container Sequences
Hold references to the object they
contain, which may be of any type.
While flat sequences physically
store that value of each item within
its own memory space.
list, tuple, collections.deque ... str, bytes, array.array, ....
24. Simple put, a mutable object can be changed after it is
created, and an immutable object can¡¯t. Objects of built-in
types like (int, float, bool, str, tuple) are immutable. Objects of
built-in types like (list, set, dict) are mutable.
25. Immutable Objects
It¡¯s no big surprise that it fails since we just learnt immutable
objects cannot change their state.
27. Big bro of the list, Tuple.
A tuple is a sequence of immutable Python objects. Tuples are sequences, just
like lists. The differences between tuples and lists are, the tuples cannot be
changed unlike lists and tuples use parentheses, whereas lists use square
brackets.