0% found this document useful (0 votes)
8 views

Unit 1

Uploaded by

Ashterite
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)
8 views

Unit 1

Uploaded by

Ashterite
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
You are on page 1/ 45

UNIT 1:

The way of the program,


variables, expressions,
statements
Learning Outcomes
1. Describe why there is a need of the course
2. Differentiate the different parts of a program
3. Implement debugging a program
4. Describe the basic concepts in Python Programming Language
5. Determine the correct usage of data types
6. Differentiate the different types of literals and different ways to
declare a constant
7. Implement the basic ways to accept user input and display an output
8. Differentiate the different types of operators and their application
Think like a computer scientist
Think
Thinklike a:
like...
Mathematician - use formal languages to
denote ideas
Engineer - design, assemble, evaluate
tradeoffs
Scientist - observe behavior, form
hypothesis, test predictions
Problem Solving
- determine the problem
- think of creative solutions
- implement that solution
Problem Solving
- determine the problem
- think of creative solutions
- implement that solution

Design -> Analysis -> Experimentation


Python
- high-level programming language (HLL)
- general-purpose and versatile
software engineering
data science and analysis
web development
- interpreted language
Interpreter and Compiler
- programs that convert HLL into machine code

Interpreter
- translates program code line by line
Compiler
- translates the entire program completely
Interpreter and Compiler
Compiler

source code compiler machine code output

Interpreter

source code interpreter output


Interpreter and Compiler
Compiler (ex. in C programming language)

source code compiler machine code output

gcc -o file_name file_name.c ./file_name


Interpreter (ex. in Python)

source code interpreter output

"python file_name.py"
What is a Program
- a sequence of instructions that specifies
how to perform a task

- telling the computer what to do


Arithmetic Operators
- Python provides us with operators
(special symbols used in computations)

+ (addition)
- (subtraction)
* (multiplication)
/ (division)
** (exponentiation)
Values and Types
value - one of the basic units of data
type - a category of values

Basic Types
int - integers (don't use commas!)
float - floating-point numbers
str - string
Examples
use the type() function to identify the type of the value

Any value surrounded by


single/double quotations is
of type str:
Assignment Statements
- programming languages let you manipulate variables

name that
assignment refers to a
operator value

variable_name = value

- an assignment statement creates


a new variable and gives it a value
Examples

You can reassign But make sure you keep track


a variable..... of their values

only reassign variables


when necessary
Other Assignment Operators

augmented assignment - combines an assignment


statement with an operator
How to name your variables
General Rules
Can be as long as you like
Can use letters, numbers, and underscore
BUT must not start with a number
Lowercase and uppercase are both allowed
Cannot used reserved keywords
How to name your variables
By convention
only use lowercase letters
use underscore to separate multiple words

Good examples:
How to name your variables
make them meaningful

vs

vs
Expressions vs Statements
Expression
a combination of values,
variables, and operators
the interpreter evaluates it
--> finds the value
Expressions vs Statements
Statement
a unit of code that has
an effect
a unit of code that the
Python interpreter can
execute --> does not
produce values
Script Mode
- this is interactive mode

- script mode is when you save your code in a file


(called a script) and call the interpreter
Operators
Assignment operators
Arithmetic operators
Relational operators
Boolean/Logical operators
Bitwise operators
Relational Operators
Boolean/Logical Operators
Bitwise Operators
Order of Operations
- Python follows PEMDAS (+ negation)
String Operations
- cannot perform mathematical operations on
strings, except + and *

Illegal Operation:
String Operations
String Concatenation: String Repetition:
Comments
- used to document your code
- helps other programmers understand your code
- use # to create a single-line comment
- use 3 single quotes (’’’) to create a block comment
Debugging
- process of tracking down programming errors (bugs)

Programmers 80%
of the time
Kinds of Errors
Syntax Error
- syntax is the set of rules that define how
we make valid statements in a language
- syntax errors occur when there is
something wrong with the syntax
indention is required here

else is misspelled as esle


Kinds of Errors
Runtime Error
- occurs during program execution
- also called exceptions
- examples:
division by zero
performing operation on incompatible types
accessing list element that doesn't exist
undefined variable
NameError - raised when a name is not defined

Read more about Exceptions in the documentation:


https://docs.python.org/3/library/exceptions.html
Kinds of Errors
Semantic Error
- executes but the program does not do
what you wanted it to do
- what you wrote is not what you wanted
to write
- the meaning of the program is wrong

syntactically correct but semantically wrong


Why?
convert
variables
into
integers
Summary
Learned data types, operators, and how
to create variables in Python
How to identify different errors and
introduced debugging
Reference
Downey, A. Think Python: How to Think Like a Computer Scientist,
2nd Edition. Green Tea Press, 2015.
Formative Assignment
In less than 400 words, write your feelings, thoughts, difficulties
about learning how to code in Python for the first time and
expectations/what you want to learn from this class.

If you have learned programming before, simply write down your


expectations.

Submit as PDF through LMS.


Filename: Formative_Assignment_1_familyname_firstname.pdf
Next Meeting
Unit 2: Functions

You might also like