Introduction To Python
Introduction To Python
Why Python?
• Simple
• Python is a simple and minimalistic language in nature
• Reading a good python program should be like reading English
• Its Pseudo-code nature allows one to concentrate on the problem rather than the language
• Easy to Learn
• Free & Open source
• Freely distributed and Open source
• Maintained by the Python community
• High Level Language –memory management
• Portable – *runs on anything c code will
2
• Interpreted
• You run the program straight from the source code.
• Python program →Bytecode →a platforms native language
• You can just copy over your code to another system and it will auto-magically work! *with python
platform
• Object-Oriented
• Simple and additionally supports procedural programming
• Extensible – easily import other code
• Embeddable –easily place your code in non-python programs
• Extensive libraries
• (i.e. reg. expressions, doc generation, CGI, ftp, web browsers, ZIP, WAV, cryptography, etc...)
(wxPython, Twisted, Python Imaging library)
3
How to use Python?
• Python can be used in Interactive Mode and Script Mode
• Interactive:
• Type directly to a python one line at a time and it responds
• Script:
• You enter a sequence of statements (lines) into a file using a text editor and tell Python to
execute the statements in the file
• Interactive Python is good for experiments and programs of 3-4 lines long
• Most programs are much longer, so we type them into a file(called as
Python Script) and tell Python to run the commands in the file.
• As a convention, we add “.py” as the suffix on the end of these files to
indicate they contain Python
Python Scripts
• When you call a python program from the command line the
interpreter evaluates each expression in the file
• Familiar mechanisms are used to provide command line arguments
and/or redirect input and output
• Python also has mechanisms to allow a python program to act both
as a script and as a module to be imported and used by another
python program
Reserved Words
You cannot use reserved words as variable names / identifiers
x = 3.9 * x * ( 1 - x )
A variable is a memory location x 0.6
used to store a value (0.6)
0.6 0.6
x = 3.9 * x * ( 1 - x )
0.4
0.4
The right side is an expression. Once the
expression is evaluated, the result is
placed in (assigned to) the variable on the
0.936
left side (i.e., x).
Python types
• Str, unicode – ‘MyString’, u‘MyString’
• List – [ 69, 6.9, ‘mystring’, True]
• Tuple – (69, 6.9, ‘mystring’, True) immutable
• Set/frozenset – set([69, 6.9, ‘str’, True])
frozenset([69, 6.9, ‘str’, True]) –no duplicates & unordered
• Dictionary or hash – {‘key 1’: 6.9, ‘key2’: False} - group of key
and value pairs
12/13/2019 CS 331 21
Python types
• Int – 42- may be transparently expanded to long through
438324932L
• Float – 2.171892
• Complex – 4 + 3j
• Bool – True of False
12/13/2019 CS 331 22
What Does “Type” Mean?
• In Python variables, literals, and
constants have a “type” >>> ddd = 1 + 4
>>> print(ddd)
• Python knows the difference between 5
an integer number and a string >>> eee = 'hello ' + 'there'
>>> print(eee)
hello there
• For example “+” means “addition” if
something is a number and
“concatenate” if something is a string
concatenate = put together
Type Matters
• Python knows what “type” >>> eee = 'hello ' + 'there'
>>> eee = eee + 1
everything is
Traceback (most recent call last):
File "<stdin>", line 1, in
• Some operations are <module>TypeError: Can't convert
prohibited 'int' object to str implicitly
>>> type(eee)
• You cannot “add 1” to a string <class'str'>
>>> type('hello')
<class'str'>
• We can ask Python what type >>> type(1)
something is by using the <class'int'>
type() function >>>
Several Types of Numbers
>>> xx = 1
• Numbers have two main types >>> type (xx)
<class 'int'>
- Integers are whole numbers:
>>> temp = 98.6
-14, -2, 0, 1, 100, 401233 >>> type(temp)
<class'float'>
- Floating Point Numbers have
>>> type(1)
decimal parts: -2.5 , 0.0, 98.6, 14.0 <class 'int'>
>>> type(1.0)
• There are other number types - they
<class'float'>
are variations on float and integer
>>>
Type Conversions
>>> print(float(99) + 100)
199.0
• When you put an integer and >>> i = 42
floating point in an >>> type(i)
expression, the integer is <class'int'>
implicitly converted to a float >>> f = float(i)
>>> print(f)
• You can control this with the 42.0
>>> type(f)
built-in functions int() and
<class'float'>
float()
>>>
• For other data types (lists, dictionaries, user-defined types), assignment
works differently.
• These datatypes are “mutable.”
• When we change these data, we do it in place.
• We don’t copy them into a new memory address each time.
• If we type y=x and then modify y, both x and y are changed.
immutable mutable
• This form of assignment might seem strange at first, but it can prove
remarkably useful (e.g., for swapping values)
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
>>> y = x
>>> x X CANNOT be done with
two simple assignments
3
>>> 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
>>>
Arithmetic Operators
Relational Operators
Logical Operators
Identity Operators
Membership Operators
Bitwise Operators
Order of Evaluation
• When we string operators together - Python must know which one
to do first
x = 1 + 2 * 3 - 4 / 5 ** 6
Used Functions/Operators Today