LectureNote_UnitI
LectureNote_UnitI
1. Introduction:
Python is an easy to learn, powerful programming language. It has efficient high-level data structures
and a simple but effective approach to object-oriented programming. Python‟s elegant syntax and
dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid
application development in many areas on most platforms. The Python interpreter and the extensive
standard library are freely available in source or binary form for all major platforms from the Python
Web site, http://www.python.org/, and can be freely distributed. The same site also contains distributions
of and pointers to many free third party Python modules, programs and tools, and additional
documentation. The Python interpreter is easily extended with new functions and data types
implemented in C or C++ (or other languages callable from C). Python is also suitable as an extension
language for customizable applications.
Python is a high-level language. Programs written in a high-level language have to be translated into
something more suitable before they can run. The engine that translates and runs Python is called the
Python Interpreter: There are two ways to use it: immediate mode and script mode. In immediate mode,
we type Python expressions into the Python Interpreter window, and the interpreter immediately shows
the result. Alternatively, we can write a program in a file and use the interpreter to execute the contents
of the file. Such a file is called a script.
IT-LCBC/LN/MAR/JAN-JUL-2025/PYTHON Page 1
Lecture Note Python Programming Session: Jan-July, 2025
The engine that translates and runs Python is called the Python Interpreter: There are two ways to
use it: immediate mode and script mode.
In immediate mode, we type Python expressions into the Python Interpreter window, and the
interpreter immediately shows the result.
Alternatively, we can write a program in a file and use the interpreter to execute the contents of the
file. Such a file is called a script.
In this class it is advised to use the latest stable version of Python3 to avoid issues from the
official website only with an IDE like VSCode/Atom/PyCharm etc.
IT-LCBC/LN/MAR/JAN-JUL-2025/PYTHON Page 2
Lecture Note Python Programming Session: Jan-July, 2025
Windows
macOS
Linux
1. Open Terminal.
2. Use your package manager to install Python:
o Debian/Ubuntu: sudo apt update && sudo apt install python3
o Fedora/Red Hat: sudo yum install python3
3. Verify the installation:
o Type: python3 --version.
IT-LCBC/LN/MAR/JAN-JUL-2025/PYTHON Page 3
Lecture Note Python Programming Session: Jan-July, 2025
3. Python Shell:
Shell is a command processor. The Python Shell gives you a command line interface you can use to
specify commands directly to the Python interpreter in an interactive manner. The Python Shell is the
interpreter that executes python programs, other pieces of Python code, or simple commands. The user of a
Python shell types commands at the prompt (>>>), and presses the return key to send these commands
immediately to the interpreter for processing.
To start the Python shell, simply type python or py and hit Enter in the terminal(in Windows , in case of
*nix systems type python3):
The interactive shell is also called REPL which stands for read, evaluate, print, loop. It'll read each
command, evaluate and execute it, print the output for that command if any, and continue this same process
repeatedly until you quit the shell.
There are different ways to quit the shell:
1. you can hit Ctrl+Z on Windows or Ctrl+D on Unix systems to quit
2. use the exit() command
3. use the quit() command
IT-LCBC/LN/MAR/JAN-JUL-2025/PYTHON Page 4
Lecture Note Python Programming Session: Jan-July, 2025
IT-LCBC/LN/MAR/JAN-JUL-2025/PYTHON Page 5
Lecture Note Python Programming Session: Jan-July, 2025
4. Code Indentation:
Indentation in Python refers to the (spaces and tabs) that are used at the beginning of a statement. The
statements with the same indentation belong to the same group called a suite. All adjacent statements at the
same level of indentation are considered to be a block/suite. The block ends when the indentation returns to
the previous level.
Primary purpose of indentation in Python is to define the scope of statements, such as those within loops,
conditionals, functions, and classes. Consistent and proper indentation is crucial for the interpreter to
understand the logical structure of the code. Indentation is not just a matter of style or convention in Python.
Here are the rules of indentation in python:
Python‟s default indentation spaces are four spaces. The number of spaces, however, is
entirely up to the user. However, a minimum of one space is required to indent a statement.
Indentation is not permitted on the first line of Python code.
Python requires indentation to define statement blocks.
A block of code must have a consistent number of spaces.
To indent in Python, whitespaces are preferred over tabs. Also, use either whitespace or tabs
to indent; mixing tabs and whitespaces in indentation can result in incorrect indentation
errors.
IT-LCBC/LN/MAR/JAN-JUL-2025/PYTHON Page 6
Lecture Note Python Programming Session: Jan-July, 2025
A Python identifier is a name used to identify a variable, function, class, module or other object. An
identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters,
underscores and digits (0 to 9).
Naming conventions for Python identifiers −
Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
Starting an identifier with a single leading underscore indicates that the identifier is private.
Starting an identifier with two leading underscores indicates a strongly private identifier.
If the identifier also ends with two trailing underscores, the identifier is a language-defined
special name.
Keywords are reserved words that cannot be used as variable names, function names, or any other
identifiers. Python keywords are the fundamental building blocks of any Python program. As of Python 3.8,
there are thirty-five keywords in Python. Some of the keywords are : if, for, while, break, continue, def, and,
or, return etc.
'False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del',
'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal',
'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'
6. Literals:
Literals are a notation for representing a fixed value in source code.
Python has different types of literals.
String literals: A string literal can be created by writing a text(a group of Characters ) surrounded by
the single(”), double(“”), or triple quotes. By using triple quotes we can write multi-line strings or
display in the desired way.
Numeric literals: There are three types of numeric literals:
Integer Both positive and negative numbers including 0. There should not be
literal any fractional part.
Float These are real numbers having both integer and fractional parts.
literal
Complex The numerals will be in the form of a + bj, where „a„ is the real part
literal and „b„ is the complex part
Boolean literals: There are only two Boolean literals in Python. They are True and False
Literal Collections: Like list literal, tuple literal etc.
Special literals: Python contains one special literal (None). ‘None’ is used to define a null variable
IT-LCBC/LN/MAR/JAN-JUL-2025/PYTHON Page 7
Lecture Note Python Programming Session: Jan-July, 2025
7. Strings:
Python string is an ordered collection of characters used to store and represent text-based
information. Python strings are categorized as immutable sequences, meaning that the characters they
contain have a left-to-right positional order and that they cannot be changed in-place.built-in len()
function is used to find length of a string.
There are a number of useful operations that can be performed with string. Among these are the
following:
s.replace(old,
Replace all instances of old with new in string
new)
IT-LCBC/LN/MAR/JAN-JUL-2025/PYTHON Page 8
Lecture Note Python Programming Session: Jan-July, 2025
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Operator Name
+ Addition
- Subtraction
* Multiplication
/ Division
// Floor division
** Exponentiation
% modulus
Operator Name
== Equal
!= Not equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
IT-LCBC/LN/MAR/JAN-JUL-2025/PYTHON Page 9
Lecture Note Python Programming Session: Jan-July, 2025
is not Returns True if both variables are not the same object
IT-LCBC/LN/MAR/JAN-JUL-2025/PYTHON Page 10
Lecture Note Python Programming Session: Jan-July, 2025
Example:
name = input("Enter your name: ")
print("Hello, " + name + "!")
IT-LCBC/LN/MAR/JAN-JUL-2025/PYTHON Page 11
Lecture Note Python Programming Session: Jan-July, 2025
Sample Questions:
IT-LCBC/LN/MAR/JAN-JUL-2025/PYTHON Page 12