Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

RAISONI GROUP OF INSTITUTIONS

Presentation
On
“Programming with Python”
By
Ms. H. C Kunwar
Lecturer

Department of Computer Engineering


G. H. Raisoni Polytechnic, Nagpur.
RAISONI GROUP OF INSTITUTIONS 1
Department of Computer Engineering

Unit 1
(08 Marks)

Introduction & Syntax of Python Program

Lecture-1

RAISONI GROUP OF INSTITUTIONS 2


What is Python Programming ?

 Python is a popular programming language.

 It was created by Guido van Rossum, and released in 1991.

 What can Python do?


• Python can be used on a server to create web applications.
• Python can be used alongside software to create workflows.
• Python can connect to database systems. It can also read and modify
files.
• Python can be used to handle big data and perform complex
mathematics.
• Python can be used for rapid prototyping, or for production-ready
software development.

 It is used for:
• web development (server-side),
• software development, mathematics, system scripting.
RAISONI GROUP OF INSTITUTIONS 3
Why Python?

 Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).

 Python has a simple syntax similar to the English language.

 Python has syntax that allows developers to write programs with fewer lines

than some other programming languages.

 Python runs on an interpreter system, meaning that code can be executed as

soon as it is written. This means that prototyping can be very quick.

 Python can be treated in a procedural way, an object-oriented way or a

functional way.

RAISONI GROUP OF INSTITUTIONS 4


1.1 Features of Python Programming
1. Easy to code:
Python is a high-level programming language. Python is very easy to learn the
language as compared to other languages like C, C#, JavaScript, Java, etc. It is very
easy to code in python language. It is also a developer-friendly language.

2. Free and Open Source:


Python language is freely available at the official website

3. Object-Oriented Language:
One of the key features of python is Object-Oriented programming. Python supports
object-oriented language and concepts of classes, objects encapsulation, etc.
4. GUI Programming Support:
Graphical User interfaces can be made using a module such as PyQt5, PyQt4, wxPython, or Tk
in python.

5. Interpreted Language:
Python is an Interpreted Language because Python code is executed line by line at a time. like
other languages C, C++, Java, etc. there is no need to compile python code this makes it easier
to debug our code. The source code of python is converted into an immediate form
called bytecode.
RAISONI GROUP OF INSTITUTIONS 5
Features of Python Programming

 6. Python is Portable language:


Python language is also a portable language. For example, if we have
python code for windows and if we want to run this code on other
platforms such as Linux, Unix, and Mac.

 7. Python is Integrated language:


Python is also an Integrated language because we can easily integrated python with
other languages like c, c++, etc.

 8. Extensible feature:
Python is a Extensible language. We can write us some Python code into C or C++
language and also we can compile that code in C/C++ language.

RAISONI GROUP OF INSTITUTIONS 6


1.2 Python Building Blocks?

Python Building Blocks

Identifiers Variables Keywords Indentation Comments

RAISONI GROUP OF INSTITUTIONS 7


1.2 Python Building Blocks?

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). ... All other identifiers start with a lowercase letter.
 User-defined identifiers can consist of letters, digits, underscores, and the
dollar-sign $
 Must start with a non-digit
 Identifiers are case sensitive (count and Count are different variables)
 Reserved words (keywords) cannot be used as identifiers
 An identifier can be any length

RAISONI GROUP OF INSTITUTIONS 8


1.2 Python Building Blocks?

 Keywords :
• Keywords are reserved words in Python and used to perform an internal
operation.
• All the keywords of Python contain lower-case letters only.

• Example:

and assert in and assert in and assert in


del else raise del else raise del else raise
from if from if from if
continue continue continue
not pass not pass not pass
finally finally finally
while yield is while yield is while yield is
as break as break as break
return return return
elif except def elif except def elif except def
global import for global import for global import for

RAISONI GROUP OF INSTITUTIONS 9


1.2 Python Building Blocks?
 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.

In the above code, the first and last line of the statement is related to the same suite because
there is no indentation in front of them.
Note : By default, Python uses four spaces for indentation, and the programmer can
manage it. RAISONI GROUP OF INSTITUTIONS 10
1.2 Python Building Blocks?

 Variables :

 A name that represents a value stored in the computer memory. Used to access
