0% found this document useful (0 votes)
5 views47 pages

Python Programming Basics for Engineers

The document outlines a syllabus for a Python programming course for engineering students, covering fundamental concepts such as syntax, data types, variable naming rules, and modes of programming. It explains Python's features, including its interpreted nature, dynamic typing, and the use of indentation for code blocks. Additionally, it provides examples of various data types, operators, and functions used in Python programming.

Uploaded by

Soumya Panda
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views47 pages

Python Programming Basics for Engineers

The document outlines a syllabus for a Python programming course for engineering students, covering fundamental concepts such as syntax, data types, variable naming rules, and modes of programming. It explains Python's features, including its interpreted nature, dynamic typing, and the use of indentation for code blocks. Additionally, it provides examples of various data types, operators, and functions used in Python programming.

Uploaded by

Soumya Panda
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON FOR ENGINEERS

OEEC6205
4th Semester, B. Tech(CSE)
IGIT, Sarang
SYLLABUS

Module I
Parts of Python Programming Language, 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,
Dynamic and Strongly Typed Language.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


2
CSEA, IGIT, Sarang
INTRODUCTION

 Python is a general purpose programming language


created by Guido Van Rossum, and released in 1991.
 It works on different platforms (Windows, Linux, Mac,
Raspberry Pi, etc.).
 It has a simple syntax similar to the English language.
 It runs on an interpreter system, meaning that code can
be executed as soon as it is written.
 A python program can be written in a Procedural way,
a Functional way or an Object Oriented way.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


3
CSEA, IGIT, Sarang
PYTHON SYNTAX

 It uses new lines to complete a command, as opposed


to other programming languages which often use
semicolons or parentheses.

 It relies on indentation, using whitespace, to define


scope; such as the scope of loops, functions and
classes.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


4
CSEA, IGIT, Sarang
MODE OF PYTHON PROGRAMMING
 Python is considered an interpreted language because
Python programs are executed by an interpreter.
 There are two modes of writing a Python program.
(i) Interactive Mode:
Type the Python statement and the interpreter
displays the result.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


5
CSEA, IGIT, Sarang
MODE OF PYTHON PROGRAMMING
(ii) Script Mode:
a) A python statement or group of python statements
are stored in a file.

b) The interpreter executes the contents of the file,


which is called a script.

c) Python scripts have names that end with .py.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


6
CSEA, IGIT, Sarang
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
7
CSEA, IGIT, Sarang
COMMENTS AND DOCUMENTATION
 The three different ways of comment in Python are:

1) Single Line Comment


