0% found this document useful (0 votes)
4 views10 pages

01 - Python

This document serves as an introduction to Python programming, covering basic concepts such as data types, variables, and operators. It includes examples of simple Python programs and explains the importance of Python in various computing environments. Additionally, it discusses the mutability of data types and the significance of variable naming and assignment in Python.

Uploaded by

amnouyporn-pra67
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

01 - Python

This document serves as an introduction to Python programming, covering basic concepts such as data types, variables, and operators. It includes examples of simple Python programs and explains the importance of Python in various computing environments. Additionally, it discusses the mutability of data types and the significance of variable naming and assignment in Python.

Uploaded by

amnouyporn-pra67
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

01 - Introduction

Introduction
data types, variables, and operators

Learning Outcomes
By the end of this lecture, students will be able to:
Explain basics understanding of computer programming.
Identify fundamental programming concept in Python.
Write computer instructions in Python.

A Taste of Py
Only ugly languages become popular. Python is the one exception. —Don Knuth

Little Programs - [Link]


1 for countdown in 5, 4, 3, 2, 1, "hey!":
2 print(countdown)

5
4
3
2
1
hey!

Special words and symbols—for, in, print, commas, colons, parentheses, and so on—that
are important parts of the language’s syntax (rules).

Little Programs - [Link]


1 spells = [
2 "Riddikulus!",
3 "Wingardium Leviosa!",
4 "Avada Kedavra!",
5 "Expecto Patronum!",
6 "Nox!",
7 "Lumos!",
8 ]
9 print(spells[3])

Expecto Patronum!

The individual spells are Python strings (sequences of text characters,


enclosed in quotes).
They’re separated by commas and enclosed in a Python
list that’s defined by enclosing square brackets ([ and ]).
The first value is at offset 0, and the fourth value is at offset 3.

Little Programs - [Link]


1 quotes = {
2 "Moe": "A wise guy, huh?",
3 "Larry": "Ow!",
4 "Curly": "Nyuk nyuk!",
5 }
6 stooge = "Curly"
7 print(stooge, "says:", quotes[stooge])

Curly says: Nyuk nyuk!

quotes is a variable that names a Python dictionary—a collection of unique keys and
associated values.
Using a dictionary, you can store and look up things by name, which is often a useful
alternative to a list.

Little Programs (cont.)