and manipulate data stored in memory.

 A variable references the value it represents.

 Use assignment statement to create a variable and make it reference data


 General format: variable = expression
 Example: age = 29
 Assignment operator : the equal sign (=)

 The structure of a variable :


 Name -- chosen by the programmer (aka identifier)
 Type -- derived from the declaration of the variable
 Size -- determined by the type
 Value -- the data stored in the variable's memory location

RAISONI GROUP OF INSTITUTIONS 11


1.2 Python Building Blocks?
 Comments :Comments are non-executable statements in Python. It means
neither the python compiler nor the PVM will execute them. Comments are
intended for human understanding, not for the compiler or PVM. Therefore they
are called non-executable statements.
 There are two types of commenting features available in Python: These are
single-line comments and multi-line comments.
 A single-line comment begins with a hash (#) symbol and is useful in
mentioning that the whole line should be considered as a comment until the
end of the line.
Example: #Defining a variable to store number.
n = 50 #Store 50 as value into variable n.
 Multi-line comment is useful when we need to comment on many lines. In
Python Triple double quote (""") and single quote (''') are used for Multi-line
commenting. It is used at the beginning and end of the block to comment on the
entire block. Hence it is also called block comments.
 Example: """
Author: www.schools.in
Description: Writes the words Hello World on the screen

""" RAISONI GROUP OF INSTITUTIONS 12


1.3 Python Environment Setup?

Install python on windows:


On Windows machines, a Python installation
is usually placed in C:\Users\UserName\
AppData\Local\Programs\Python\
Python(Version), although you can change
this while running the installer.
To install Python on a Windows machine,
follow these steps:
• Download Python for Windows.
• Run the Python installer.
• Here you will see the Python wizard, which
is very easy to use. Just accept the default
recommended settings and click on the
Next button, wait until the install is
complete and you are done.

RAISONI GROUP OF INSTITUTIONS 13


1.3 Python Environment Setup?

Python GUI:

There are various GUI based Python IDE that python programmers can use for
better coding experience.

An IDE (Integrated Development Environment) understand your code much better than a
text editor. It usually provides features such as build automation, code linting, testing and
debugging. This can significantly speed up your work. The downside is that IDEs can be
complicated to use.

Names of some Python interpreters are:

• PyCharm
• Python IDLE
• The Python Bundle
• pyGUI
• Sublime Text etc.

RAISONI GROUP OF INSTITUTIONS 14


1.4 Running Simple Python Script?

Python Script:

>>>print('Welcome to the world of Python')

Output:

Welcome to the world of Python

RAISONI GROUP OF INSTITUTIONS 15


1.5 Python Data Types?
Python data types are different in some aspects from other programming
languages. It is simple to understand and easy to use. Because Python is
interpreted programming language and Python interpreter can determine
which type of data is storing, so no need to define the data type of memory
location.

RAISONI GROUP OF INSTITUTIONS 16


1.5 Python Data Types?

RAISONI GROUP OF INSTITUTIONS 17


1.5 Python Data Types?
Numeric :
In Python, numeric data type represent the data which has numeric value.
Numeric value can be integer, floating number or even complex numbers. These
values are defined as int , float and complex class in Python. Integers – This value
is represented by int class.

Working with Numeric Datatype:

# Python program to
# demonstrate numeric value

a=5
print("Type of a: ", type(a)) Output:
Type of a: <class 'int'>
b = 5.0
print("\nType of b: ", type(b)) Type of b: <class 'float'>

c = 2 + 4j Type of c: <class 'complex'>


print("\nType of c: ", type(c))
RAISONI GROUP OF INSTITUTIONS 18
1.5 String Data Types?

String :
 In Python, Strings are arrays of bytes representing Unicode characters.
 A string is a collection of one or more characters put in a single quote,
double-quote or triple quote.
 In python there is no character data type, a character is a string of length
one.
 It is represented by str class.

 Example:

RAISONI GROUP OF INSTITUTIONS 19


1.5 String Data Types?
# Creating a String with single Quotes
String1 = 'Welcome to the Disney World' Output:
print("String with the use of Single Quotes: ") String with the use of Single Quotes:
print(String1) Welcome to the Disney World

# Creating a String with double Quotes String with the use of Double
String1 = "I'm a ABC" Quotes:
print("\nString with the use of Double Quotes: ")
I'm a ABC
print(String1)
print(type(String1)) <class 'str'>

# Creating a String with triple Quotes String with the use of Triple Quotes:
String1 = '''I'm a Geek and I live in a world of "Geeks"''' I'm a Geek and I live in a world of
print("\nString with the use of Triple Quotes: ") "Geeks"
print(String1) <class 'str'>
print(type(String1))
# Creating String with triple Quotes allows multiple
Creating a multiline String:
lines
String1 = '''Geeks Geeks
For For
Life''' Life
print("\nCreating a multiline String: ")
print(String1)
RAISONI GROUP OF INSTITUTIONS 20
1.5 Tuple Data Types?

Tuple:
 Tuples are used to store multiple items in a single variable.
 Tuple is one of 4 built-in data types in Python used to store collections of data,
the other 3 are List, Set, and Dictionary, all with different qualities and usage.
 A tuple is a collection which is ordered and unchangeable.
 A tuple is created by placing all the items (elements) inside parentheses (),
separated by commas.
 The parentheses are optional, however, it is a good practice to use them.

 A tuple can have any number of items and they may be of different types
(integer, float, list, string, etc.).

RAISONI GROUP OF INSTITUTIONS 21


1.5 Tuple Data Types?
# Creating an empty tuple Initial empty Tuple:
Tuple1 = () ()
print("Initial empty Tuple: ")
print (Tuple1)

# Creating a Tuple with the use of Strings


Tuple1 = ('Geeks', 'For') Tuple with the use of String:
print("\nTuple with the use of String: ") ('Geeks', 'For')
print(Tuple1

# Creating a Tuple with the use of list Tuple using List:


list1 = [1, 2, 4, 5, 6] (1, 2, 4, 5, 6)
print("\nTuple using List: ")
print(tuple(list1))

# Creating a Tuple with the use of built-in function


Tuple1 = tuple('Geeks') Tuple with the use of function:
print("\nTuple with the use of function: ") ('G', 'e', 'e', 'k', 's')
print(Tuple1)
RAISONI GROUP OF INSTITUTIONS 22
1.5 List Data Types?

List :

 Lists are used to store multiple items in a single variable.

 Lists are one of 4 built-in data types in Python used to store collections of data,
the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.

 Lists are created using square brackets:

 List items are ordered, changeable, and allow duplicate values.

 List items are indexed, the first item has index [0], the second item has index [1]
etc.

 A list can contain different data types

RAISONI GROUP OF INSTITUTIONS 23


1.5 List Data Types?
# Creating a List
List = []
print("Intial blank List: ")
print(List) Intial blank List:
[]
# Creating a List with the use of a String
List = ['GeeksForGeeks'] List with the use of String:
print("\nList with the use of String: ") ['GeeksForGeeks']
print(List)
List containing multiple
# Creating a List with the use of multiple values values:
List = ["Geeks", "For", "Geeks"] Geeks
print("\nList containing multiple values: ") Geeks
print(List[0])
print(List[2]) Multi-Dimensional List:
[['Geeks', 'For'], ['Geeks']]
# Creating a Multi-Dimensional List (By Nesting
a list inside a List)
List = [['Geeks', 'For'], ['Geeks']]
print("\nMulti-Dimensional List: ")
print(List) RAISONI GROUP OF INSTITUTIONS 24
1.5 Dictionary Data Types?

Dictionary :
 Dictionary in Python is an unordered collection of data values, used to store
data values like a map, which unlike other Data Types that hold only single
value as an element, Dictionary holds key:value pair.
 Key value is provided in the dictionary to make it more optimized.
 Dictionary can be created by placing sequence of elements within curly {}
braces, separated by ‘comma’.
 Values in a dictionary can be of any datatype and can be duplicated, whereas
keys can’t be repeated and must be immutable.
 Dictionary keys are case sensitive, same name but different cases of Key will
be treated distinctly.

RAISONI GROUP OF INSTITUTIONS 25


1.5 Dictionary Data Types?

# Creating a Dictionary with Integer Keys


Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)

# Creating a Dictionary with Mixed keys


Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)

Output
Dictionary with the use of Integer Keys:
{1: 'Geeks', 2: 'For', 3: 'Geeks'}

Dictionary with the use of Mixed Keys:


{1: [1, 2, 3, 4], 'Name': 'Geeks'}
RAISONI GROUP OF INSTITUTIONS 26
1.5 Declaration & Use of Data Types?

RAISONI GROUP OF INSTITUTIONS 27


Thank you !

RAISONI GROUP OF INSTITUTIONS 28

You might also like