lbobgdt-03-Intro Programming python
lbobgdt-03-Intro Programming python
BIGDATA
Big Data Techniques
and Technologies
Bobby Reyes
Introduction to Programming
with Python
Scalar Objects (int, float, complex, bool, string, NoneType)
Operators and Precedence of Operators
Variables, Assignment statement
print() statement
input() statement
1
1/14/2025
What is Python?
◼ Python…
◼ …is a general purpose interpreted programming language.
◼ …is a language that supports multiple approaches to software
design, principally structured and object-oriented
programming.
◼ …provides automatic memory management and garbage
collection
◼ …is extensible
◼ …is dynamically typed.
Some History
◼ “Over six years ago, in December 1989, I was
looking for a "hobby" programming project that
would keep me occupied during the week around
Christmas… I chose Python as a working title for
the project, being in a slightly irreverent mood
(and a big fan of Monty Python's Flying Circus).”
–Python creator Guido Van Rossum, from the foreword
to Programming Python (1st ed.)
◼ Goals:
◼ An easy and intuitive language just as powerful as major
competitors
◼ Open source, so anyone can contribute to its development
◼ Code that is as understandable as plain English
◼ Suitability for everyday tasks, allowing for short
development times
2
1/14/2025
Programming Basics
◼ code or source code: The sequence of instructions in a program.
Python Programs
3
1/14/2025
4
1/14/2025
Hello, World!
◼ Install Anaconda3 and launch JupyterLab. In JupyterLab,
open a Console and enter the following code:
print("Hello World!")
5
1/14/2025
Hello World!
◼ In a Jupyter Notebook:
print("Hello World!") ← in Code cell
Hello World! ← Output
Google Colab
◼ Can also work with a hosted Jupyter Notebook service,
e.g. colab.google.com
6
1/14/2025
Objects
OBJECT
▪ programs manipulate data objects
S
▪ objects have a type that defines the kinds of things
programs can do to them
◦ Ana is a human so she can walk, speak English, etc.
◦ Chewbacca is a wookie so he can walk, “mwaaarhrhh”, etc.
▪ objects are
◦ scalar (cannot be subdivided)
◦ non-scalar (have internal structure that can be accessed)
Scalar Objects
SCALAR
▪ int – represent integers, ex. 5
OBJECTS
▪ float – represent real numbers, ex. 3.27
▪ complex – represent complex numbers, ex. 3.27 + 2j
▪ bool – represent Boolean values True and False
▪ str – represent String, a sequence of characters
enclosed in “” or ‘’, ex. “This is it!”
▪ NoneType – special and has one value, None
▪ can use type()function to see the type of an object
>>> type(5)
int
>>> type(3.0)
float
7
1/14/2025
Numbers: Integers
◼ Integer – represents
whole numbers,
positive or negative, >>> 132224
without decimals, of 132224
unlimited length >>> 132323 ** 4
306578259430545516241
>>>
8
1/14/2025
Numbers: Complex
◼ Built into Python
◼ A complex number comprises a
real part and an imaginary part
◼ Denoted as a + bj, where
a is the real part, >>> x = 3 + 2j
b is the imaginary part, and >>> y = -1j
>>> x + y
j is the imaginary unit (3+1j)
◼ Same operations are supported >>> x * y (2-3j)
>>> z = complex(2,5)
as integer and float >>> z
◼ Use complex() function to (2+5j)
create a complex data type or
convert / cast into complex data
type
String Literals
◼ think of as a sequence of case sensitive characters
◼ Can use single or double quotes, and
three quotes for a multi-line string
◼ can compare strings with ==, >, < etc.
9
1/14/2025
Printing to Console
In [11]: 3+2
Out[11]: 5
PRINTING TO CONSOLE
In [12]: print(3+2)
5
10
1/14/2025
Expressions
11
1/14/2025
Precedence of Operators
▪ precedence: Order in which operations are evaluated
▪ PEMDAS rule:
▪ Parentheses used to tell Python to do these operations first
** Exponentiation has a higher precedence
* / // % Multiplication / Division
Addition / Subtraction
SIMPLE OPERATIONS
+ -
◦ Thus 1 + 3 * 4 is 13
12
1/14/2025
String Concatenation
◼ + is overloaded to do
concatenation
>>> x = 'hello'
>>> x = x + ' there'
>>> x
'hello there'
>>> s = '012345'
>>> s[3] •len(String) – returns the length
'3' (number of characters) in the String
>>> s[1:4]
'123' •str(Object) – returns a String
>>> s[2:] representation of the Object
'2345'
>>> x='string'; len(x)
>>> s[:4]
6
'0123'
>>> s[-2] >>> str(10.3)
'4' '10.3'
13
1/14/2025
String Manipulation
Math Commands
◼ Python has useful commands (or called functions) for performing
calculations.
Command name Description Constant Description
abs(value) absolute value e 2.7182818...
ceil(value) rounds up pi 3.1415926...
cos(value) cosine, in radians
floor(value) rounds down
log(value) logarithm, base e
• Note: python is
log10(value) logarithm, base 10 case-sensitive
max(value1, value2) larger of two values • abs is different from
min(value1, value2) smaller of two values Abs, ABS
round(value) nearest whole number
sin(value) sine, in radians
sqrt(value) square root
14
1/14/2025
Variables
◼ variable: A named piece of memory that can store a value.
◼ Usage:
◼ Compute an expression's result,
Abstracting Expressions
▪ why give names to values of expressions?
▪ to reuse names instead of values
▪ easier to change code later
ABSTRACTING
pi = 3.14159
EXPRESSIONS
radius = 2.2
area = pi*(radius**2)
15
1/14/2025
Variables
◼ assignment statement*: Stores a value into a variable.
* binds name to value
◼ Syntax:
name = value
◼ Examples: In memory:
x = 5 x 5
gpa = 3.14
gpa 3.14
Programming vs Math
▪ in programming, you do not “solve for x”
In memory:
pi 3.14159
CHANGING BINDINGS
pi = 3.14159
radius 2.2
16
1/14/2025
Changing Bindings
▪ can re-bind variable names using new assignment
statements
▪ previous value may still be stored in memory but lost
the handle for it
CHANGING BINDINGS
▪ value for area does not change until you tell the
computer to do the calculation again
3.14
pi = 3.14159 pi
2.2
radius = 2.2 radius
area = pi*(radius**2) area 3.2
>>> print(x,y)
10 20
>>> temp = x
>>> x = y
>>> y = temp
>>> print(x,y)
20 10
17
1/14/2025
Strings
▪ strings are “immutable” – cannot be modified
s = "hello"
s[0] = 'y' → gives an error
s = 'y'+s[1:len(s)] → is allowed,
s bound to new object
"hello"
"yello"
print
◼ print : Produces text output on the console.
◼ Syntax:
print("Message")
print(Expression)
➢ Prints the given text message or expression value on the console
and moves the cursor down to the next line.
print(Item1, Item2, ..., ItemN)
➢ Prints several messages and/or expressions on the same line.
◼ Examples:
print("Hello, world!")
age = 45
print("You have", 65 – age, "years until retirement")
Output:
Hello, world!
You have 20 years until retirement
18
1/14/2025
• The end parameter can be used to append any string at the end
of the output of the print statement in python.
String Formatting
◼ Similar to C’s printf
<formatted string> % <elements to insert>
◼ Can usually just use %s for everything,
it will convert the object to its String representation.
19
1/14/2025
input
◼ input : Takes input from the user.
◼ You can assign (store) the result of input into a variable.
◼ Input read is returned as a string; typecast / convert input string to
correct type before using in arithmetic operations
◼ Example:
age = int(input("How old are you? "))
print("Your age is", age)
print("You have", 65–age, "years until retirement")
Output:
How old are you? 53
Your age is 53
You have 12 years until retirement
◼ Exercise: Write a Python program that prompts the user for his/her
amount of money, then reports how many Nintendo Wiis the person
can afford, and how much more money he/she will need to afford an
additional Wii.
input: Example
input.py
print("What's your name?")
name = input("> ")
print("Hi ", name, "!", "You are ", 2024 –birthyear, “y.o.”)
% python input.py
What's your name?
> Michael
What year were you born?
>1980
Hi Michael! You are 44 y.o.
20
1/14/2025
21