Single-line comments begin with the hash character
(#) and are terminated by the end of line.
Example:
# This is a single line comment in Python

2) Inline Comment
A comment is added along with the Python statement.
Example:
print("Hello Python") # This line prints Hello Python
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
8
CSEA, IGIT, Sarang
COMMENTS AND DOCUMENTATION
3) Multiple Line Comment
Comments spanning multiple lines have """(three
double quotes) or '''(three single quotes) on either
end.
Example:
""" This type of comment spans multiple lines. These
are mostly used for documentation of functions,
classes and modules. """
(OR)
''' This type of comment spans multiple lines. These
are mostly used for documentation of functions,
classes and modules. '''
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
9
CSEA, IGIT, Sarang
RULES FOR NAMING VARIABLES
1) It may consist of both alphabets and digits.

2) It should not start with a digit.

3) It should start with an alphabet.

4) No special characters are used in the variable name except


underscore ( _ ).

5) It may start with an underscore ( _ ) but, generally avoid


doing this unless writing library code for others to use.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


10
CSEA, IGIT, Sarang
RULES FOR NAMING VARIABLES
6) Generally use all lowercase alphabets.
7) Keywords cannot be used.
8) Valid Examples:
sum
add_and_product
age
age45
9) Invalid Examples:
4age
add and product
rate@
percentage%
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
11
CSEA, IGIT, Sarang
KEYWORDS

 Python has 33 keywords.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


12
CSEA, IGIT, Sarang
VARIABLE

 A variable is a name that refers to a value.


 Assignment Operator (=): It creates a new variable and
stores the value.
 Syntax:
variable_name = value

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


13
CSEA, IGIT, Sarang
EXAMPLES
1) String value is stored in the variable 'name'
name = 'Python'
2) Integer value is stored in the variable 'a'
a = 10
3) Float value is stored in the variable 'radius'
radius = 3.5
4) Complex value is stored in the variable 'c'
c = 10+20j
5) Boolean value is stored in the variable 'flag'
flag = True
6) None value is stored in the variable 'sum' and
afterwards any type of data can be stored.
sum = None
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
14
CSEA, IGIT, Sarang
DATA TYPES

 The type of a variable is the type of the value it refers to.

 The four basic data types in python are:


1) String
2) Number
a) Integer
b) Floating Point
c) Complex Number
3) Boolean
4) None

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


15
CSEA, IGIT, Sarang
STRING DATA TYPE

 A group of characters are enclosed in between single


quotes (' ') or double quotes ("" "").

 A Multiline string is always enclosed in between triple


quotes (""" """).

 The data type is str.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


16
CSEA, IGIT, Sarang
STRING DATA TYPE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


17
CSEA, IGIT, Sarang
STRING DATA TYPE

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


18
CSEA, IGIT, Sarang
NUMBER DATA TYPE

 There are three different ways to represent numbers in


Python.

 They are
1. Integer (or integral) numbers
2. Floating point numbers
3. Complex numbers

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


19
CSEA, IGIT, Sarang
INTEGER NUMBERS

 Integers are whole numbers that do not need to have a


fractional element.

 All integer values, no matter how big or small are


represented by the type int.

 Example:

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


20
CSEA, IGIT, Sarang
FLOATING POINT NUMBERS

 Python represents floating point numbers using a


decimal point to separate the whole part from the
fractional part of the number.

 The type used to represent a floating point number is


called float.

 Example:

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


21
CSEA, IGIT, Sarang
COMPLEX NUMBERS

 A complex number is defined by a real part and an


imaginary part.
 The data type is complex.
 It has the form a + bi (where i is the imaginary part
and a and b are real numbers).
 Examples:

 Two complex numbers can be added, subtracted,


multiplied, and divided by using arithmetic
operators.
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
22
CSEA, IGIT, Sarang
BOOLEAN VALUES

 A Boolean type can only be one of True or False.

 The data type is bool.

 Examples:

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


23
CSEA, IGIT, Sarang
NONE VALUE

 It is a special data type called NoneType, with a single


value, None.
 It is used to represent null values or nothingness.

 It is not the same as False, or an empty string or 0.

 It is a non-value.

 Examples:

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


24
CSEA, IGIT, Sarang
ARITHMETIC OPERATORS

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


25
CSEA, IGIT, Sarang
ORDER OF OPERATIONS

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


26
CSEA, IGIT, Sarang
ORDER OF OPERATIONS

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


27
CSEA, IGIT, Sarang
OUTPUT
FUNCTION

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


28
CSEA, IGIT, Sarang
print( ) FUNCTION

 It prints any value (constant / variable / expression /


both).

 Syntax-1 (one value):


print (value)

 Syntax-2 (multiple values):


print (value-1, value-2,…….,value-n)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


29
CSEA, IGIT, Sarang
print( ) FUNCTION

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


30
CSEA, IGIT, Sarang
QUESTIONS
1. Interchange 12 and 7.
2. The given five numbers are 5, 10.6, 7, 12, and 3. Then find the
sum and the average. Then print both the results with proper
prompt message.
3. Divide 10 by 3 to get the remainder, quotient, quotient (complete
division). Then print all the three results with proper prompt
message.
4. The given number is 1025. Then get the left part as 10 and the
right part as 25. Then find the square of (left part + right part).
Then print the number and the result.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


31
CSEA, IGIT, Sarang
type( ) FUNCTION
 This function is used to know the data type present in a
variable or data type of a constant.
Syntax: type (constant / variable)

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


32
CSEA, IGIT, Sarang
DATA TYPE
CONVERSION
FUNCTIONS

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


33
CSEA, IGIT, Sarang
int( ) FUNCTION

 It converts the integer data of string and Boolean kind to


integer kind.
 Syntax: int(Constant / Variable)

 Example-1: Example-2:

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


34
CSEA, IGIT, Sarang
int( ) FUNCTION
 Example-3: Example-4:

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


35
CSEA, IGIT, Sarang
float( ) FUNCTION
 It converts the floating data of string kind to floating kind.
 Syntax:
float(Floating_of_String_Kind_Value / Variable)

 Example-1: Example-3:

 Example-2:

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


36
CSEA, IGIT, Sarang
bool( ) FUNCTION
 It converts any non-zero value to True and 0 to False.
 Syntax: bool (value)
 Examples:

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


37
CSEA, IGIT, Sarang
complex( ) FUNCTION

 It changes two real values to a complex number format.


 Syntax: complex(real_part, imaginary_part)
 Example-1: Example-2:

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


38
CSEA, IGIT, Sarang
complex( ) FUNCTION
 Example-3:

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


39
CSEA, IGIT, Sarang
INPUT
FUNCTION

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


40
CSEA, IGIT, Sarang
input( ) FUNCTION
 It allows you to input an unknown value in a variable.
 Syntax:
variable_name = input('Any Prompt Message')
 Example-1:

 Example-2:

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


41
CSEA, IGIT, Sarang
Program: Read a student name, roll, branch, and total marks. Then
display all.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


42
CSEA, IGIT, Sarang
 The program has been saved in a file called "prog2022_1.py".
 Then run the script by pressing F5 or click .
 Then the cursor appears in the IPython console which is given
below.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


43
CSEA, IGIT, Sarang
QUESTIONS
1) Read any three numbers. Then find its sum and average.
2) Read principle, rate, and time. Then calculate simple
interest and amount.
3) Read radius of a circle. Then calculate area and
circumference.
4) Read two integers. Then swap both the integers.
5) Read an employee name, designation, age, gender, and
salary. Then display all with proper prompt message.
6) Read a distance in Kilometers. Then convert into meters and
centimeters. Then display all with proper prompt message.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


44
CSEA, IGIT, Sarang
INDENTATION

 Python use of indentation to mark blocks of code.

 Each line of code must be indented by the same


amount to denote a block of code.

 Indentation is required to indicate which block of


code or statement belongs to.

PYTHON FOR ENGINEERS By Mr. Bapuji Rao,


45
CSEA, IGIT, Sarang
SUITES
 A collection of individual statements that makes a single code
block are called suites.
 A header line followed by a suite are required for compound or
complex statements such as if, while, def, and class.
 Header lines begin with a keyword, and terminate with a
colon (:) and are followed by one or more lines that make up
the suite.
 Example:
# if statement is an example of suites.
x=1
if x == 1:
___print ('x has a value of 1')
else:
___print ('x does NOT have a value of 1')
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
46
CSEA, IGIT, Sarang
DYNAMIC AND STRONGLY TYPED LANGUAGE
 In Python, types are determined automatically at runtime, not in response
to declarations in your code.
 This means that you never declare variables ahead of its use.
 All variables must be explicitly assigned before they can be used;
referencing unassigned variables results in errors.

 Example: a = 10
 VARIABLE CREATION
A variable (i.e., a), is created when the code assigns the value 10 to it.
Future assignments change the value of the already created name.
 VARIABLE TYPES
A variable never has any type information or constraints associated with
it.
 VARIABLE USE
When a variable appears in an expression, it is immediately replaced with
the object that it currently refers to, whatever that may be.
PYTHON FOR ENGINEERS By Mr. Bapuji Rao,
47
CSEA, IGIT, Sarang

You might also like