0% found this document useful (0 votes)
2 views9 pages

Python Unit 1

The document provides an overview of Python programming, covering its applications in web development, data science, automation, and game development. It discusses Python's features, installation process, basic syntax, data types, operators, and the significance of indentation in code structure. Additionally, it highlights the differences between Python versions and the importance of type conversion.

Uploaded by

Kanchan Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Python Unit 1

The document provides an overview of Python programming, covering its applications in web development, data science, automation, and game development. It discusses Python's features, installation process, basic syntax, data types, operators, and the significance of indentation in code structure. Additionally, it highlights the differences between Python versions and the importance of type conversion.

Uploaded by

Kanchan Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Python Programming BCA IV Semester (NEP)

Unit – I Applications of Python


Introduction to Python Programming  Web Development: Python is widely used for
building dynamic web applications. Frameworks like
 Python is a high-level, interpreted programming Django and Flask provide developers with powerful
language known for its simplicity and readability. tools for rapid development, scalability, and security.

 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 %.

 No identifiers can begin with a number, and no


symbols other than the underscore.

 Python is a case sensitive programming language.

2. Python Keywords

 Keywords in python are the reserved words.

 These words cannot be used as a variable name,


On clicking the Install Now, The installation function name, or any other identifier name.
process starts.
3. Verify Python is installed on Windows  The following table lists some keywords of python.

To ensure if Python is successfully installed


on your system. Follow the given steps −
 Open the command prompt.
 Type ‘python’ and press enter.
 The version of the python which you have
installed will be displayed if the python is
successfully installed on your windows.

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.

 in (checks if value is present in sequence)


 not in (checks if value is not present in
sequence).

7. Identity Operators:

o Identity operators compare the memory addresses of


4. Logical Operators: two objects.

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.

o Operators with higher precedence are evaluated before


operators with lower precedence.

o For example, multiplication (*) has higher precedence


than addition (+), so 3 + 5 * 2 is evaluated as 3 + (5 *
2), resulting in 13.

o Parentheses can be used to override precedence and


explicitly specify the order of evaluation.
1. Numeric Types:
Association:
o int: Integers represent whole numbers without any
o Operator association determines the direction in which decimal point. Example: x = 10.
operators of the same precedence are evaluated.
o float: Floats represent real numbers with a decimal
o It's relevant when operators of equal precedence are point. Example: y = 3.14.
present in an expression.
o complex: Complex numbers consist of a real part
o Most operators in Python have left-to-right association, and an imaginary part represented by j. Example: z
meaning they are evaluated from left to right. = 3 + 2j.
o However, exponentiation (**) has right-to-left 2. Boolean Type:
association, meaning it's evaluated from right to left.
o bool: Booleans represent the truth values True and
o For example, 2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2), False. Example: is_python_fun = True.
resulting in 512.
3. Dictionary:

o Unordered collections of key-value pairs enclosed in


curly braces {}.

o Keys must be unique and immutable (often strings


or numbers), while values can be of any data type

o Example: D= {"name": "Alice", "age": 30}.

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):

o Python is dynamically typed, meaning variables


don't have a fixed data type assigned at declaration.

o The type is associated with the value assigned to the


variable.
2. Importing Specific Functions or Classes:
o When performing operations or assigning values
that involve different data types, Python sometimes You can import specific functions or classes from a
automatically converts the data types to ensure library using the from ... import syntax.
compatibility. This implicit conversion happens
behind the scenes. Example:

2. Explicit Type Conversion (Manual):

o In some cases, you might want to explicitly control


the data type conversion.

o Python provides built-in functions to perform this 3. Giving Aliases to Libraries:


type casting.
For long library names, you can assign shorter
o Common Type Conversion Functions: aliases using the as keyword.
 int(x): Converts x to an integer. Example
 float(x): Converts x to a floating-point number.

 str(x): Converts x to a string.

 bool(x): Converts x to a boolean (True or


False).

Python Libraries Python Control Flow


o Python libraries are collections of pre-written code Control flow in Python refers to the order in which
modules that provide functionalities for various statements are executed in a program. Python provides
tasks. several control flow constructs to enable conditional
execution, looping, and branching
o They extend the capabilities of the Python language
and allow you to perform complex operations 1. Conditional Statements:
without writing everything from scratch.
Used to make decisions based on certain conditions.
Importing Libraries
i. if statement:
1. Importing the Entire Library:
Executes a block of code if a specified condition is
Use the import statement followed by the library True.
name to import all functionalities from the library.

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:

Provides an alternative block of code to execute if i. break:


the condition in the if statement is False. Used within loops to prematurely exit the loop
when a certain condition is met.

iii. elif statement: ii. continue:


Used within loops to skip the current iteration and
Allows you to check multiple conditions proceed to the next one.
sequentially.

range () and exit () functions in python


2. Looping Statements:
1. range() Function:
Used to repeat a block of code multiple times. o The range() function generates a sequence of numbers
within a specified range.
i. for loop:
o It is commonly used with loops to iterate over a
Iterates over a sequence of items (like a list or sequence of numbers.
string) and executes a block of code for each item.
o The range() function can take one, two, or three
arguments: range(stop), range(start, stop), or
range(start, stop, step).

o By default, range() starts from 0 and increments by 1.

ii. while loop:

Continues executing a block of code as long as a


specified condition remains True.

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.

o It can be used to exit the current script or Python


session immediately.

o The exit() function is usually called with an optional


status code, which can indicate success (0) or failure
(non-zero).

o exit() is typically used in command-line scripts or


interactive sessions to halt execution.

o It is part of the sys module and can also be accessed


as sys.exit().

Key Differences:

o range() is used for generating sequences of numbers for


iteration, while exit() is used for terminating the
program execution.

o range() doesn't return a list directly, but exit()


terminates the program completely.

Prof. Raviraj K
9|Page
KLE`s BK BCA College, Chikodi.

You might also like