Introduction To Python
Introduction To Python
Programming
•Introduction to computer languages
and python program compilation
•Variables and constants
•Operators
•Input and output
Samir V
Samir V
Computer Languages
Computer languages can be classified into following
categories:
Computer
Language
Fourth
Machine Assembly High level
generation
Language Language Language
Language
Samir V
• It is a binary language in which
the instructions are in the form
of ‘0’ and ‘1’.
• The program written in ‘0’ and
‘1’ can be understood and
executed by the computer.
• It is very difficult to write and
test programs written in
machine language.
Samir V
• It is a program written using English
keywords or symbols or mnemonics.
Samir V
• It is a computer program written
using short English phrases.
• It is easy to learn and requires less
time to write.
• The program written using high level
language is not understood by the
computer.
• It is thus converted to machine
language using translators.
• For example: BASIC, C, C++, Java,
Python
Samir V
• It is closer to human language than
any other high level language.
• It is more visual and does not
require a lot of programming
knowledge.
• It is designed to reduce the overall
time, effort and cost of software
development.
• For example: SQL
Samir V
Python History
• Developed in the early 1990s by Guido van Rossum.
Pros:
Free , Powerful, Widely used
Python programmers could quickly write programs (and not
be burdened with an overly difficult language)
Cons:
Python programs weren’t optimized to run as efficiently as
programs written in some other languages.
Samir V
The Process Of Creating A Computer Program
Program Creation
• A person (programmer) writes a computer program
(series of instructions).
• The program is written and saved using a text
editor.
• The instructions in the programming language (e.g.,
Python) are high level (look much like a human
language).
Translation
Execution
• A special computer program (translator) translates
the program written by the programmer into the • The machine/binary
only form that the computer can understand language instructions can
now be directly executed by
(machine language/binary)
the computer.
slide 9
Samir V
Types Of Translators
1) Interpreters (e.g., Python is an interpreted language)
• Each time the program is run the interpreter translates the program
(translating a part at a time).
• If there are any translation errors during the process of interpreting the
program, the program will stop execution right when the error is encountered.
• Specify advantages: partial execution, multi-platform
Samir V
Compiling and Linking in Python
• Python first compiles your source code (.py file) into a format known
as byte code . Compilation is simply a translation step, and byte code is a
lower-level, and platform-independent, representation of your source code.
Compiled code is usually stored in .pyc files , and is regenerated when the
source is updated, or when otherwise necessary. In order to distribute a
program to people who already have Python installed, you can ship either
the .py files or the .pyc files.
• The bytecode (.pyc file) is loaded into the Python runtime and interpreted
by a Python Virtual Machine , which is a piece of code that reads each
instruction in the bytecode and executes whatever operation is indicated.
Samir V
Variables
• Set aside a location in memory.
• Used to store information.
– This location can store one ‘piece’ of information.
• Putting another piece of information at an existing location overwrites previous
information.
– At most the information will be accessible as long as the program runs i.e., it’s
temporary
• Some types of information which can be stored in variables include: integer
(whole), floating point (fractional), strings (essentially any characters you
can type and more)
Format (creating):
<name of variable> = <Information to be stored in the variable>
Examples (creating):
– Integer (e.g., num1 = 10)
– Floating point (e.g., num2 = 10.0)
– Strings: alpha, numeric, other characters enclosed in quotes.
• e.g., name = "james"
• To be safe get in the habit of using double (and not single) quotes
slide 12
Samir V
Variable Naming Conventions
• Naming requirements:
– The name should be meaningful.
– Names must start with a letter (Python requirement) and
should not begin with an underscore (style requirement).
– Names are case sensitive
– Variable names should generally be all lower case.
– For names composed of multiple words, separate each word
by capitalizing the first letter of each or by using an
underscore.
– Can't be a keyword.
Samir V
Key Words In Python1
and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try
#YES
BIRTH_RATE = 17.58
MORTALITY_RATE = 0.1257
currentPopulation = 1000000
populationChange = (BIRTH_RATE - MORTALITY_RATE) *
currentPopulation
Samir V
Why Use Named Constants (2)
2) Makes the program easier to maintain
– If the constant is referred to several times throughout the program,
changing the value of the constant once will change it throughout the
program.
– Using named constants is regarded as “good style” when writing a
computer program.
Samir V
Purpose Of Named Constants (3)
BIRTH_RATE = 0.998
MORTALITY_RATE = 0.1257
populationChange = 0
currentPopulation = 1000000
populationChange = (BIRTH_RATE - MORTALITY_RATE) *
currentPopulation
if (populationChange > 0):
print("Increase")
print("Birth rate:", BIRTH_RATE, " Mortality rate:",
MORTALITY_RATE, " Population change:", populationChange)
elif (populationChange < 0):
print("Decrease")
print("Birth rate:", BIRTH_RATE, " Mortality rate:",
MORTALITY_RATE, "Population change:", populationChange)
else:
print("No change")
print("Birth rate:", BIRTH_RATE, " Mortality rate:",
MORTALITY_RATE, "Population change:", populationChange)
Samir V
Operators in python
Python divides the operators in the following groups:
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
5. Bitwise operators
6. Identity operators
7. Membership operators
Samir V
Samir V
Arithmetic Operators: Arithmetic operators in Python allow you to
perform basic mathematical operations such as addition, subtraction,
multiplication, division, and more. They include:
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulo (%) – Gives reminder of a division operation
Floor Division (//)- Gives only integer portion of division.
Exponentiation (**)
print(5/2)
>>2.5
#but if want only the integer part of 2.5 then we have to use //
print(5//2)
>>2
#modulo(%) is used to find the remainder
print(5%2)
>>1
#power operator is **
print(3**2)
>>9 Samir V
Assignment Operators:
1. Assignment (=)
2. Addition Assignment (+=)
3. Subtraction Assignment (-=)
4. Multiplication Assignment (*=)
5. Division Assignment (/=)
6. Modulo Assignment (%=)
7. Exponentiation Assignment (**=)
8. Floor Division Assignment (//=)
Samir V
Samir V
Comparison Operators:
Comparison operators are used to compare two values and determine their
relationship. They return a Boolean value (True or False) based on the
comparison. Some common comparison operators in Python include:
1. Equal to (==)
2. Not equal to (!=)
3. Greater than (>)
4. Less than (<)
5. Greater than or equal to (>=)
6. Less than or equal to (<=)
Samir V
Membership Operators:
Membership operators are used to test whether a value belongs to a sequence
or collection(list, tuple, or string). Python provides two membership operators:
1. In
2. Not in
Identity Operators:
Identity operators are used to compare the memory locations of two objects.
They determine if two variables point to the same object in memory. Python has
two identity operators:
1. is
2. is not
Samir V
Input/Output Function
Samir V
Displaying Output Using The Print() Function
print("hi")
print("hey",end="")
print("-sup?")
Samir V
Print("… ") Vs. Print(<name>)
• Enclosing the value in brackets with quotes means the value in
between the quotes will be literally displayed onscreen.
• Excluding the quotes will display the contents of a memory
location.
• Example:
aString = "Some message"
print(aString)
print("aString")
Samir V
Escape Codes/Characters
• The back-slash character enclosed within quotes won’t be
displayed but instead indicates that a formatting (escape) code
will follow the slash:
Escape sequence Description
Samir V
Escape Codes
print ("\a*Beep!*")
print ("hi\nthere")
Samir V
input()
The print() function makes it easy to send your data as text to standard out
(see also: print()).
The Python function input() goes the other direction, letting the user type
something that goes in a Python variable.
Eg-
The parameter to input() is a prompt string that prints out, prompting the
user what they are supposed to type.
Samir V
String Conversion
The result from input() is always a string, so it may need a conversion like
int() to convert it to a number
Samir V