PYTHON PROGRAMMING
Introduction
Prepared by:
Dr. Ayan Kumar Banerjee
Talk the talk
– High-level: Python, C++, Java
– Low-level: machine language/assembly languages, computers can
only execute these
– High-level languages have to be processed into low-level before
the computer can run them -this extra processing takes some
time, which is a small disadvantage of high-level languages
Talk the talk
- Programs written in a high-level language can be written and read by
humans; take less time to write
- they are shorter and easier to write and read, and they are more
likely to be correct.
- high-level languages are portable, meaning that they can run on
different kinds of computers with few or no modifications.
- Low-level programs can run on only one kind of computer and have
to be rewritten to run on another.
How does high-level get translated into low- level?
– Interpreters and compilers!
– Interpreter processes the program a little bit at a time and runs it
– Compiler translates everything before running it
high-level the translated
program is program is
called the called the
source code object code or
the executable
What is ?
Python is a programming language that let us work more
quickly and integrate our systems more effectively.
Python Vocab
• Program – A larger file of code that may contain one or more functions.
• Variable - names that you can assign values to, allowing you to reuse them
later on.
E.g.: x = 1 or msg = “Hi, I’m a message!”
• Comments – These are notes ignored by the computer. In Python, comments
start with a hash mark (#) and end at the end of the line.
E.g.: >>> x + y #both variables store user input
• Operators – Mathematical symbols, like +, -, *, and /, but also ** (for
exponents).
Python Vocab
• Keyword – Words with meaning/purpose in Python. E.g.
“and”, “print”, and “if”.
• Function – A chunk of code that performs an action. Functions
have names and are reusable. Kinds: built-in ones, and “user-
defined” ones.
• Expression – Statements that produce values. Examples include
3 + 5, "Hello world!“.
• Error – When your program has a problem, the command area
will provide an error message.
The bottom part (in black), the “command
area”, is the brains of the operation.
Indentation
• Not an Option, but a Requirement!
• In Python, indenting specifies the “scope” of
different chunks of your code.
def main():
print "Hello world!"
The print line will be executed when you invoke
main().
Similar formatting, different output
• print "Hello", "world", "!"
will output
Hello world !
• print
"Hello" +
"world" +
"!"
will output
Helloworld!
Data Types
The data type of an object determines the values it can hold, and
the operations which can be performed on it.
• Numeric Data
Numeric data comes in 2 main flavors:
– Integers (whole numbers) 2, 5, -7, etc.
– Floating Point Numbers (non-integers) 0.2, 5.125, etc.
Data Types
• Non-numeric Data Types:
Those include strings (text), lists, dictionaries, etc..
Basically anything you can not add up using a simple plus
sign (+).
Defining Variables
Rules for naming variables:
• Have to start with a letter or underscore (_)
• Can contain letters, numbers, and underscores
• Can’t contain spaces, punctuation, etc.
• Can’t be Python keywords
• Are case sensitive
Defining Variables
Things that aren’t rules, but are worth considering:
• You should give your variables sensible names (“price”,
“pixelColor, or “samplingRate” instead of “x”)
• Just because you technically can start your
variable names with underscores doesn’t mean you
should.
Defining Variables
• For multi-word variable names, two options:
– start capitalizing each word after the first “myCar”
– separate words with underscores. For instance, a variable
for “Ford Focus” could be “my_car”.
• Abbreviating is common for longer words. So, a variable for
“average price” could be “avgPrice” or even “avg”.
Numeric Operators
• Python built-in numeric operators:
• + addition
• - subtraction
• * multiplication
• / division
• ** exponentiation
• % remainder (modulo)
(CS108 Notes, Prof. Aaron Stevens)
Python Arithmetic
• Try writing the following code in your program area and see what it
outputs
Def main():
a = 12
b = 2
c = 16
d = 3
e = 2.5
print "the value of a is", a
print (a / b) * 5
print a + b * d
print (a + b) * d
print b ** d
print c - e
a = a + b
print "the value of
a is", a
Taking User Input
• requestNumber()
• requestInteger()
• requestIntegerInRange()
• requestString()
THANK YOU