The spells example used square brackets ( [ and ] ) to make a Python list.
The quotes example uses curly brackets ( { and } , to make a Python dictionary.
A colon ( : ) is used to associate each key in the dictionary with its value.
Python in the Real World
So, is learning Python worth the time and effort?
Python has been around since 1991 (older than Java, younger than C), and is
consistently in the top five most popular computing languages.
People are paid to write Python programs—serious stuff that you use every day, such
as Google, YouTube, Instagram, Netflix, and Hulu.
Python has a reputation for productivity that appeals to fast moving organizations.

Python in the Real World (cont.)


You’ll find Python in many computing environments, including these:
The command line in a monitor or terminal window
Graphical user interfaces (GUIs), including the web
The web, on the client and server sides
Backend servers supporting large popular sites
The cloud (servers managed by third parties)
Mobile devices
Embedded devices
Python programs range from one-off scripts—such as those you’ve seen so far in this
chapter—to million-line systems.

Why Python?
One reason, not necessarily the most important, is popularity.
More recently, it’s become extremely popular in the data science and machine learning
worlds.
Compared with other popular languages, Python has a gentle learning curve that
makes you productive sooner, yet it has depths that you can explore as you gain
expertise.

Why Not Python?


Python isn’t the best language for every situation.
It is not installed everywhere by default.
It’s fast enough for most applications, but it might not be fast enough for some of the
more demanding ones.
A program written in C, C++, C#, Java, Rust, or Go will generally run faster than its
Python equivalent. But not always!

Your Moment of Zen


Each computing language has its own style.
There is often a Pythonic way to express yourself
Embedded in Python is a bit of free verse that expresses the Python philosophy.
Just type the following code:

1 import this

Data types
Under the hood, everything in your computer is just a sequence of bits.
One of the insights of computing is that we can interpret those bits any way we want—
as data of various sizes and types.
We begin with Python’s data types and the values that they can contain.
Then we see how to represent data as literal values and variables.

Basic Python Types


Name Type Mutable? Example
Boolean bool no True, False
Integer int no 45, 25000, 25_000
Float float no 3.14, 2.7e5
Text string str no 'alas', "alack", '''a verse attack''''
List list yes ['Winken', 'Blinken', 'Nod']
Tuple tuple yes (2, 4, 8)
Set set yes set([3, 5, 7])
Dictionary dict yes {'game': 'bingo', 'dog': 'dingo', 'drummer': 'Ringo'}
Mutability
The type also determines whether the data value contained by the box can be changed
(mutable) or is constant (immutable).
Think of an immutable object as a sealed box, but with clear sides, you can see the
value but you can’t change it.
A mutable object is like a box with a lid: not only can you see the value inside, you can
also change it; however, you can’t change its type

Variables
Now, we’ve arrived at a key concept in computing languages.
Python, like most computer languages, lets you define variables—names for values in
your computer’s memory that you want to use in a program.
Python variable names have some rules:
They can contain only these characters:
Lowercase letters ( a through z )
Uppercase letters ( A through Z )
Digits ( 0 through 9 )
Underscore ( _ )
They are case-sensitive: thing, Thing, and THING are different names.
They must begin with a letter or an underscore, not a digit.
Names that begin with an underscore are treated specially.
They cannot be one of Python’s reserved words (also known as keywords).
Within a Python program, you can find the reserved words with

1 help("keywords")
2 # or
3 import keyword
4 [Link]

Assignment
In Python, you use = to assign a value to a variable.
We all learned in grade school arithmetic that = means equal to.
So why do many computer languages, including Python, use = for assignment?
One reason is that standard keyboards lack logical alternatives such as a left
arrow key, and = didn’t seem too confusing.
Also, in computer programs you use assignment much more than you test for
equality
Programs are not like algebra. When you learned math in school, you saw equations
like this:

1 y = x + 12

You would solve the equation by “plugging in” a value for x.


If you gave x the value 5 , 5 + 12 is 17 , so the value of y would be 17.
Plug in 6 for x to get 18 for y , and so on.

Assignment (cont.)
Computer program lines may look like equations, but their meaning is different.
In Python and other computer languages, x and y are variables.
Python knows that a bare sequence of digits like 12 or 5 is a literal integer.

1 x = 5
2 y = x + 12
3 y

17

Here’s the big difference between math and programs: in math, = means equality of
both sides, but in programs it means assignment: assign the value on the right side to
the variable on the left side.
Also in programs, everything on the right side needs to have a value (this is called
being initialized).
The right side can be a literal value, or a variable that has already been assigned a
value, or a combination.

Variables Are Names, Not Places


Python variables are just names.

1 a = 7

Assignment does not copy a value; it just attaches a name to the object that contains
the data.
The name is a reference to a thing rather than the thing itself.
Visualize a name as a tag with a string attached to the object box somewhere else in
the computer’s memory.
Names point to objects (variable a points to an integer object with value 7)

int 7

Assigning to Multiple Names


Assigning to Multiple Names

1 two = deux = zwei = 2


2 print(two)
3 print(deux)
4 print(zwei)

222

Reassigning a Name
Because names point to objects, changing the value assigned to a name just makes
the name point to a new object.
The reference count of the old object is decremented, and the new one’s is
incremented.
Copying
If the object is immutable (like an integer), its value can’t be changed, so both names
are essentially read-only. Try this:

1 x = 5
2 y = x
3 print(x)
4 print(y)
5 x = 29
6 print(x)
7 print(y)

5 5 29 5

Copying (cont.)
When we assigned x to y , that made the name y point to the integer object with value 5
that x was also pointing to.
Changing x made it point to a new integer object with value 29. It did not change the
one containing 5, which y still points to.
But if both names point to a mutable object, you can change the object’s value via
either name, and you’ll see the changed value when you use either name. If you didn’t
know this first, it could surprise you.

Copying (cont.)
A list is a mutable array of values.
For this example, a and b each point to a list with three integer members:

1 a = [2, 4, 6]
2 b = a
3 print(a)
4 print(b)

[2, 4, 6]
[2, 4, 6]

Copying (cont.)
These list members ( a[0] , a[1] , and a[2] ) are themselves like names, pointing to
integer objects with the values 2 , 4 , and 6.
The list object keeps its members in order.
Now change the first list element, through the name a , and see that b also changed:

1 a[0] = 99
2 print(a)
3 print(b)

[99, 4, 6]
[99, 4, 6]

When the first list element is changed, it no longer points to the object with value 2 , but
a new object with value 99.
The list is still of type list , but its value (the list elements and their order) is mutable.

Choose Good Variable Names


He said true things, but called them by wrong names. —Elizabeth Barrett Browning

Operators - Number
Operator Description Example Result
+ Addition 5+8 13
- Subtraction 90 - 10 80
* Multiplication 4*7 28
/ Floating-point division 7/2 3.5
// Integer (truncating) division 7 // 2 3
% Modulus (remainder) 7%3 1
** Exponentiation 3 ** 4 81

Operators - Comparison
Operator Description
equality ==
inequality !=
Operator Description
less than <
less than or equal <=
greater than >
greater than or equal >=

References
Lubanovic, B. (2019). Introducing Python: Modern Computing in Simple Packages.
O’Reilly Media.

You might also like