Week 1 Introduction To Python
Week 1 Introduction To Python
Sheikh Abujar
sheikh[dot]cse[at]diu.edu.bd
cybermanbd[at]gmail.com
In Class !
Why Program?
Real Problems !
Real Problems Cont.
• Programmers have some tools that allow them to build new tools
Computer
Programmer
Hardware + Software
Main
Memory
Generic
Software What
Next? Computer
Input Central
and Output Processing
Devices Unit
Secondary
if x< 3: print Memory
Main
Memory
Generic
Software What
Next? Computer
Input Central
and Output Processing
Devices Unit
01001001 Secondary
00111001 Memory
Main
Memory
Machine
Language
Python as a Language
Python is the language of the Python
Interpreter and those who can converse with
it. An individual who can speak Python is
known as a Pythonista. It is a very uncommon
skill, and may be hereditary. Nearly all known
Pythonistas use software initially developed by
Guido van Rossum.
Early Learner: Syntax Errors
• We need to learn the Python language so we can communicate our
instructions to Python. In the beginning we will make lots of mistakes and
speak gibberish like small children.
• When you make a mistake, the computer does not think you are “cute”. It
says “syntax error” - given that it knows the language and you are just
learning it. It seems like Python is cruel and unfeeling.
• You must remember that you are intelligent and can learn. The computer is
simple and very fast, but cannot learn. So it is easier for you to learn Python
than for the computer to learn English...
Talking to Python
csev$ python3
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType
"help", "copyright", "credits" or "license" for more information.
>>>
What
next?
csev$ python3
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType
"help", "copyright", "credits" or "license" for more information.
>>> x = 1
>>> print(x)
1
>>> x = x + 1 This is a good test to make sure that you have
>>> print(x) Python correctly installed. Note that quit() also
2 works to end the interactive session.
>>> exit()
What Do We Say?
Elements of Python
• Vocabulary / Words - Variables and Reserved words (Chapter 2)
• Most programs are much longer, so we type them into a file and tell
Python to run the commands in the file.
• Interactive
• Script
print('Smaller') Program:
No Output:
x=5
Yes if x < 10: Smaller
x > 20 ? print('Smaller') Finis
if x > 20:
print('Bigger') print('Bigger')
No
print('Finis')
print('Finis')
n=5 Repeated Steps
No Yes Output:
n>0? Program:
5
print(n) n=5 4
while n > 0 :
3
print(n)
n = n -1 n=n–1 2
print('Blastoff!') 1
Blastoff!
Loops (repeated steps) have iteration variables that
print('Blastoff')
change each time through a loop.
name = input('Enter file:')
handle = open(name, 'r') Sequential
Repeated
counts = dict()
for line in handle: Conditional
words = line.split()
for word in words:
counts[word] = counts.get(word,0) + 1
bigcount = None
bigword = None
for word,count in counts.items():
if bigcount is None or count > bigcount:
bigword = word
bigcount = count
print(bigword, bigcount)
name = input('Enter file:') A short Python “Story”
handle = open(name, 'r') about how to count
counts = dict()
words in a file
for line in handle:
words = line.split() A word used to read
for word in words: data from a user
counts[word] = counts.get(word,0) + 1
Example
x = 40 # x is of type int
x = 50
x = ”shifat” # x is now of type str
y = “Bangladesh"
print(x)
print(x)
Print(type(x))
print(y)
Built-in Data Types
Numeric int, float, complex
Types:
Sequence list, tuple, range
Types:
Mapping dict
Type:
Set Types: set, frozenset
Boolean bool
Type:
Binary bytes, bytearray, memoryview
Types:
Arithmetic operators
OPERATOR DESCRIPTION SYNTAX
In [3]: print('Welcome\nto\n\nPython!')
Welcome
To
Python!
Other Escape Sequences
In [1]: a , b = input().split()
5 10
In [2]: Print(a , b)
5 10
In [3]: Print(type(a))
<class ‘str’>
Objects and Dynamic Typing
• In [1]: type(7)
• Out[1]: int
• In [2]: type(4.1)
• Out[2]: float
• In [3]: type('dog')
• Out[3]: str
Values such as 7 (an integer), 4.1 (a floating-point number) and 'dog' are all objects. Every object has a type and a
value. An object’s value is the data stored in the object. The snippets above show objects of Python built-in types
int (for integers), float (for floating-point numbers) and str (for strings).
Variables Refer to Objects
Assigning an object to a variable binds (associates) that variable’s name to the object. As you’ve seen, you can
then use the variable in your code to access the object’s value:
• In [4]: x = 7
• In [5]: x + 10
• Out[5]: 17
• In [6]: x
• Out[6]: 7
After snippet [4]’s assignment, the variable x refers to the integer object containing 7. As shown in snippet [6],
snippet [5] does not change x’s value. You can change x as follows:
• In [7]: x = x + 10
• In [8]: x
• Out[8]: 17
Dynamic Typing
• Python uses dynamic typing—it determines the type of the object a variable refers to while executing your
code. We can show this by rebinding the variable x to different objects and checking their types:
• In [9]: type(x)
• Out[9]: int
• In [10]: x = 4.1
• In [11]: type(x)
• Out[11]: float
• In [12]: x = 'dog'
• In [13]: type(x)
• Out[13]: str
Getting Your Questions Answered
Online forums enable you to interact with other Python programmers and get your Python questions answered.
Popular Python and general programming forums include:
• python-forum.io
• StackOverflow.com
• https://www.dreamincode.net/forums/forum/29-python/
Thank You