Lecture notes on first topic in CSC1015 (Basics of Programming). Combines notes from lectures and recommended textbook (Python Programming: An Introduction to Computer Science (2nd edition) by John Zelle).
Lesson 1: Hello World
Program:
Set of instructions given to computer corresponding to an algorithm to solve a problem.
Programming (writing programs) in a programming language (e.g. Python)
Input Process internally Output (like a mathematical function, diff input => diff output)
Python:
Programs stored in text files.
Program = set of instructions interpreted by python interpreter executed (user command)
Python interpreter: (python3)
o Instructions run one at a time.
o Executes each instruction and prompts user for next, exit() to end
o Useful for testing code and exploring functions/modules
o Note: always save your python files .py
Usually done through Integrated Development Environment (IDE)
o Graphical interface (like word)
Structure:
# what the program does
# author of program
# date
Python Instruction 1
Python Instruction 2
print(“Hello World”) :
print = name of function, “Hello World” = argument/value input into function
Lesson 2: Numbers and Variables
Program Syntax & Style:
Every statement (instruction) on new line
Case-sensitive
# = comments to describe code
o Brief description of code/of each function/explanation of non-obvious parts of code,
author, date
Syntax & Logic errors:
o Syntax error = program doesn’t conform to structure required => program won’t run
o Logic error = program runs but doesn’t work as expected (always test!!)
Escape Sequences: (useful, keep them on hand)
o Special characters that can’t be represented easily:
o \a - bell (beep)
o \b – backspace
o \n – newline
o \t – tab
o \’ – single quote
, o \” – double quote
o \\ - \ character
Numeric Data Types:
- 2 primitive types of numbers:
o Integer = whole number, no fraction
o Floating point number = has fractional part, stores only as APPROXIMATION to real
number, there is a limit to accuracy of stored values (e.g. 10/3)
- Integers:
o Literals
Actual data values written into program.
Numeric literals can be output like text, e.g.:
print(12) 12
print(12+13) 25
o Expressions
+, -, /, *, % (mod)
// (integer division – always goes towards negative infinitiy)
a**b (a to the power of b)
Precedence of operation = BODMAS (1st = (), 2nd = * and /, 3rd = + and -)
Modulus (%)
- aka integer remainder operator
- a % b = remainder of a dividend by b
- e.g. 18 % 5 = 3 (closest multiple of 5 is 15, therefore 18-15 = 3)
- e.g. 5 % 2 = 1 (closest multiple of 2 is 4, therefore 5-4 = 1)
Identifiers:
print = identifier (reserved – has a particular meaning)
Used to name parts of program.
o Start with _ or letter, followed by _, letter, or digit e.g. hello_world
Reserved words = if, for, else (they have special meanings, therefore cannot be used for other
purposes)
Variables:
Sections of memory where data can be stored.
Most variables have identifiers e.g. a_value
Defined by assigning initial value to them e.g. a_value = 10
Assignment and Output (I/O):
LHS is the variable, RHS is the expression e.g. b = a + 5
Output values are like literals (can do both on one line) e.g. print(“The value is “, a)
Lesson 3: Input and Output
Output: print
print = built-in function with precise set of rules for how it works.
Displays value of each expression, separated by blank spaced followed by carriage return.
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller aqeelahismail. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $7.17. You're not tied to anything after your purchase.