Unit - 1 Python
Unit - 1 Python
PREPARED BY
Mr. P. NANDAKUMAR
ASSISTANT PROFESSOR,
DEPARTMENT OF INFORMATION TECHNOLOGY,
SVCET.
COURSE CONTENT
UNIT-I
INTRODUCTION TO PROBLEM SOLVING, EXPRESSION AND DATA
TYPES
Fundamentals: what is computer science - Computer Algorithms - Computer
Hardware - Computer software - Computational problem solving using the Python
programming language - Overview of Python, Environmental Setup, First program
in Python, Python I/O Statement. Expressions and Data Types: Literals, Identifiers
and Variables, Operators, Expressions. Data types, Numbers, Type Conversion,
Random Number.
Problem solving: Restaurant Tab calculation and Age in seconds.
FUNDAMENTALS OF COMPUTER SCIENCE
Python is a
cross-platform programming language
free and open source
general-purpose interpreted, interactive, object-oriented, and
high-level programming language
Python was created by “Guido Van Rossum” in the year 1991
(late 1980’s)
Designed to be easy as well as fun, the name "Python" is a nod to
the British comedy group Monty Python.
GUIDO VAN ROSSUM
Python Environmental Setup
Getting Python
The most up-to-date and current source code, binaries, documentation,
news, etc., is available on the official website of Python
https://www.python.org/
Windows Installation
Here are the steps to install Python on Windows machine.
Open a Web browser and go to https://www.python.org/downloads/.
Run the downloaded file. This brings up the Python install wizard,
which is really easy to use. Just accept the default settings, wait until the
install is finished, and you are done.
https://www.tutorialspoint.com/python/python_environment.htm
Python Features:
Easy-to-learn
Easy-to-read
Easy-to-maintain
A broad standard library
Interactive mode
Portable
Extendable
Databases
GUI Programming
Scalable
Top Python Applications
(2023)
Applications of Python:
Web Development
Game Development
Machine Learning and Artificial Intelligence
Data Science and Data Visualization
Desktop GUI
Web Scraping Applications
Business Applications
Audio and Video Applications
CAD Applications
Embedded Applications
Starting the Python
Two ways:
1. Immediate mode
2. Script mode
Typing python code in a file, such a file is called script.
Save script as filename.py (ends with .py extension – denotes
python file)
The IDLE Python Development Environment
IDLE is an integrated development environment ( IDE ).
An IDE is a bundled set of software tools for program development.
This typically includes an editor for creating and modifying
programs, a translator for executing programs, and a program
debugger.
A debugger provides a means of taking control of the execution of a
program to aid in finding program errors.
The Python Standard Library
The Python Standard Library is a collection of built-in modules ,
each providing specific functionality beyond what is included in the
“core” part of Python.
In order to utilize the capabilities of a given module in a specific
program, an import statement is used in the below figure.
A Bit of Python
Variables
A simple description of a variable is “a name that is assigned to a
value,” as shown below,
n = 5, variable n is assigned the value 5
If you have entered the program code correctly, the program should
execute as shown in below Figure.
Learning How to Use IDLE
If, however, you have mistyped part of the program resulting in a
syntax error (such as mistyping print), you will get an error message
similar to that in the following Figure.
FIRST PYTHON PROGRAM
>>>
FIRST PYTHON PROGRAM
Hello, World!
FIRST PYTHON PROGRAM
Hello, World!
INPUT AND OUTPUT STATEMENTS IN
PYTHON
The input() function helps to enter data at run time by the user
and the output function print() is used to display the result of the
program on the screen after execution.
Syntax:
input('prompt’)
where prompt is an optional string that is displayed on the
string at the time of taking input.
INPUT AND OUTPUT IN PYTHON
# Output
print("Hello, " + name)
print(type(name))
INPUT AND OUTPUT IN PYTHON
add = num + 1
# Output
print(add)
Data and Expressions
FUNDAMENTAL CONCEPTS
Literals
A literal is a sequence of one of more characters that stands for itself,
such as the literal 12.
A literal is a sequence of one or more characters that stands for
itself.
Numeric Literals
A numeric literal is a literal containing only the digits 0–9, an optional
sign character ( 1 or 2 ), and a possible decimal point.
Data and Expressions
Built-in format Function
The built-in format function can be used to produce a numeric string
version of the value containing a specific number of decimal places.
>>>12/5 >>>5/7
2.4 0.7142857142857143
'2.40’ '0.71'
The built-in format function can be used to produce a numeric
string of a given floating-point value rounded to a specific number
of decimal places.
Data and Expressions
String Literals
Numerical values are not the only literal values in programming. String
literals , or “ strings ,” represent a sequence of characters,
print('Hello\nJennifer Smith’)
Jennifer Smith
Data and Expressions
String Formatting
The format function can be used to control how strings are displayed.
the format function has the form,
format(value, format_specifier)
Example:
Variable Update
VARIABLES
Identifier Naming
Identifiers and Keywords in Python
>>> and = 10
True False
OPERATORS
What Is an Operator?
An operator is a symbol that represents an operation that may be
performed on one or more operands.
For example, the + symbol represents the operation of addition.
An operand is a value that a given operator is applied to, such as
operands 2 and 3 in the expression 2 + 3.
A unary operator operates on only one operand, such as the negation
operator in -12.
A binary operator operates on two operands, as with the addition
operator.
OPERATORS
Arithmetic Operators
Python provides the arithmetic operators given in the following Figure.
Division Operators in
OPERATORS
Other Operators
Lastly, the modulus operator (%) gives the remainder of the division
of its operands, resulting in a cycle of values. This is shown in the
following Figure.
operand types.
The modulus operator (%) gives the remainder of the division of its
operands.
EXPRESSIONS AND DATA TYPES
What Is an Expression?
An expression is a combination of symbols that evaluates to a value.
Expressions, most commonly, consist of a combination of operators and
operands,
4 + (3 * k)
An expression can also consist of a single literal or variable.
Expressions that evaluate to a numeric type are called arithmetic
expressions.
A subexpression is any expression that is part of a larger expression.
Subexpressions may be denoted by the use of parentheses ().
EXPRESSIONS AND DATA TYPES
Operator Precedence
The way we commonly represent expressions, in which operators
appear between their operands, is referred to as infix notation.
For example, the expression 4 + 3 is in infix notation since the +
operator appears between its two operands, 4 and 3.
There are other ways of representing expressions called prefix and
postfix notation, in which operators are placed before and after their
operands, respectively.
Mixed-Type Expressions
A mixed-type expression is an expression containing operands of
different type.
EXPRESSIONS AND DATA TYPES