Python Unit 1
Python Unit 1
PYTHON PROGRAMMING
Introduction to Python
What is Python?
-Python is a high-level, interpreted, general-
purpose programming language.
-It was created by Guido van Rossum and first
released in 1991.
Features of Python
Interpreted Language: Python runs code line by line. This
makes fixing errors faster and development quicker, as you
don't need a separate build step.
High-Level Language: It handles complex computer details
for you, letting you focus on solving your problem.
General-Purpose Language: You can use it for many
different things: building websites, analyzing data, making AI,
automating tasks, and creating games.
Dynamically Typed: You don't have to tell Python what type
of data a variable holds (like if it's a number or text); Python
figures it out as it runs.
Object-Oriented Programming (OOP) Support: It
supports OOP concepts like classes, objects, inheritance,
and polymorphism for reusable.
Open Source : Python is freely available and modifiable.
Extensive Standard Library: Comes with many ready-
made tools and functions for common tasks, so you don't
have to write everything from scratch.
Platform Independent (Portable): Your Python code can
run on different operating systems like Windows, Mac, or
Linux without needing major changes.
Python Keywords
What are Keywords?
•Keywords also known as reserved words .
•They have a predefined meaning and purpose, which
the Python interpreter understands.
•Keywords cannot be used as variable names, function
names, class names, or any other identifier. And that
lead to a SyntaxError.
•Python keywords are case-sensitive.
To get list of all keywords in your current Python version (e.g., Python 3.11 has 36
keywords).
Values & Operators:
Exception Handling:
Advanced / Other:
•with: Used for context management (e.g., handling files, database connections).
Ensures resources are properly acquired and released.
•as (with with): Used in conjunction with with to assign the result of the context
manager.
•del: Used to delete objects or names.
•global: Declares a variable inside a function to be a global variable.
•nonlocal: Declares a variable inside a nested function to refer to a variable in the
nearest enclosing (non-global) scope.
•async: Used to define an asynchronous function (coroutine). (Python 3.5+)
•await: Used to pause the execution of an async function until an awaitable
Identifiers
An identifier is a name given to entities like variables,
functions, classes, modules, or any other object. It's
essentially how you label and refer to different
components in your code.
Valid Rules:
•Can contain a-z, A-Z, 0-9, _.
•Must not start with a digit.
•Case-Sensitive.
Invalid Rules:
•Cannot begin with a number.
•Cannot use @, #, $, %, etc., except for the underscore (_).
•Cannot be a Python keyword (reserved word like if, else, for).
Variables
A variable is a named storage location for data.
The information can be numbers, text, lists, or
more complex data types.
The value can change (or "vary") during the
execution of a program.
Example:
Comments
Comments are lines of text within your code that the Python
interpreter completely ignores during program execution.
Why are Comments Helpful?
Identity Operators:
Bitwise Operators:
•Used to perform operations on individual bits of integers
•& (Bitwise AND)
•| (Bitwise OR)
•^ (Bitwise XOR)
•~ (Bitwise NOT)
•<< (Left shift)
•>> (Right shift)
Input and Output in Python
Input and Output (I/O) operations allow your program to interact with the
outside world.
•Input refers to the process of receiving data from an external source into your
program. This data can come from a user typing on a keyboard, reading from
a file, receiving data over a network, etc.
•Output refers to the process of sending data from your program to an
external destination. This data can be displayed on the screen, written to a file,
sent over a network, etc.
The most common built-in functions for basic input and output are input()
and print().
Input (input() function):
The input() function is used to get data (typically text) from the user via
the keyboard.
-Main Purpose is to pause program execution and wait for the user to
type something and press Enter.
-The input() function always returns the user's input as a string (str).