0% found this document useful (0 votes)
59 views20 pages

Introduction To Python Programming - Notes

Uploaded by

vihavgowda2105
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
59 views20 pages

Introduction To Python Programming - Notes

Uploaded by

vihavgowda2105
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 20

Introduction to Python Programming:

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

3. High Level Language:


Low level language requires extensive knowledge of the hardware since it is machine
dependent. To overcome this limitation, high level language has been evolved which
uses normal English, which is easy to understand to solve any problem.
High level languages are computer independent and programming becomes quite easy
and simple.
Various high-level languages are given below:
• BASIC (Beginners All Purpose Symbolic Instruction Code): It is widely used,
easy to learn general purpose language. Mainly used in microcomputers in
earlier days.
• COBOL (Common Business Oriented language): A standardized language used
for commercial applications.
• FORTRAN (Formula Translation): Developed for solving mathematical and
scientific problems. One of the most popular languages among scientific
community.
• C: Structured Programming Language used for all purpose such as scientific
application, commercial application, developing games etc.
• C++: Popular object-oriented programming language, used for general purpose.
• Python: it is a procedure oriented as well as object-oriented programming
language. Mainly used in gaming application development, image processing
etc.
PROGRAMMING LANGUAGE TRANSLATORS:

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.

Flowing are the various tools to achieve this purpose:

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.

Interpreter: it also executes instructions written in a high-level language. interpreter takes 1


statement, translates it, executes it & then again takes the next statement.

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:

• Python is an interpreted, object-oriented, high-level programming language with


dynamic semantics.
• Its high-level built in data structures, combined with dynamic typing and dynamic
binding, make it very attractive for Rapid Application Development, as well as for use
as a scripting or glue language to connect existing components together.
• Python's simple, easy to learn syntax emphasizes readability and therefore reduces the
cost of program maintenance.
• Python supports modules and packages, which encourages program modularity and
code reuse. The Python interpreter and the extensive standard library are available in
source or binary form without charge for all major platforms, and can be freely
distributed.
Features of 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.

Different IDE available for python programming:

Instructions to Install Anaconda and Run Jupyter Notebook

• Download & Install Anaconda Distribution


• Create Anaconda Environment
• Install and Run Jupyter Notebook
1. Download & Install Anaconda Distribution
Follow the below step-by-step instructions to install Anaconda distribution.

1.1 Download Anaconda Distribution


Go to https://anaconda.com/ and select Anaconda Individual Edition to download the latest
version of Anaconda. This downloads the .exe file to the windows download folder.
1.2 Install Anaconda
By double-clicking the .exe file starts the Anaconda installation. Follow the below screen shot’s
and complete the installation
This finishes the installation of Anaconda distribution, now let’s see how to create an
environment and install Jupyter Notebook.
2. Create Anaconda Environment from Navigator
2.1 Open Anaconda Navigator
Open Anaconda Navigator from windows start or by searching it. Anaconda Navigator is a UI
application where you can control the Anaconda packages, environment etc.
2.2 Create an Environment to Run Jupyter Notebook
This is optional but recommended to create an environment before you proceed. This gives
complete segregation of different package installs for different projects you would be working
on. If you already have an environment, you can use it too.
3. Install and Run Jupyter Notebook
Once you create the anaconda environment, go back to the Home page on Anaconda Navigator
and install Jupyter Notebook from an application on the right panel.
It will take a few seconds to install Jupyter to your environment, once the install completes,
you can open Jupyter from the same screen or by accessing Anaconda Navigator ->
Environments -> your environment (mine pandas-tutorial) -> select Open With Jupyter
Notebook

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

Elements of Python Programming:


• Identifiers
• Keywords
• Variables
• Datatypes
• Operators
Variables in Python:
• Python variables are the reserved memory locations used to store values with in a
Python Program. This means that when you create a variable you reserve some space
in the memory.
• Based on the data type of a variable, Python interpreter allocates memory and decides
what can be stored in the reserved memory.
• Therefore, by assigning different data types to Python variables, you can store integers,
decimals or characters in these variables.
• A Python variable is created automatically when you assign a value to it. The equal sign
(=) is used to assign values to variables.
• The operand to the left of the = operator is the name of the variable and the operand to
the right of the = operator is the value stored in the variable.
counter = 100 # Creates an integer variable
miles = 1000.0 # Creates a floating-point variable
name = "Python" # Creates a string variable
Here, 100, 1000.0 and "Zara Ali" are the values assigned to counter, miles, and name
variables, respectively

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

Python Numeric Data Type:


Python numeric data types store numeric values. Number objects are created when you assign
a value to them.
For example −
var1 = 1
var2 = 10
var3 = 10.023
Python supports four different numerical types −
• int (signed integers)
• float (floating point real values)
• complex (complex numbers)

Python String Data Type:


Python Strings are identified as a contiguous set of characters represented in the quotation
marks. Python allows for either pairs of single or double quotes.
example −
str = 'Hello World!'
name= “Python Programming”
Python Boolean Data Types:
Python boolean type is one of built-in data types which represents one of the two values
either True or False. Python bool() function allows you to evaluate the value of any
expression and returns either True or False based on the expression.
Examples
Following is a program which prints the value of boolean variables a and b –
a = True

You might also like