Your first lines of code: numbers, variables and text
Introduction to Data Science and Machine Learning · Md Mahbub E Noor · University of Barishal
print("Hello, Data Science!")print("hello") does exactly what it says. Beginners get results on day one.
No license to buy. Runs offline in Jupyter, or online in Colab.
Tools like pandas and scikit-learn, which we use to build the spam detector, are Python libraries.
print() displays whatever is inside the brackets"Hello"print() starts a new lineprint("Hello, Data Science!")
print(2 + 3)
print("2 + 3")Hello, Data Science! 5 2 + 3
+ - * / work as you expect** means power: 7 ** 2 is 7 squared/ always gives a decimal: 7 / 2 is 3.5# starts a comment: a note Python ignores# Python as a calculator print(7 + 3) print(7 * 3) print(7 / 2) print(7 ** 2)
10 21 3.5 49
= means "store this", not "is equal to"spam_found, not xmessages_today = 12 spam_found = 3 share = spam_found / messages_today print(share)
0.25
WATCH FOR
Silent video: press play, pause any time.
int: whole numbers like 12float: decimals like 0.25str (string): text like "free prize"bool: True or False, answers to yes/no questionsprint(type(12))
print(type(0.25))
print(type("free prize"))
print(type(True))<class 'int'> <class 'float'> <class 'str'> <class 'bool'>
len() counts characters, spaces included.lower() makes all letters small, so FREE and free matchin asks: does this text appear inside?message = "Claim your FREE prize now"
print(len(message))
print(message.lower())
print("free" in message)
print("free" in message.lower())25 claim your free prize now False True
f before the quotes{ } is calculated and inserted{share:.0%} shows a number as a percentagespam_found = 3
total = 12
print(f"{spam_found} of {total} messages were spam")
share = spam_found / total
print(f"That is {share:.0%} of the inbox")3 of 12 messages were spam That is 25% of the inbox
namemessagename = "..." message = "..." print(f"...") print(...)
Starter code: replace the ... with your own code
Read the last line of an error first. It names the problem.
| What you typed | What Python says | What it means |
|---|---|---|
total = 12 print(totl) | NameError: name 'totl' is not defined | Typo in a name. Python only knows names you created. Check spelling. |
print("hello) | SyntaxError: unterminated string literal (detected at line 1) | A quote or bracket was opened but not closed. Close it. |
print("Total: " + 12) | TypeError: can only concatenate str (not "int") to str | Cannot glue text and a number with +. Use an f-string. |
In the lab you can
Opens in a new tab and works offline. All labs: labs/START_HERE.html
print(x) | Show a value |
# note | Comment, ignored by Python |
name = value | Store a value in a variable |
+ - * / ** | Arithmetic, ** is power |
type(x) | Which kind of value is this? |
len(text) | Number of characters |
text.lower() | All letters to lowercase |
"a" in text | Does the text contain "a"? True/False |
f"{x} items" | Insert values into text |
Due before Lecture 2.3 · [date]
Create a notebook (Jupyter or Colab) named HW2_2_<your ID>. Put your name in a text cell at the top.
Store your name, department and favourite number in variables. Print one f-string sentence using all three.
For each line, write your prediction as a comment first, then run it: print(10 / 4), print(10 // 4), print("ab" * 3), print(len("data science")).
Store a real SMS you received (remove names and numbers). Print its length, its lowercase version, and whether it contains "free".
Create one error deliberately. Keep the error message and explain in one sentence what it means.
Watch it again at home to revise.