Unit-1
Introduction to Python
Programming
Prepared By:-
Prof. Chaitanya Kale
Python Introduction
• Python is a general-purpose interpreted, interactive, object-
oriented, and high-level programming language. It was created by
Guido van Rossum during 1985- 1990.
• Like Perl, Python source code is also available under the GNU
General Public License (GPL).
Why to Learn Python?
• Python is Interpreted − Python is processed at runtime by the interpreter.
You do not need to compile your program before executing it. This is
similar to PERL and PHP.
• Python is Interactive − You can actually sit at a Python prompt and
interact with the interpreter directly to write your programs.
• Python is Object-Oriented − Python supports Object-Oriented style or
technique of programming that encapsulates code within objects.
• Python is a Beginner's Language − Python is a great language for the
beginner-level programmers and supports the development of a wide range
of applications from simple text processing to WWW browsers to games.
Characteristics of Python
• Following are important characteristics of Python Programming −
• It supports functional and structured programming methods as well as
OOP.
• It can be used as a scripting language or can be compiled to byte-code for
building large applications.
• It provides very high-level dynamic data types and supports dynamic type
checking.
• It supports automatic garbage collection.
• It can be easily integrated with C, C++, COM, ActiveX, CORBA, and
Java.
Applications of Python
• Easy-to-learn − Python has few keywords, simple structure, and a clearly
defined syntax. This allows the student to pick up the language quickly.
• Easy-to-read − Python code is more clearly defined and visible to the eyes.
• Easy-to-maintain − Python's source code is fairly easy-to-maintain.
• A broad standard library − Python's bulk of the library is very portable
and cross-platform compatible on UNIX, Windows, and Macintosh.
• Interactive Mode − Python has support for an interactive mode which
allows interactive testing and debugging of snippets of code.
Applications of Python
• Portable − Python can run on a wide variety of hardware platforms
and has the same interface on all platforms.
• Extendable − You can add low-level modules to the Python
interpreter. These modules enable programmers to add to or
customize their tools to be more efficient.
• Databases − Python provides interfaces to all major commercial
databases.
• GUI Programming − Python supports GUI applications that can
be created and ported to many system calls, libraries and windows
systems, such as Windows MFC, Macintosh, and the X Window
system of Unix.
• Scalable − Python provides a better structure and support for large
programs than shell scripting.
Installation of Python
• Open Following URL to download python IDLE
https://www.python.org/downloads/
How to Run python Program
• Three ways to run python program
– Python Command line
– Windows Command prompt
– Microsoft IDLE
How to Create and Save python code-
• Open any text editor for e.g.- Notepad
• Write python code
• Go to file menu and click on save option.
• Select location to save file.
• Enter file name with .py extension.
• Click on save button.
Python Syntax
• Python syntax can be executed by writing directly in the Command
Line:
• Using the .py file extension, and running it in the Command Line:
Python Identifiers
• 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).
• Python does not allow punctuation characters such as @, $, and % within
identifiers. Python is a case sensitive programming language.
Thus, Manpower and manpower are two different identifiers in Python.
• Here are naming conventions for Python identifiers −
– Class names start with an uppercase letter. All other identifiers start with
a lowercase letter. Eg- Sanjivani__
– 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.
Reserved Words
Python Comments
• Comments can be used to explain Python code.
• Comments can be used to make the code more readable.
• Comments can be used to prevent execution when testing code.
• Comments starts with a #, and Python will ignore them:
• Example
– #This is a comment- following syntax use to display output
print("Hello, World!")
Multi Line Comments
• Python does not really have a syntax for multi line comments.
• To add a multiline comment you could insert a # for each line.
• But Python will ignore string literals that are not assigned to a
variable, you can add a multiline string (triple quotes) in your code,
and place your comment inside it:
Python Indentation
• Indentation refers to the spaces at the beginning of a code line.
• Where in other programming languages the indentation in code is
for readability only, the indentation in Python is very important.
• Python uses indentation to indicate a block of code.
• Python will give you an error if you skip the indentation:
Python Variables
• Variables are containers for storing data values.
• In Python, variables are created when you assign a value to it:
Rules for Variable Names
• Variable Names
• A variable can have a short name (like x and y) or a more descriptive
name (age, carname, total_volume).
• Rules for Python variables:
– A variable name must start with a letter or the underscore
character
– A variable name cannot start with a number
– A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
– Variable names are case-sensitive (age, Age and AGE are three
different variables)
Casting
• Get the Type
• You can get the data type of a variable with
the type() function.
• Output
Python Variables - Assign Multiple Values
• Many Values to Multiple Variables
• Python allows you to assign values to multiple variables in one
line:
• One Value to Multiple Variables
Unpack a Collection
• If you have a collection of values in a list, tuple etc. Python allows you
extract the values into variables. This is called unpacking.
Python - Global Variables
• Variables that are created outside of a function (as in all of the
examples above) are known as global variables.
• Global variables can be used by everyone, both inside of functions
and outside.
Python - Global Variables
• The global Keyword
• Normally, when you create a variable inside a function, that
variable is local, and can only be used inside that function.
• To create a global variable inside a function, you can use
the global keyword.
Python Data Types
• Built-in Data Types
• n programming, data type is an important concept.
• Variables can store data of different types, and different types can
do different things.
• Python has the following data types built-in by default, in these
categories:
Setting the Data Type
Setting the Data Type
Setting the Specific Data Type
Specify a Variable Type
• There may be times when you want to specify a type on to a variable. This
can be done with casting.
• Python is an object-orientated language, and as such it uses classes to define
data types, including its primitive types.
• Casting in python is therefore done using constructor functions:
– int() - constructs an integer number from an integer literal, a float literal
(by removing all decimals), or a string literal (providing the string
represents a whole number)
– float() - constructs a float number from an integer literal, a float literal or
a string literal (providing the string represents a float or an integer)
– str() - constructs a string from a wide variety of data types, including
strings, integer literals and float literals
Specify a Variable Type
References
• 1 Python Programming John M Zelle Franklin Beedle-2018.
• 2 Python Machine Learning: Machine Learning and Deep Learning
with Python Sebastian Raschka Vahid Mirjalili scikit-learn, and
TensorFlow”, Packt- 20173