python tutorial, examples, programs, language, beginners, introduction, strings, lists, dictionary, tuples, date, time, files, functions, modules, methods, exceptions
Python Tutorial | Python Programming Language
Python is a popular programming language known for its unique features and advantages over languages like Java and C++. This tutorial covers Python basics and advanced topics, including installation, conditional statements, loops, data structures, Object-Oriented Programming, Generators, Exception Handling, Python RegEx, and more. It’s designed for both beginners and professionals.
History of Python
In the late 1980s, Guido van Rossum dreamed of developing Python. The first version, Python 0.9.0, was released in 1991. Since then, Python’s popularity has skyrocketed. Reports show that Python is now the most popular programming language among developers due to its high demand in the tech industry.
What is Python?
Python is a general-purpose, high-level programming language. It is dynamically typed, meaning the data types of variables are determined at runtime. Python is both compiled and interpreted, making it versatile and efficient. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
Key Features of Python
- Easy to Use and Read: Python’s syntax is clear and straightforward, making it ideal for both beginners and experienced programmers. This simplicity can speed up development and reduce errors.
- Dynamically Typed: You don’t need to specify the data type of a variable when writing code; Python determines it at runtime.
- High-Level: Python code is human-readable, which makes it easier to understand and write.
- Compiled and Interpreted: Python code is first compiled into bytecode, then interpreted line by line. The default implementation of Python, CPython, is both compiled and interpreted.
- Garbage Collected: Python automatically manages memory allocation and de-allocation, so programmers don’t have to.
- Purely Object-Oriented: Everything in Python is an object, including numbers and strings.
- Cross-Platform Compatibility: Python can be installed on Windows, macOS, and various Linux distributions, allowing developers to create software that runs on different operating systems.
- Rich Standard Library: Python includes many standard libraries with ready-to-use modules and functions for tasks like web development, data manipulation, machine learning, and networking.
- Open Source: Python is free to use and distribute, which has led to its adoption across many sectors and disciplines.
Python’s Ecosystem
Python has a wealth of web-based resources, open-source projects, and an active community. This makes learning the language, collaborating on projects, and contributing to the Python ecosystem easy for developers.
Why Learn Python?
Python’s simple syntax and readability make it an excellent language for beginners. It helps experienced programmers write clean and error-free code. Python also has numerous third-party libraries that extend its functionality in areas such as web development, scientific computing, and data analysis.
Popularity and Demand
According to the TIOBE Index, Python consistently ranks as one of the top programming languages. Its versatility and ease of use contribute to its widespread adoption in various industries, including finance, healthcare, and education.
Conclusion
Python’s simplicity, versatility, and strong community support make it an ideal choice for both new and experienced programmers. Whether you’re interested in web development, data science, or automation, Python has the tools and libraries to help you succeed.
Java vs. Python
Python is a great choice for rapid development and scripting tasks, while Java emphasizes a strong type system and object-oriented programming. Let’s explore some basic programs to illustrate the key differences between them.
Printing “Hello World”
Python Code:
pythonCopy codeprint("Hello World")
In Python, it’s just one line of code with a simple syntax to print “Hello World.”
Java Code:
javaCopy codepublic class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
In Java, you need to declare classes and method structures, among other things.
While both programs produce the same output, the syntax difference is clear. Python’s syntax is straightforward and easy to learn, while Java’s syntax requires more code and structure to perform the same task.
Key Differences Between Python and Java
- Ease of Learning and Writing Code:
- Python: Known for its simplicity and readability, making it easier to learn and write code.
- Java: Requires more boilerplate code and structure, which can make it more complex for beginners.
- Typing System:
- Python: Dynamically typed, meaning you don’t need to declare the variable type.
- Java: Statically typed, meaning you need to declare the variable type.
Suitable Domains
- Python:
- Data Science: Extensive libraries such as Pandas, NumPy, and SciPy.
- Machine Learning: Popular frameworks like TensorFlow and scikit-learn.
- Web Development: Frameworks like Django and Flask.
- Scripting and Automation: Easy syntax for writing scripts to automate tasks.
- Java:
- Web Development: Frameworks like Spring and Hibernate.
- Mobile App Development: Primarily used for Android app development.
- Enterprise Applications: Robust features for building large-scale enterprise applications.
Conclusion
Both Python and Java have their strengths and are suitable for different types of projects. Python’s simplicity and versatility make it an excellent choice for rapid development, data science, and scripting. Java’s strong type system and extensive frameworks make it ideal for large-scale enterprise applications and Android development. Your choice between Python and Java will depend on the specific needs of your project and your familiarity with each language.
Python Basic Syntax
Python is known for its clean and readable syntax. Unlike many other programming languages, Python doesn’t use curly braces {}
or semicolons ;
. Instead, it relies on indentation to define code blocks. This makes Python code look more like English and easier to read.
Example of Indentation:
pythonCopy codedef func():
statement_1
statement_2
# More statements
statement_N
In this example, all indented statements are part of the func
function. Typically, four spaces are used for indentation.
End of Statements:
Python uses a newline character to end statements instead of a semicolon.
Case Sensitivity:
Python is case-sensitive, meaning that name
and Name
are considered two different variables.
Comments:
Comments in Python start with the #
symbol. Any text following this symbol on the same line is ignored by the interpreter. Comments are useful for adding notes or temporarily disabling code.
pythonCopy code# This is a comment
print("Hello, World!") # This will print Hello, World!
Reserved Keywords:
Certain keywords in Python, such as if
, else
, for
, while
, try
, except
, and finally
, cannot be used as variable names because they have special meanings in the language.
History of Python
Python was created by Guido van Rossum in the late 1980s at the Centrum Wiskunde & Informatica (CWI) in the Netherlands. He aimed to develop a language that was easy to read and efficient, as a successor to the ABC programming language.
- 1991: The first public version, Python 0.9.0, was released.
- 1994: Python 1.0 was released, introducing many features still present today.
- 2000: Python 2.0 was released, bringing list comprehensions, garbage collection, and Unicode support.
- 2008: Python 3.0 was released, introducing several backward-incompatible changes for improved readability and maintainability.
- 2010s: Python’s popularity soared, especially in data science, machine learning, and web development.
The Python Software Foundation (PSF) was established in 2001 to support and promote the Python programming language and its community.
Why Learn Python?
Python offers numerous benefits, making it one of the most popular programming languages today:
- Easy to Use and Learn: Simple and clear syntax makes it accessible for beginners.
- Expressive Language: Allows expressing complex concepts in fewer lines of code.
- Interpreted Language: No need for compilation, enabling rapid development and testing.
- Object-Oriented: Supports reusable and modular code.
- Open Source: Free to use, distribute, and modify.
- Extensible: Can be extended with modules written in C, C++, or other languages.
- Standard Library: Rich library for tasks like string manipulation, web programming, and more.
- GUI Programming: Frameworks like Tkinter and PyQt facilitate desktop application development.
- Integrated: Easily integrates with other languages and technologies.
- Embeddable: Can be embedded in other applications as a scripting language.
- Dynamic Memory Allocation: Automatically manages memory.
- Libraries and Frameworks: Extensive libraries and frameworks for various tasks.
- Versatility: Used in web development, machine learning, data analysis, scientific computing, and more.
- Large Community: Vast community offering support and resources.
- Career Opportunities: High demand in fields like data science, AI, and web development.
Where is Python Used?
Python’s versatility makes it applicable in various fields:
- Data Science: Libraries like NumPy, Pandas, and Matplotlib.
- Desktop Applications: Libraries like PyQt and Tkinter.
- Console Applications: Useful for command-line tools.
- Mobile Applications: Frameworks like Kivy and BeeWare.
- Software Development: Suitable for small to large-scale projects.
- Artificial Intelligence: Libraries like TensorFlow, Keras, and PyTorch.
- Web Applications: Frameworks like Django and Flask.
- Enterprise Applications: Suitable for large-scale applications with distributed computing.
- 3D CAD Applications: Libraries like Blender.
- Machine Learning: Widely used due to its simplicity and powerful libraries.
- Computer Vision: Libraries like OpenCV.
- Speech Recognition: Libraries like SpeechRecognition and PyAudio.
- Scientific Computing: Libraries like NumPy, SciPy, and Pandas.
- Education: Ideal for teaching programming to beginners.
- Testing: Frameworks like unittest and pytest for automated testing.
- Gaming: Libraries like Pygame.
- IoT: Used in devices like Raspberry Pi and Arduino.
- Networking: Scripts and applications for network automation and management.
- DevOps: Automation and scripting for infrastructure management.
- Finance: Libraries for financial modeling and analysis.
- Audio and Music: Libraries like Pyaudio and Music21.
- Writing Scripts: Utility scripts for tasks like web scraping and data processing.
Python Popular Frameworks and Libraries
Python boasts a vast array of libraries and frameworks that are extensively used across various domains such as machine learning, artificial intelligence, and web development. Here are some of the most popular ones:
Web Development (Server-side)
- Django: A high-level web framework that encourages rapid development and clean, pragmatic design.
- Flask: A micro web framework that is simple to use and easy to scale.
- Pyramid: A flexible framework that allows developers to start small and scale up.
- CherryPy: An object-oriented web framework that allows for rapid development of web applications.
GUI-based Applications
- Tkinter: The standard Python interface to the Tk GUI toolkit.
- PyGTK: A set of Python wrappers for the GTK+ graphical user interface library.
- PyQt: A set of Python bindings for The Qt Company’s Qt application framework.
- PyJs: Allows you to write your GUI applications using JavaScript and HTML while leveraging Python for the backend.
Machine Learning
- TensorFlow: An end-to-end open-source platform for machine learning developed by Google.
- PyTorch: An open-source machine learning library developed by Facebook’s AI Research lab.
- Scikit-learn: A simple and efficient tool for data mining and data analysis, built on NumPy, SciPy, and matplotlib.
- Matplotlib: A plotting library for the Python programming language and its numerical mathematics extension NumPy.
- SciPy: A Python library used for scientific and technical computing.
Mathematics and Data Analysis
- NumPy: A library for the Python programming language, adding support for large, multi-dimensional arrays and matrices.
- Pandas: A library providing high-performance, easy-to-use data structures, and data analysis tools.
Web Scraping and Parsing
- BeautifulSoup: A library for parsing HTML and XML documents.
- Requests: A simple and elegant HTTP library for Python.
Database Interaction
- SQLAlchemy: A SQL toolkit and Object-Relational Mapping (ORM) library for Python.
Mobile and Game Development
- Kivy: An open-source Python library for developing multitouch applications.
- Pygame: A set of Python modules designed for writing video games.
Testing
- Pytest: A framework that makes it easy to write simple and scalable test cases.
- Django REST framework: A powerful and flexible toolkit for building Web APIs.
Fast Web Frameworks
- FastAPI: A modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.
Data Science and Machine Learning Apps
- Streamlit: A library for building interactive web applications for machine learning and data science.
Natural Language Processing
- NLTK: A leading platform for building Python programs to work with human language data.
Python print()
Function
The print()
function in Python is used to display output to the console or terminal. It can print text, variables, and other data types in a human-readable format.
Syntax:
pythonCopy codeprint(object(s), sep=separator, end=end, file=file, flush=flush)
Parameters:
- object(s): One or more objects to be printed, separated by commas.
- sep: A string inserted between objects, default is a space.
- end: A string appended after the last object, default is a newline.
- file: An object with a
write
method, default issys.stdout
. - flush: Whether to forcibly flush the stream, default is
False
.
Examples:
pythonCopy code# Displaying a string
print("Hello, World!")
# Displaying multiple values
name = "Aman"
age = 21
print("Name:", name, "Age:", age)
# Printing variables and literals
x = 5
y = 7
print("x =", x, "y =", y, "Sum =", x + y)
# Printing with formatting
percentage = 85.75
print("Score: {:.2f}%".format(percentage))
Output:
makefileCopy codeHello, World!
Name: Aman Age: 21
x = 5 y = 7 Sum = 12
Score: 85.75%
In these examples, the print
function is used to display strings, integers, floats, and formatted output. It’s a versatile tool for debugging, logging, and providing information to users.
Python Conditional Statements
Conditional statements in Python help us run specific blocks of code based on certain conditions. Python provides keywords like if
, else
, and elif
to set up these conditions.
Example of if..else Statement
pythonCopy codex = 10
y = 5
if x > y:
print("x is greater than y")
else:
print("y is greater than or equal to x")
Output:
csharpCopy codex is greater than y
In this example, x
and y
are variables with values 10 and 5 respectively. The if..else
statement checks if x
is greater than y
. If x
is greater, it prints “x is greater than y”. If not, it prints “y is greater than or equal to x”.
Python Loops
Loops in Python help us repeat specific blocks of code multiple times. There are two main types of loops: for
and while
.
Python For Loop
pythonCopy codefruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x, end=" ")
Output:
Copy codeapple banana cherry
In this example, the for
loop iterates over a list of fruits ("apple"
, "banana"
, "cherry"
) and prints each fruit on the same line.
Python While Loop
pythonCopy codei = 1
while i < 5:
print(i, end=" ")
i += 1
Output:
Copy code1 2 3 4
Here, the while
loop repeats a block of code as long as a condition (i < 5
) is true. It starts with i
set to 1 and prints i
until i
becomes 5 or more.
These loops are fundamental in Python for iterating over sequences (like lists) or executing code repeatedly based on a condition.
Python Data Structures
Python includes four main data structures: lists, tuples, sets, and dictionaries. These structures help organize and manage data effectively.
- Lists
- Lists are collections of items that can be changed.
- Items in a list are ordered and accessed using indices.
- Example:pythonCopy code
fruits = ['apple', 'banana', 'cherry'] print("fruits[1] =", fruits[1]) fruits.append('orange') print("fruits =", fruits) num_list = [1, 2, 3, 4, 5] sum_nums = sum(num_list) print("sum_nums =", sum_nums)
- Output:cssCopy code
fruits[1] = banana fruits = ['apple', 'banana', 'cherry', 'orange'] sum_nums = 15
- Tuples
- Tuples are similar to lists but immutable (cannot be changed once created).
- Elements in tuples are ordered and accessed using indices.
- Example:pythonCopy code
point = (3, 4) x, y = point print("(x, y) =", x, y) tuple_ = ('apple', 'banana', 'cherry', 'orange') print("Tuple =", tuple_)
- Output:pythonCopy code
(x, y) = 3 4 Tuple = ('apple', 'banana', 'cherry', 'orange')
- Sets
- Sets are collections of unique items without a specific order.
- Sets can be changed (mutable) and are defined with curly braces
{}
. - Example:pythonCopy code
set1 = {1, 2, 2, 1, 3, 4} print("set1 =", set1) set2 = {'apple', 'banana', 'cherry', 'apple', 'orange'} print("set2 =", set2)
- Output:arduinoCopy code
set1 = {1, 2, 3, 4} set2 = {'apple', 'cherry', 'orange', 'banana'}
- Dictionaries
- Dictionaries store data as key-value pairs.
- They are mutable and accessed using keys.
- Example:pythonCopy code
person = {'name': 'Umesh', 'age': 25, 'city': 'Noida'} print("person =", person) print(person['name']) person['age'] = 27 print("person =", person)
- Output:javaCopy code
person = {'name': 'Umesh', 'age': 25, 'city': 'Noida'} Umesh person = {'name': 'Umesh', 'age': 27, 'city': 'Noida'}
These data structures in Python offer different ways to store and manipulate data, each suited for specific tasks. They are fundamental for building and organizing information in Python programs.
Requests: Offers a simple user interface for web APIs and performs HTTP requests
Python File I/O Files are used to store data on a computer disk. Python provides built-in tools for handling files. You can open a file with Python and perform operations like reading, writing, and appending data. There are different ways to open files depending on what you want to do. Let’s explore these operations with examples. We’ll also learn how to work with binary files, which store data in a format that isn’t directly readable by humans.
Python Exceptions An exception is a problem that stops a program from running normally. It’s an error in the program that interrupts its flow. When an exception happens, the program halts and doesn’t continue with the rest of the code. Exceptions in Python are represented as objects that describe what went wrong, like the type of error and a message.
To handle exceptions in Python, we use try and except blocks. The try block contains code that might cause an exception. If an exception occurs, the except block handles it and allows the program to continue without crashing. For instance:
pythonCopy codetry:
x = int(input("Enter a number: "))
y = 10 / x
print("Result:", y)
except ZeroDivisionError:
print("Error: Division by zero")
except ValueError:
print("Error: Invalid input")
Python CSV CSV stands for “comma separated values,” which is a simple file format for storing tabular data like spreadsheets. It’s plain text and widely used for data interchange. You can read a CSV file using Python’s csv.reader function, which returns a reader object that iterates over rows of the CSV file. Each row is returned as a list of values, where each value corresponds to a column in the CSV file.
pythonCopy codeimport csv
with open('data.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
To write data to a CSV file, you can use csv.writer function. It returns a writer object that you can use to write rows to the CSV file:
pythonCopy codeimport csv
data = [
['Name', 'Age', 'Country'],
['Alice', '25', 'USA'],
['Bob', '30', 'Canada'],
['Charlie', '35', 'Australia']
]
with open('data.csv', 'w') as file:
writer = csv.writer(file)
for row in data:
writer.writerow(row)
Python Magic Methods Magic methods in Python are special methods that start and end with double underscores, like __init__
or __str__
. These methods are automatically invoked by Python in certain situations. They allow you to customize how objects behave in operations like comparing objects, converting them to strings, or accessing them as containers.
For example, the __str__
method defines how an object should be represented as a string:
pythonCopy codeclass Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"{self.name} ({self.age})"
person = Person('Vikas', 22)
print(person)
Python Oops Concepts In Python, everything is treated as an object, including integers, floats, functions, and classes. Object-oriented programming (OOP) is supported in Python, and it revolves around classes and objects. Here are some key OOP concepts in Python:
- Classes and Objects: Classes are blueprints for objects. An object is a collection of data (attributes) and methods (functions) that act on the data.
- Inheritance: It allows one class to inherit the properties and methods of another class.
- Constructor: The
__init__
method initializes an object’s attributes when it’s created. - Polymorphism: It allows objects of different classes to be treated as objects of a common superclass, enabling different classes to be used interchangeably through a common interface.
- Encapsulation: It involves wrapping data (attributes) and methods into a single unit. Python uses access modifiers like public, private, and protected to indicate the level of access to attributes and methods.
Python Advance Topics Python offers advanced features that help programmers tackle complex tasks. Some of these include:
- Iterators: Objects that allow iteration over a collection of data, processing each element individually without loading the entire collection into memory.
- Generators: Functions that produce a sequence of values using the
yield
statement. They generate values lazily, one at a time, preserving their local state between calls. - Decorators: Functions that modify the behavior of other functions without changing their code directly. They are defined with the
@
symbol followed by the decorator function’s name. They’re often used for tasks like logging, timing, or caching.
These concepts enhance Python’s capabilities, making it versatile for various programming tasks.
Python MySQL
Python MySQL is a powerful relational database system. To use MySQL with Python, you need to set up the environment and establish a connection. Here are the steps to work with MySQL in Python:
Environment Setup: Install and configure MySQL Connector/Python to integrate MySQL with Python.Database Connection: Connect Python to MySQL using MySQL Connector/Python.Creating New Database: Use SQL commands in Python to create a new database in MySQL.Creating Tables: Define tables within your MySQL database using Python and SQL commands.Insert Operation: Insert data into MySQL tables from Python using SQL commands.Read Operation: Retrieve data from MySQL tables using Python and SQL commands.Update Operation: Modify existing data in MySQL tables using Python and SQL commands.Join Operation: Combine data from two or more MySQL tables using Python and SQL commands.Performing Transactions: Execute a set of SQL queries as a single unit of work in MySQL using Python.Error Handling: Manage errors that may occur during MySQL operations in Python.Indexes: Improve query performance by creating indexes on fields within your MySQL tables.Stored Procedures and Functions: Utilize stored procedures and functions in MySQL with Python for complex operations.
Environment Setup: Install and configure MongoDB and PyMongo library for Python.Database Connection: Connect Python to MongoDB using PyMongo’s MongoClient class.Creating a new database: Use MongoClient to create a new database in MongoDB.Creating collections: Define collections within your MongoDB database to store documents.Inserting documents: Add new documents to a MongoDB collection using insert_one() or insert_many() methods.Querying documents: Retrieve documents from a MongoDB collection using find_one(), find(), and other query methods.Updating documents: Modify existing documents in a MongoDB collection using update_one() or update_many() methods.Deleting documents: Remove documents from a MongoDB collection using delete_one() or delete_many() methods.Aggregation: Perform aggregation operations like grouping and counting using MongoDB’s aggregation pipeline framework.Indexing: Enhance query performance by creating indexes on fields in MongoDB collections.
Environment Setup: Python comes with a built-in module, SQLite3, to connect to SQLite databases.Connecting to SQLite: Use SQLite3 to run SQL queries, insert, select, update, delete data, and retrieve data from tables.Transactions: Handle transactions in SQLite to ensure data integrity and rollback changes if needed.
Dynamic Content: Use Python CGI scripts to create HTML web pages, handle form inputs, and interact with databases.Web Applications: Develop scalable and secure web applications using Python CGI for dynamic content generation.
Asyncio: Use Python’s asyncio library for asynchronous programming, offering coroutines to handle multiple tasks concurrently.Event Loops: Manage coroutines with asyncio’s event loops for non-blocking execution of asynchronous tasks.
Multithreading: Use Python’s threading module for concurrent execution of multiple threads within a single process, suitable for I/O-bound tasks.Multiprocessing: Employ Python’s multiprocessing module for running multiple processes concurrently across multiple CPU cores, ideal for CPU-intensive operations.
Requests: Use Python’s requests package to fetch HTML content from websites.Parsing: Employ libraries like Beautiful Soup and lxml to parse HTML and extract desired data.Ethics: Adhere to website terms of service and robots.txt guidelines when scraping data.
NLTK: Utilize NLTK for tokenization, stemming, lemmatization, and other NLP tasks.spaCy: Employ spaCy for fast and efficient processing of large text datasets, including pre-trained models for various NLP tasks.