Professional Documents
Culture Documents
COMPUTING
Basic Elements of Python Programs
Mohammad Hammoud
Carnegie Mellon University in Qatar
• Development started in the 1980’s by Guido van Rossum.
• Only became popular in the last decade or so.
• Python 2.x currently dominates, but Python 3.x is the future of
Python.
• Interpreted, very-high-level programming language.
• Supports a multitude of programming paradigms.
• OOP, functional, procedural, logic, structured, etc.
• General purpose.
• Very comprehensive standard library includes numeric modules, crypto
services, OS interfaces, networking modules, GUI support, development
tools, etc.
ABOUT PYTHON
• Easy to learn.
• Supports quick development.
• Cross-platform.
• Open Source.
• Extensible.
• Embeddable.
• Large standard library and active community.
• Useful for a wide variety of applications.
NOTABLE FEATURES
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
LITERALS
A literal is used to indicate a specific value, which can be assigned to
a variable
>>> x = 2
>>> print(x)
x is a variable and 2 is its value 2
>>> x = 2.3
>>> print(x)
2.3
>>> x = 2
>>> print(x) Before After
2
>>> x = 2.3 x 2 x = 2.3 2.3
>>> print(x)
2.3
X
known as garbage collection
will be
x automatically
reclaimed by the
2.3 garbage collector
GARBAGE COLLECTION
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?
ASSIGNING INPUT
Here is a sample interaction with the Python interpreter:
Still a string!
ASSIGNING INPUT
Here is a sample interaction with the Python interpreter:
ASSIGNING INPUT
Here is a sample interaction with the Python interpreter:
ASSIGNING INPUT
Here is another sample interaction with the Python interpreter:
ASSIGNING INPUT
Besides, we can convert the string output of the input function into an integer or a
float using the built-in int and float functions
>>> number = int(input("Enter a number: "))
Enter a number: 3
>>> print(number)
An integer
3
(no single quotes)!
>>>
DATATYPE CONVERSION
Besides, we can convert the string output of the input function into an integer or a
float using the built-in int and float functions
DATATYPE CONVERSION
As a matter of fact, we can do various kinds of conversions between strings, integers
and floats using the built-in int, float, and str functions
This form of assignment might seem strange at first, but it can prove remarkably
useful (e.g., for swapping values)
>>> x, y = 2, 3
>>> print(x)
2
>>> print(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
>>> y = 3
>>> x = y
X
>>> y = x CANNOT be done with
>>> print(x) two simple assignments
3
>>> print(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
>>> y = 3
Thus far, we have been using >>> temp = x
different names for >>> x = y CAN be done with
>>> y = temp three simple assignments,
variables. These names >>> print(x)
are technically called but more efficiently with
3
identifiers >>> print(y) simultaneous assignment
2
>>>
SIMULTANEOUS ASSIGNMENT
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
>>> print(2celsius)
File "<stdin>", line 1
2celsius
^
SyntaxError: invalid syntax
Python has some rules about how identifiers can be formed
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
IDENTIFIERS
Python Keywords
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
for = 4
^
An example… SyntaxError: invalid syntax
IDENTIFIERS