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

Python Basic To Medium Level Book NO 1 Data Science 1683351431

Uploaded by

Steve McQueen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Python Basic To Medium Level Book NO 1 Data Science 1683351431

Uploaded by

Steve McQueen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Python Programming Masterclass

Prasad Karunanayaka
What is the Python?
Python is a high-level programming language that is widely used for various
purposes, including web development, data analysis, artificial intelligence,
machine learning, and scientific computing. It was first released in 1991 by
Guido van Rossum, and since then, it has become one of the most popular
programming languages in the world.

Python is known for its simple syntax, ease of use, and flexibility, which makes
it an ideal language for beginners as well as experienced developers. It has a
large standard library that provides many useful modules and functions for
common programming tasks, as well as a vast ecosystem of third-party
packages that can be easily installed and used.

Python is an interpreted language, which means that the code is executed


directly by the interpreter without the need for compilation. It is also cross-
platform, which means that it can run on different operating systems such as
Windows, Linux, and macOS.
difference between tuple and list
Mutability: Tuples are immutable, while lists are mutable. This means that once a
tuple is created, it cannot be modified, while elements in a list can be added, removed
or modified after it has been created.

Syntax: Tuples are created using parentheses () or without any parentheses, but with
commas , separating the elements. Lists are created using square brackets [].

Performance: Tuples are generally faster than lists in terms of creation and accessing
elements, especially when the size of the collection is small. However, for larger
collections or operations that require frequent modifications, lists may be more
efficient.

Usage: Tuples are commonly used for fixed collections of data, such as coordinates or
database records. Lists are often used when the order or number of elements in a
collection may change or when the elements need to be modified.
Here are some common functions and methods that can be used with tuples in
Python:

len(): Returns the number of elements in the tuple.

count(x): Returns the number of times the element x appears in the tuple.

index(x): Returns the index of the first occurrence of the element x in the tuple.

Note that since tuples are immutable, However, functions and methods like
append(), insert(), remove(), pop(), sort(), and reverse() cannot be used with
tuples because they modify the contents of the sequence, which is not allowed in
tuples.
list is a mutable ordered collection that allows duplicates, set is an unordered collection
of unique elements, and tuple is an immutable ordered collection that allows duplicates.

I can explain some of the properties of the set data structure in Python that make it
unique compared to other data structures:

Uniqueness: A set contains only unique elements. When you try to add a duplicate
element to a set, it is automatically ignored.

Unordered: Elements in a set are not stored in a particular order. You cannot access a
specific element in a set by its index, as you would in a list or tuple.

Mutable: Sets are mutable, which means you can add or remove elements from a set
after it has been created.

Operations: Sets support a variety of set-theoretic operations, such as union,


intersection, difference, and symmetric difference. These operations can be used to
compare or manipulate sets in various ways.

Hashable: Elements in a set must be hashable, which means they must have a unique
hash value that can be used to identify them. This requirement is important because
sets use hash tables to efficiently store and retrieve elements.
you cannot add or remove values from a tuple in Python.
1. set() - Creates a new empty set, or converts an iterable object into a set.
2. len(set) - Returns the number of elements in the set.
3. x in set - Tests whether an element is in the set.
4. x not in set - Tests whether an element is not in the set.
5. set.add(elem) - Adds a new element to the set.
6. set.clear() - Removes all elements from the set.
7. set.copy() - Creates a shallow copy of the set.
8. set.difference(set2) - Returns a new set containing only the elements that are in
the first set but not in the second set.
9. set.difference_update(set2) - Removes all elements of another set from this set.
10. set.discard(elem) - Removes an element from the set if it is present, otherwise
does nothing.
11. set.intersection(set1, set2, ...) - Returns a new set containing only the elements
that are common to all sets.
12. set.intersection_update(set2) - Updates the set with the intersection of itself
and another.
13. set.isdisjoint(set2) - Returns True if two sets have no elements in common.
14. set.issubset(set2) - Returns True if another set contains this set.
15. set.issuperset(set2) - Returns True if this set contains another set.
16. set.pop() - Removes and returns an arbitrary element from the set. Raises a
KeyError if the set is empty.
17. set.remove(elem) - Removes an element from the set. Raises a KeyError if the
element is not found.
18. set.symmetric_difference(set2) - Returns a new set containing only the
elements that are in either of the sets, but not in both.
19. set.symmetric_difference_update(set2) - Updates the set with the symmetric
difference of itself and another.
20. set.union(set1, set2, ...) - Returns a new set containing all elements from all
sets.
21. set.update(set2) - Updates the set with the union of itself and another set.
In summary, the main differences between sets and dictionaries are:

Sets are unordered collections of unique elements, while dictionaries are collections
of key-value pairs.
Sets do not allow duplicate elements, while dictionaries do not allow duplicate
keys.
Sets are commonly used for operations like set intersection, union, and difference,
while dictionaries are commonly used for looking up values associated with a
particular key.

nested list can change dict


In programming, conditionals and branching are used to make decisions and
execute different pieces of code based on certain conditions.

In summary, break and continue are used for controlling the flow of execution
within loops, while return is used for controlling the flow of execution within
functions

break is used to exit a loop (for loop or while loop) prematurely, before the
loop has completed all its iterations. When break is executed, the loop is
immediately terminated and control is passed to the next statement after the
loop. Here is an example

continue is used to skip over certain iterations of a loop based on a condition,


without exiting the loop prematurely. When continue is executed, the current
iteration of the loop is skipped, and control is passed to the next iteration of
the loop. Here is an example:
return is used to exit a function prematurely, before the function has
completed all its instructions. When return is executed, the function is
immediately terminated and control is passed back to the caller of the
function. return can also be used to return a value from a function to the
caller.
In Python, a function is a block of reusable code that performs a
specific task. Functions in Python are defined using the def
keyword, followed by the function name, any arguments that the
function takes (enclosed in parentheses), and a colon. The code to
be executed by the function is then indented below the function
definition.
Global variables: These variables are declared outside of any function or class
and can be accessed from anywhere in the code.

Local variables: These variables are declared inside a function and can only
be accessed within that function.

Nonlocal variables: These variables are used in nested functions and can be
accessed from the nested functions.
Instance variables: These variables are associated with an instance of a
class and can be accessed using the instance name. They are created when
the instance is created and are destroyed when the instance is destroyed.

You might also like