Introduction To Python Programming - Notes
Introduction To Python Programming - Notes
Module-1:
Introduction: What Is a Program? Programming Languages, Software Development, History
of Python Programming Language, Features of Python, Execution of a Python Program,
Identifiers, Keywords, Statements and Expressions, Variables, Operators, Precedence and
Associativity, Data Types, Indentation, Comments, Reading Input, Print Output, Type
Conversions, the type () Function and Is Operator
Control Flow Statements: The if, if…else, if…elif…else, Nested if Statement, while Loop, the
for Loop, the continue and break Statements
INTRODUCTION TO PROGRAMMING:
• A language that is acceptable to a computer system is called a computer language or
programming language and the process of creating a sequence of instructions in such a
language is called programming or coding.
• A program is a set of instructions, written to perform a specific task by the computer.
• A set of large programs is called software.
To develop software, one must have knowledge of a programming language. COMPUTER
LANGUAGES are a means of communication. Normally people interact with each other
through a language. On the same pattern, communication with computers is carried out through
a language. This language is understood both by the user and the machine. Just as every
language like English, Hindi has its own grammatical rules; every computer language is also
bounded by rules known as syntax of that language. The user is bound by that syntax while
communicating with the computer system.
Computer languages are broadly classified as:
1. Low Level Language: The term low level highlights the fact that it is closer to a
language which the machine understands. The low-level languages are classified as: o
Machine Language: This is the language (in the form of 0’s and 1’s, called binary
numbers) understood directly by the computer. It is machine dependent. It is difficult
to learn and even more difficult to write programs. Programming for Problem Solving
Assembly Language: This is the language where the machine codes comprising of
0’sand 1’s are substituted by symbolic codes (called mnemonics) to improve their
understanding. It is the first step to improve programming structure.
Ex: 10001 110001 1000
100101 1110001 100
2. Assembly language programming:
It is simpler and less time consuming than machine level programming, it is easier to
locate and correct errors in assembly language than in machine language programs. It
is also machine dependent. Programmers must have knowledge of the machine on
which the program will run.
Ex: move ax,3
move bx,4
add ax,bx
As you know that high level language is machine independent and assembly language though
it is machine dependent yet mnemonics that are being used to represent instructions are not
directly understandable by the machine. Hence to make the machine understand the instructions
provided by both the languages, programming language instructors are used. They transform
the instruction prepared by programmers into a form which can be interpreted & executed by
the computer.
Compiler: The software that reads a program written in high level language and translates it
into an equivalent program in machine language is called as compiler. The program written by
the programmer in high level language is called source program and the program generated by
the compiler after translation is called as object program. The complier converts the entire
source code into machine level program.
Assembler: The software that reads a program written in assembly language and translates it
into an equivalent program in machine language is called as assembler.
Linker: A linker or link editor is a computer program that takes one or more object files
generated by a compiler and combines them into a single executable file, library file, or another
object file.
Software Development Lifecycle:
WHAT IS PYTHON:
Easy to Code
Python is a very high-level programming language, yet it is effortless to learn. Anyone
can learn to code in Python in just a few hours or a few days. Mastering Python and all
its advanced concepts, packages and modules might take some more time. However,
learning the basic Python syntax is very easy, as compared to other popular languages
like C, C++, and Java.
Easy to Read
Python code looks like simple English words. There is no use of semicolons or brackets,
and the indentations define the code block. You can tell what the code is supposed to
do simply by looking at it.
Free and Open-Source
Python is developed under an OSI-approved open source license. Hence, it is
completely free to use, even for commercial purposes. It doesn't cost anything to
download Python or to include it in your application. It can also be freely modified and
re-distributed. Python can be downloaded from the official Python website.
Robust Standard Library
Python has an extensive standard library available for anyone to use. This means that
programmers don’t have to write their code for every single thing unlike other
programming languages. There are libraries for image manipulation, databases, unit-
testing, expressions and a lot of other functionalities. In addition to the standard library,
there is also a growing collection of thousands of components, which are all available
in the Python Package Index.
Interpreted
When a programming language is interpreted, it means that the source code is executed
line by line, and not all at once. Programming languages such as C++ or Java are not
interpreted, and hence need to be compiled first to run them. There is no need to compile
Python because it is processed at runtime by the interpreter
Portable
Python is portable in the sense that the same code can be used on different machines.
Suppose you write a Python code on a Mac. If you want to run it on Windows or Linux
later, you don’t have to make any changes to it. As such, there is no need to write a
program multiple times for several platforms.
Object-Oriented and Procedure-Oriented
A programming language is object-oriented if it focuses design around data and objects,
rather than functions and logic. On the contrary, a programming language is procedure-
oriented if it focuses more on functions (code that can be reused). One of the critical
Python features is that it supports both object-oriented and procedure-oriented
programming.
Extensible
A programming language is said to be extensible if it can be extended to other
languages. Python code can also be written in other languages like C++, making it a
highly extensible language.
Support for GUI
One of the key aspects of any programming language is support for GUI or Graphical
User Interface. A user can easily interact with the software using a GUI. Python offers
various toolkits, such as Tkinter, wxPython and JPython, which allows for GUI's easy
and fast development.
Dynamically Typed
Many programming languages need to declare the type of the variable before runtime.
With Python, the type of the variable can be decided during runtime. This makes Python
a dynamically typed language.
For example, if you have to assign an integer value 20 to a variable “x”, you don’t need
to write int x = 20. You just have to write x = 15
High-level Language
Python is a high-level programming language because programmers don’t need to
remember the system architecture, nor do they have to manage the memory. This makes
it super programmer-friendly and is one of the key features of Python.
Simplify Complex Software Development
Python can be used to develop both desktop and web apps and complex scientific and
numerical applications. Python's data analysis features help you create custom big data
solutions without so much time and effort. You can also use the Python data
visualization libraries and APIs to present data in a more appealing way. Several
advanced software developers use Python to accomplish high-end AI and natural
language processing tasks.
Python file need to be saved with extension .py. Source code is translated to bytecode by the
interpreter then using PVM(python virtual machine) converts bytecode into machine level
code.
Now select New -> PythonX and enter the below lines and select Run. On Jupyter, each cell is
a statement, so you can run each cell independently when there are no dependencies on previous
cells.
This completes installing Anaconda and running Jupyter Notebook
Applications of Python:
Python is a general-purpose, popular programming language and it is used in almost every
technical field. The various areas of Python use are given below.
o Data Science
o Date Mining
o Desktop Applications
o Console-based Applications
o Mobile Applications
o Software Development
o Artificial Intelligence
o Web Applications
o Enterprise Applications
o 3D CAD Applications
o Machine Learning
o Computer Vision or Image Processing Applications.
o Speech Recognitions
Identifiers:
Identifiers are the name given to memory location.
n Identifier is used to identify the literals used in the program. The rules to name an identifier
are given below.
o The first character of the variable must be an alphabet or underscore ( _ ).
o All the characters except the first character may be an alphabet of lower-case(a-z),
upper-case (A-Z), underscore, or digit (0-9).
o Identifier name must not contain any white-space, or special character (!, @, #, %, ^,
&, *).
o Identifier name must not be similar to any keyword defined in the language.
o Identifier names are case sensitive; for example, my name, and MyName is not the
same.
o Examples of valid identifiers: a123, _n, n_9, etc.
o Examples of invalid identifiers: 1a, n%4, n 9, etc.
Datatypes in Python:
Python Data Types are used to define the type of a variable. It defines what type of data we are
going to store in a variable. The data stored in memory can be of many types.
For example, a person's age is stored as a numeric value and his or her address is stored as
alphanumeric characters.
Python has various built-in data types. They are:
• Numeric - int, float, complex
• String - str
• Boolean - bool
• None – None Type