Python Unit 1
Python Unit 1
Created by Guido van Rossum and first released in Data Science and Machine Learning: Python has
1991. become the de facto language for data science and
machine learning due to its extensive libraries such as
Python has become the most popular programming NumPy, pandas, etc. These libraries facilitate data
languages particularly for tasks like web development, manipulation, analysis, visualization, and
data analysis, artificial intelligence, scientific implementation of machine learning algorithms.
computing, and automation.
Automation and Scripting: Python's simplicity and
Features of pythons readability make it ideal for automating repetitive tasks
and writing scripts. It is commonly used for tasks such
Readability and Simplicity: Python's syntax are as system administration, file manipulation, and batch
easier for programmers to understand and write code, processing.
reducing errors and improving maintainability.
Game Development: Python is increasingly being
Ease of Use: Compared to other languages, Python used in game development, both for indie games and
requires less code. This frees programmers from writing large-scale productions. Libraries like Pygame provide
repetitive code and allows them to focus on the core developers with tools for building 2D games, while
logic of their program. engines like Unity and Unreal Engine offer support for
scripting in Python.
Interpreted Language: An interpreter executes the
code line by line, making development faster and Desktop GUI Applications: Python can be used to
debugging easier. create desktop graphical user interface (GUI)
applications using libraries like Tkinter, PyQt, and
Cross-Platform Compatibility: Python code can wxPython. These libraries allow developers to build
run on various operating systems like Windows, cross-platform GUI applications with ease.
macOS, and Linux without modification.
Educational Purposes: Python's easy-to-understand
Dynamic Typing: Python variables don't have a syntax and extensive documentation make it an
predefined data type. The type is associated with the excellent choice for teaching programming to
value assigned to the variable at runtime. beginners. Many educational institutions use Python as
an introductory language for computer science courses.
Object-Oriented Programming (OOP): Python
supports object-oriented programming concepts like Python Versions
classes, objects, inheritance, and polymorphism.
Python has undergone several major releases since
Extensible: Python allows for easy integration with its inception. Some of the notable versions include:
other languages like C and C++.
Python 1.x: The initial versions of Python, including
1.0 and 1.6, introduced fundamental features of the
Strong Standard Library: Python comes with a language such as the core syntax and basic data
comprehensive standard library that provides modules
structures. These versions are no longer in active use.
and functions for a wide range of tasks, such as file I/O,
networking, and data manipulation.
Python 2.x: Python 2 was a major release series that
introduced significant improvements and new features.
The most widely used versions in this series were
Python 2.7, which became the stable version for many
Prof. Raviraj K
1|Page
KLE`s BK BCA College, Chikodi.
Python Programming BCA IV Semester (NEP)
years. However, Python 2 reached its end-of-life on 4. Verify Pip was installed
January 1, 2020, and is no longer maintained or
supported. To verify if pip was installed, follow the given
steps −
Python 3.x: Python 3 is the current and actively Open the command prompt.
developed version of the language. Notable versions in Enter pip –V to check if pip was installed.
the Python 3 series include Python 3.0, Python 3.6, The following output appears if pip is installed
Python 3.7, Python 3.8, Python 3.9, and Python 3.10
successfully.
(the latest stable release as of my last update). Python 3
is the recommended version for all new development.
Basics of Pythons
Installation of Python 1. Python Identifiers
1. Download Python Executable Installer An identifier in Python is a name used for
On the web browser, in the official site of identifying a variable, class, function, object,
python (www.python.org), move to the Download for module.
Windows section.
An identifier in python programming starts with a
2. Run Executable Installer letter A to Z / a to z / an underscore followed by
zero or more letters, underscores, and digits.
Run the installer. Make sure to select both the
checkboxes at the bottom and then click Install New.
Python language does not allow punctuation
characters within an identifier name for example:
@, $, and %.
2. Python Keywords
Prof. Raviraj K
2|Page
KLE`s BK BCA College, Chikodi.
Python Programming BCA IV Semester (NEP)
3. Statements and Expressions count = count + 1
o A variable is created the moment you first assign a
Statements: value to it.
o Variables do not need to be declared with any
o Statements are complete instructions that tell
particular type and can even change type after they
the Python interpreter to perform an action.
have been set.
They don't necessarily return a value.
x=4 # x is of type int
o Statements can be used for various tasks like:
x= "Tally" #x is now of type str
Assigning values to variables (x = 5)
o String variables in Python can be declared either by
Printing output to the console
using single or double quotes.
(print("Hello, world!"))
Creating control flow using if, for,
and while statements
Defining functions (def my_function():) Operators in Python
Importing modules (import math) Operators in Python are symbols that perform
operations on variables and values.
Expressions:
Python supports various types of operators, including
o Expressions are combinations of values,
arithmetic, comparison, assignment, logical, bitwise,
variables, operators, and function calls that membership, and identity operators.
evaluate to a single result.
o This result can be a number, a string, a Boolean 1. Arithmetic Operators
value, or even another data structure.
o Expressions are often used within statements, o Arithmetic operators perform basic mathematical
operations.
but they don't perform actions on their own.
4. Variables in Python
o Variables are containers for storing a value.
o Python is dynamically typed, it means that there is
no requirement of pre-declaration of a variable or
its type.
o The type (and value) are initialized on the
assignment. Assignments are performed using an
equal sign (=).
o Following example depicts how to declare variables
in python:
>>> count = 0
name = 'Ram'
Prof. Raviraj K
3|Page
KLE`s BK BCA College, Chikodi.
Python Programming BCA IV Semester (NEP)
2. Comparison Operators: 5. Bitwise Operators:
o Comparison operators compare two values and o Bitwise operators perform operations on binary
return a Boolean result (True or False). representations of integers.
3. Assignment Operators:
6. Membership Operators:
o Assignment operators are used to assign values to
o Membership operators test for membership in
variables.
sequences like lists, tuples, and strings.
7. Identity Operators:
o Logical operators perform logical operations on is (checks if two variables refer to the same
Boolean values. object),
is not (checks if two variables refer to different
objects).
Prof. Raviraj K
4|Page
KLE`s BK BCA College, Chikodi.
Python Programming BCA IV Semester (NEP)
Precedence and Association Datatypes in Python
o In Python, operator precedence determines the order in o In Python, data types represent the type or category of
which operators are evaluated in an expression. data that a variable or object can hold.
o While operator association defines the direction in o Python is a dynamically-typed language, meaning that
which operators of the same precedence are evaluated. variables do not have fixed types. Instead, the type of a
variable is determined at runtime based on the value
Precedence: assigned to it.
o Operator precedence determines the order in which o Some of the primary data types in Python are:
operators are evaluated in an expression.
Prof. Raviraj K
5|Page
KLE`s BK BCA College, Chikodi.
Python Programming BCA IV Semester (NEP)
4. Set: New Block, New Indentation: To create a new
block of code (like within an if statement or loop),
o Unordered collections of unique elements enclosed you must indent the statements further to the right.
in curly braces {}.
Consistent Indentation: It's crucial to maintain
o Elements must be immutable (often strings or consistent indentation throughout your code.
numbers). Mixing spaces and tabs or using inconsistent
indentation levels will lead to errors.
o Sets don't allow duplicate elements (e.g., {1,
"apple", 3.14}). Built-in Functions- Console Input and
Console Output
5. Sequence Types:
o In Python, you can use built-in functions to perform
o string: Strings represent sequences of characters console input and output operations.
enclosed in single (') or double (") quotes.
o These functions allow you to interact with the user
Example: message = 'Hello, World!'. through the console (also known as standard input and
standard output).
o list: Lists represent ordered collections of items that can
be of different data types. o Here are the main built-in functions for console input
and output:
Example: my_list = [1, 'apple', True].
1. Console Output:
o tuple: Tuples are similar to lists but are immutable
(cannot be modified after creation). o print(): The print() function is used to display
output to the console.
Example: my_tuple = (1, 2, 3).
o It can take one or more arguments, which can be of
6. None Type: different data types.
o None: Represents the absence of a value or a null value. o The function converts the arguments to strings and
Example: x = None. concatenates them, separating them with a space by
default.
Indentation in Python
2. Console Input:
o Indentation is a unique and essential aspect of Python's
syntax. o input(): The input() function is used to receive
input from the user via the console.
o Unlike many other programming languages that use
curly braces { } to define code blocks, Python relies on o It displays a prompt to the user (if provided) and
indentation to structure your code. waits for the user to enter a value followed by
pressing the Enter key.
o Here's a breakdown of how indentation works in
Python: o The function returns the input value as a string.
Indentation Levels: Python uses consistent Type Conversions in python
whitespace (usually 4 spaces) to define the nesting
of code blocks. o Type conversion, also known as type casting, in
Python refers to the process of transforming a value
Indentation Scope: Statements with the same from one data type to another.
indentation level belong to the same block of code
and are executed together. o There are two main approaches to type conversion
in Python:
Prof. Raviraj K
6|Page
KLE`s BK BCA College, Chikodi.
Python Programming BCA IV Semester (NEP)
1. Implicit Type Conversion (Automatic):
Example:
Prof. Raviraj K
7|Page
KLE`s BK BCA College, Chikodi.
Python Programming BCA IV Semester (NEP)
ii. else statement: 3. break and continue Statements:
Prof. Raviraj K
8|Page
KLE`s BK BCA College, Chikodi.
Python Programming BCA IV Semester (NEP)
2. exit() Function:
o The exit() function is used to terminate the execution
of the Python interpreter.
Key Differences:
Prof. Raviraj K
9|Page
KLE`s BK BCA College, Chikodi.