Chapter 1 Introduction
Python
Python is what is called an interpreted language.
Compiled languages examine your entire program at compile time, and are able to warn you about a whole class of errors prior to execution. In contrast, Python interprets your script line by line as it executes it. Python will stop executing the entire program when it encounters an error (unless the error is expected and handled by the programmer, a more advanced subject that we’ll cover later on in this course).
Jupyter Lab
to install on mac: pip3 install jupyterlab on terminal
to upgrade pip: pip3 install --upgrade pip on terminal
check python version
!python -V
## 3.10.3 (v3.10.3:a342a49189, Mar 16 2022, 09:34:18) [Clang 13.0.0 (clang-1300.0.29.30)]
[Tip:] sys is a built-in module that contains many system-specific parameters and functions, including the Python version in use. Before using it, we must explicitly import it.
In Python, the concepts of objects and data types are closely related. In fact, in Python, everything is an object. Understanding this relationship is key to working effectively with Python.
1.1 Basics
1.1.1 Data Types in Python
Type of an Object: The type of an object in Python represents the kind of data that the object can hold. For example, the types
int,float,str, etc., represent different categories of data.Type Checking: You can use the
type()function to determine the type of an object. For example:
- Dynamic Typing: Python is dynamically typed, which means you don’t have to explicitly declare the type of a variable. The type is determined at runtime.
1.1.2 Objects vs Data Types
Every value in Python is an object, and every object has a type. For example,
5is an object of typeint.Data types in Python are essentially classes, and objects are instances of those classes. When you create a variable, you are creating an instance of a specific data type (class).
You can think of data types as categories or blueprints for objects. They define the behavior and characteristics of objects.
1.1.3 Objects in Python
Everything is an Object: In Python, every value is an object, and every object has a type. This includes not only the fundamental data types (integers, floats, strings) but also more complex types like lists, dictionaries, and even functions.
Object Identity: Each object in Python has a unique identity, which can be obtained using the
id()function. This identity is guaranteed to be unique and constant for the lifetime of the object.Object Attributes and Methods: Objects in Python can have attributes (characteristics) and methods (functions associated with the object). For example, a string object has methods like
upper()and attributes likelength.
In Python, everything is an object. Objects are instances of classes, and Python is an object-oriented programming language. Here are some common types of objects in Python:
Numbers:
int: Integer type, e.g.,x = 5.float: Floating-point type, e.g.,y = 3.14.complex: Complex number type, e.g.,z = 2 + 3j.
Strings:
str: String type, e.g.,s = "Hello, World!".
Collections:
list: Ordered collection of items, e.g.,my_list = [1, 2, 3].tuple: Immutable ordered collection of items, e.g.,my_tuple = (1, 2, 3).set: UNORDERED, MUTABLE collection of unique items, e.g.,my_set = {1, 2, 3}.dict: Key-value pairs, e.g.,my_dict = {'a': 1, 'b': 2}.
List vs Tuple: - both ordered - tuple is IMMUTABLE
- Boolean:
bool: Boolean type, representingTrueorFalse.
- None:
None: A special object representing the absence of a value or a null value.
- Functions:
- Functions themselves are objects in Python. You can assign them to variables, pass them as arguments, and return them from other functions.
- Classes:
- User-defined classes create objects. Instances of a class are objects.
- Modules:
- Modules are also objects. When you import a module, you are working with an object.
- File Objects:
- When you open a file, the file itself is an object in Python.
- Exceptions:
- Exception instances are objects that represent exceptional conditions.
- Custom Objects:
- Objects created from user-defined classes.
In Python, you can use the type() function to determine the type of an object, and the isinstance() function to check if an object is an instance of a particular class. The dynamic typing nature of Python allows objects to change types during runtime.
There are many different types of objects in Python. Let’s start with the most common object types: strings, integers and floats. Anytime you write words (text) in Python, you’re using character strings (strings for short). The most common numbers, on the other hand, are integers (e.g. -1, 0, 100) and floats, which represent real numbers (e.g. 3.14, -42.0).