PY-101 Lecture03
PY-101 Lecture03
Programming
Basic Elements of Python Programs
Lecture 3
Recap
• A computer is a universal information-processing machine, which can carry out
any process that can be described in sufficient detail
• The process of creating software is called programming
• A basic functional view of a computer system comprises a central processing
unit (CPU), a main memory, a secondary memory, and input and output devices
• Programs are written using a formal notation known as a programming
language
• There are many different languages, but all share the property of having a
precise syntax (form) and semantics (meaning)
• Computer hardware only understands a very low-level language known as
machine language
Recap
• Today’s Session:
• Basic Elements of Python Programs: Literals, Assignments, Datatype Conversion,
Identifiers, and Expressions
• Announcement:
• Assignment
Elements Of A Program
The Difference Between Brackets, Braces,
and Parentheses
• ( ) Parentheses
• [ ] Brackets
• { } Braces
Simple Expressions
• 1. 2300 + 20
• 2. 4800 / 12
• 3. 11 * 19
Values & Types
• A value is one of the basic things a program works with.
• It can be a letter or a number.
• Example: Integer
• 14
• ‘Hello !! ’ String
• 13.235
Float
• Every value in Python has a type.
Values & Types
• Printing Values:
• Syntax:
print (value)
Examples:
print (“Python is fun”) Python is fun
print (3.14) 3.14
Values & Types
• Self-Review Exercise:
Determine the type of the following value:
It’s a String….
Values & Types
• E.g.
• 2 + 4.0 = 6.0
• 15.0 / 3=???
Variables
• The operand to the left of the = operator is the name of the variable and the
operand to the right of the = operator is the value stored in the variable.
Variables
Syntax:
Variable = Value
• Example:
• name = “Moosa”
• miles = 25.6
Display the value of a variable
Syntax(print value):
Print (variable_name)
• X = X + 1;
X 10 + 1
= 11
Variable names and keywords
1. Choose meaningful names for variables.
2. They can contain both letters and numbers, but they cannot start with a
number.
3. It is legal to use uppercase letters, but it is a good idea to begin variable names
with a lowercase letter.
4. The underscore character (_) can appear in a variable name.
5. Variable names can start with an underscore character, but generally it should
be avoided.
6. For variables with an illegal name, interpreter gives a syntax error.
7. Keywords can not be used as variable name.
Self-Review Exercise
• number1 =
268
amount$= 56.89
pass = 10
Variable names and keywords
Keywords:
The words reserved by the
python language.
Literals
• In the following example, the parameter values passed to the print
function are all technically called literals
• More precisely, “Hello” and “Programming is fun!” are called textual literals,
while 3 and 2.3 are called numeric literals
>>> print("Hello")
Hello
>>> print("Programming is fun!")
Programming is fun!
>>> print(3)
3
>>> print(2.3)
2.3
Simple Assignment Statements
• A literal is used to indicate a specific value, which can be assigned to
a variable
>>> x = 2
x is a variable and 2 is its value >>> print(x)
2
>>> x = 2.3
>>> print(x)
2.3
Simple Assignment Statements
• A literal is used to indicate a specific value, which can be assigned to
a variable
>>> x = 2
x is a variable and 2 is its value >>> print(x)
2
x can be assigned different values; >>> x = 2.3
hence, it is called a variable >>> print(x)
2.3
Simple Assignment Statements: Box View
• A simple way to view the effect of an assignment is to assume that
when a variable changes, its old value is replaced
>>> x = 2 x = 2.3
Before After
>>> print(x)
2 x 2 x 2.3
>>> x = 2.3
>>> print(x)
2.3
Simple Assignment Statements: Actual View
• Python assignment statements are actually slightly different from the
“variable as a box” model
• In Python, values may end up anywhere in memory, and variables are used to
refer to them
x = 2.3
>>> x = 2
Before After
>>> print(x) What will
2 2 happen to
x 2 x
>>> x = 2.3 value 2?
>>> print(x)
2.3
2.3
Garbage Collection
• Interestingly, as a Python programmer you do not have to worry about
computer memory getting filled up with old values when new values are
assigned to variables
After
Memory location
• Python will automatically clear old
values out of memory in a process
known as garbage collection
x 2
X will be automatically
reclaimed by the
garbage collector
2.3
Assigning Input
• So far, we have been using values specified by programmers and printed or
assigned to variables
• How can we let users (not programmers) input values?
>>> x = 2
>>> y = 3
>>> x = y
>>> y = x
>>> x
3
X CANNOT be done with
two simple assignments
>>> y
3
Simultaneous Assignment
• Suppose you have two variables x and y, and you want to swap their
values (i.e., you want the value stored in x to be in y and vice versa)
>>> x = 2
Thus far, we have been using >>> y = 3
different names for >>> temp = x CAN be done with
variables. These names >>> x = y three simple assignments,
are technically called
identifiers
>>> y = temp
>>> x
but more efficiently with
simultaneous assignment
3
>>> y
2
>>>
Identifiers
• Python has some rules about how identifiers can be formed
• Every identifier must begin with a letter or underscore, which may be
followed by any sequence of letters, digits, or underscores
>>> x1 = 10
>>> x2 = 20
>>> y_effect = 1.5
>>> celsius = 32
>>> 2celsius
File "<stdin>", line 1
2celsius
^
SyntaxError: invalid syntax
Identifiers
• Python has some rules about how identifiers can be formed
• Identifiers are case-sensitive
>>> x = 10
>>> X = 5.7
>>> print(x)
10
>>> print(X)
5.7
Identifiers
• Python has some rules about how identifiers can be formed
• Some identifiers are part of Python itself (they are called reserved words or
keywords) and cannot be used by programmers as ordinary identifiers
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
Python Keywords
Identifiers
• Python has some rules about how identifiers can be formed
• Some identifiers are part of Python itself (they are called reserved words or
keywords) and cannot be used by programmers as ordinary identifiers
>>> for = 4
File "<stdin>", line 1
An example… for = 4
^
SyntaxError: invalid syntax
Expressions
• You can produce new data (numeric or text) values in your program
using expressions
>>> x = 2 + 3
This is an expression that uses the >>> print(x)
addition operator 5
>>> print(5 * 7)
35
>>> print("5" + "7")
57
Expressions
• You can produce new data (numeric or text) values in your program
using expressions
>>> x = 2 + 3
This is an expression that uses the >>> print(x)
addition operator 5
>>> print(5 * 7)
This is another expression that uses the 35
multiplication operator >>> print("5" + "7")
57
Expressions
• You can produce new data (numeric or text) values in your program
using expressions
>>> x = 2 + 3
This is an expression that uses the >>> print(x)
addition operator 5
>>> print(5 * 7)
This is another expression that uses the 35
multiplication operator >>> print("5" + "7")
57
This is yet another expression that uses the
addition operator but to concatenate (or glue)
strings together
Expressions
• You can produce new data (numeric or text) values in your program
using expressions
• A Python module file is just a text file with a .py extension, which can
be created using any program for editing text (e.g., notepad or vim)
Programming Environments and IDLE
• A special type of software known as a programming environment
simplifies the process of creating modules/programs
• Operators are used to form and combine expressions into more complex
expressions (e.g., the expression x + 3 * y combines two expressions
together using the + and * operators)
Summary
• In Python, assignment of a value to a variable is done using the equal sign (i.e.,
=)
• Using assignments, programs can get inputs from users and manipulate them
internally
• Python allows simultaneous assignments, which are useful for swapping values
of variables