VIT Bhopal University
Bhopal-Indore Highway, Kothrikalan, Sehore,
Madhya Pradesh – 466114.
Course Code
CSE1021
Introduction to Problem Solving
and Programming
MODULE 2 Credits 4
Unit -2
(8-Hours)
VIT Bhopal
Unit -2
Python Data, Expressions and Statements
1. Python interpreter and interactive mode
2. values and types – variables, expressions,
statements, tuple assignment, precedence of
operators,
3. comments – modules and functions, function
definition and use, flow of execution, parameters
and arguments.
What is Programming?
● Just like we use Hindi or English to
communicate with each other, we use a
Programming language to communicate
with the computer.
● Programming is a way to instruct the
computer to perform various tasks.
Introduction
● Python is a widely used general-purpose, high level programming
language.
● It was created by Guido van Rossum in 1991 and further developed by
the Python Software Foundation.
● It was designed with an emphasis on code readability, and its syntax
allows programmers to express their concepts in fewer lines of code.
● This Pseudo code nature of Python makes it easy to learn and
understandable for beginners.
Interpreter
● An interpreter is a kind of program that executes other
programs.
● When you write Python programs , it converts source code
written by the developer into intermediate language which is
again translated into the native language / machine language that
is executed.
Interactive “Shell”
● Great for learning the language
● Great for experimenting with the library
● Great for testing your own modules
● Two variations: IDE (GUI),python (command line)
● Type statements or expressions at prompt:
>>> print "Hello, world“ #Input
Hello, world #output
>>> x = 12**2 # x is a variable take as input
>>> 144 # output
Python Features:
○ Easy-to-learn:
■ Python is clearly defined and easily
readable.
■ The structure of the program is very simple.
■ It uses few keywords.
○ Easy-to-maintain:
■ Python's source code is fairly
easy-to-maintain.
Python Features:
○ Portable:
■ Python can run on a wide variety of
hardware platforms and has the same
interface on all platforms.
○ Interpreted:
■ Python is processed at runtime by the
interpreter. So, there is no need to compile a
program before executing it.
Installation
Create a file called hello.py
print(“Hello World”) # print is a function
print(‘“VIT
VIT
VIT’’’)
Modules
● A module is a file containing code written by somebody else
(usually) which can be imported and used in our programs.
● Pip
● Pip is a package manager for python. You can use pip to install a
module on your system.
● E.g., pip install tensorFlow
Types of modules
1. There are two types of modules in Python:
2. Built-in modules – Pre-installed in Python
3. External modules – Need to install using pip
4. Some examples of built-in modules are os, abc, etc.
5. Some examples of external modules are
TensorFlow, flask, etc.
Variable
● A variable is a name given to a memory
location in a program. For example
●
Variable – Container to store a value
● Keywords – Reserved words in Python
● Identifiers – class/function/variable name
Python Data Types
● Data types are the classification or categorization of
data items.
● It represents the kind of value that tells what
operations can be performed on a particular data.
● Since everything is an object in Python
programming, data types are actually classes and
variables are instance (object) of these classes.
The standard or built-in data type
of Python:
PRECEDENCE OF
OPERATORS
FLOW OF EXECUTION
✓ In order to ensure that a function is defined before its first use, you have to know the order in which
statements are executed, which is called the flow of execution.
✓ Execution always begins at the first statement of the program. Statements are executed one at a time, in
order from top to bottom.
✓ Function definitions do not alter the flow of execution of the program, but remember that statements
inside the function are not executed until the function is called.
✓ A function call is like a detour in the flow of execution. Instead of going to the next statement, the flow
jumps to the body of the function, executes all the statements there, and then comes back to pick up where it
left off. That sounds simple enough, until you remember that one function can call another.
✓ While in the middle of one function, the program might have to execute the statements in another
function. But while executing that new function, the program might have to execute yet another function!
✓ Fortunately, Python is good at keeping track of where it is, so each time a function completes, the
program picks up where it left off in the function that called it. When it gets to the end of the program, it
terminates.
Parameters and Arguments
✓ Some of the built-in functions we have seen require arguments.
✓ For example, when you call math.sin you pass a number as an argument. Some functions take more than one
argument:
math.pow takes two, the base and the exponent.
✓ Inside the function, the arguments are assigned to variables called parameters. Here is an example of a user-defined
function that takes an argument:
def print_twice (bruce):
print bruce print bruce
✓ This function assigns the argument to a parameter named bruce. When the function is called, it prints the value of
the parameter (whatever it is) twice.
✓ This function works with any value that can be printed.
>>> print_twice('Spam')
Spam
Spam
>>> print_twice(17)
17
17
>>> print_twice(math.pi)
3.14159265359
3.14159265359
Thanks!
Do you have
any
questions?