Python Tuts
Introduction
What is Python?
Python is a high-level, interpreted, and general-purpose programming language. Let’s break down what that means:
High-Level: It’s designed to be readable and easy for humans to write, using syntax that resembles English. It abstracts away (hides) the complex details of the computer’s hardware (like memory management), allowing you to focus on solving problems.
Interpreted: Unlike compiled languages (e.g., C++), Python code is executed line-by-line by an interpreter. This means you can run code immediately without a separate compilation step, making it great for prototyping and scripting.
General-Purpose: It can be used for almost anything! Whether it’s building websites, analyzing data, creating artificial intelligence models, automating tasks, or developing games, Python has the tools and libraries to do it.
Features & Advantages
Language Fundamentals
Identifiers
Keywords
Constants
Operators
Datatypes
Functions
Basic Programming
Conditional Programming
Iterative programming
Adv-Datatypes/Collections
Introduction
bytes
bytearray
list
What is list?
In Python, a list is a built-in collection object that stores an ordered collection of items. i.e, when we need to store more than one value as a single object then choose list.
Characteristics
- It is heterogeneous collection
- It is Ordered collection.
- It is mutable (can modify)
- It allows duplicates
- It supports index and slice.
- It is dynamic (growable)
Creating a list.
List can be create…
- Empty List
- List with elements.
- List with constructor
Methods
| Method | Description |
|---|---|
| append(x) | Adds an item x to the end of the list. |
| extend(iterable): | Adds all elements of an iterable (like another list) to the end of the list. |
| insert(index, x) | Inserts an element x at the specified index |
| remove(x) | Removes the first occurrence of the value x from the list. |
| pop([index]) | Removes and returns the element at the given index (or the last item if the index is omitted). |
| clear() | Removes all elements from the list, leaving it empty. |
| index(x): | Returns the index of the first occurrence of the value x. |
| count(x) | Returns the number of occurrences of the value x. |
| sort(key=None, reverse=False) | Sorts the list in ascending order (or descending if reverse=True). |
| reverse() | Reverses the elements of the list in place. |
| copy() | Returns a shallow copy of the list. |
tuple
Definition
A tuple in Python is an ordered collection of elements, which can store multiple items in a single variable. Tuples are similar to lists but have one key difference: tuples are immutable, meaning once a tuple is created, its elements cannot be changed.
Item #2
Item #3
set
frozenset
range
dict
Modules
What is module?
In Python, a module is a file containing Python code. The code within a module can consist of Python statements, functions, classes, or variables. Modules allow you to organize your Python code into separate files, making it easier to manage and reuse code across multiple projects.
There are two types of modules known as…
1. Standard Library Modules
2. User Defined Modules
Pre-defined Modules
Python provides many predefined (built-in or standard library) modules that offer a wide range of functionalities without requiring external installation. These modules are part of the Python Standard Library and are automatically available when you install Python.
Operating System Related Modules
- os – Provides functions to interact with the operating system (file, directory operations).
- sys – Access to system-specific parameters and functions (like command-line arguments).
- shutil – High-level file operations like copying and deleting.
- platform – Provides system platform information.
- math – Provides mathematical functions like
sqrt(),sin(),log(). - random – Functions for generating random numbers, shuffling, and random selections.
- statistics – Functions for statistical operations like
mean(),median(). - decimal – Fixed-point and floating-point arithmetic with more precision.
- fractions – Support for rational number arithmetic.
- datetime – Working with dates and times.
- time – Time-related functions like sleeping and getting the current time.
- calendar – Functions to handle and display calendars.
- json – Read and write JSON data.
- pickle – Serialize and deserialize Python objects.
- csv – Read and write CSV files.
- tkinter – GUI development (standard interface to Tk GUI toolkit).
- turtle – Drawing graphics using turtle graphics.
User-defined Modules
A user-defined module in Python is a file created by the programmer/developer that contains Python code—such as functions, classes, or variables—which can be reused in other programs. These modules are saved with a .py extension.
Add Your Heading Text Here
- Create a Module File
- Save the Module
- Import the Module in Another Program
- Call the module properties
Advantages of modules:
- Code Reusability – Write once, use in multiple programs.
- Modular Design – Organize code into logical blocks.
- Maintainability – Easier to update or fix a specific part of the code.
