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

Python Objects

The document provides an overview of Python programming concepts, focusing on objects, operators, and error types. It explains the structure of Python instructions, the importance of variables and identifiers, and introduces type casting. Exercises are included to engage readers in practical understanding of these concepts.
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 views27 pages

Python Objects

The document provides an overview of Python programming concepts, focusing on objects, operators, and error types. It explains the structure of Python instructions, the importance of variables and identifiers, and introduces type casting. Exercises are included to engage readers in practical understanding of these concepts.
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

Python Objects

CSC A20

Akshay Arun Bapat


with huge thanks to Brian Harrington
Python
● Sequence of instructions
Exercise

● Find your neighbours


● What do you think is the simplest instruction?
Instructions
● Nothing
○ An empty line
○ Like LaTeX
● Comment
○ These are not for the computer
○ These are for us humans
● Literal
○ These are objects
○ An instruction that doesn’t say what to do
■ It just refers to something
○ Multi-line string literal can be used as a comment
Python shell
● Computer is ready to read in Python
● Written one instruction at a time
○ Until it’s complete
■ we will see and understand this later
● What does a computer do
○ Read
○ Evaluate (perform action)
○ Print
Python Objects
● Everything in Python is an object
○ 2
○ 4.0
○ “Akshay”
Operators
● Special instructions
○ Standard arithmetic
■ Add (+)
■ Subtract (-)
■ Multiply (*)
■ Divide (/)
○ Additional arithmetic operators
■ Integer division (//)
■ Remainder after integer division (%)
■ Exponentiation (**)
Operators
● Require objects to act upon
○ Called operands
○ Or arguments
Exercise

Again with your neighbours

● Consider you are making a sandwich


○ You are given instructions to do so
○ And no bread is provided

What will happen?


Errors
● Syntax errors
○ Sentence structure is invalid
● Runtime errors
○ Correct structure
○ Computer starts following the instruction
○ But hits a roadblock
Exercise

Again with your neighbours

Can you think of a runtime error?


Python script
● Create a separate file with instructions
○ Called a script
○ Saved with a .py extension
● Use a special command to tell computer to read these instructions
○ And that they are written in Python
■ Because other languages are possible
● What does a computer do
○ One instruction at a time
■ Read
■ Evaluate
Printing in script
● print() function
○ Argument is printed
Expressions
● An instruction that needs evaluation
● Mixture
○ Objects
○ Operators
Types of Objects
● Basic types
○ Numerical
■ Int
■ Float
■ Complex (not covered in this course)
○ Boolean (bool)
○ String (str)
● None type
Types of Objects
● Complex types (we study this later)
○ Collections
○ User defined
● Binary types
○ Not covered in this course
Binary
● All data in Computers is binary
● 01011001
Binary
How high can you count on your fingers?

1 2 3 4 5
Binary
How high can you count on your fingers?

1 3 7 15 31
Exercise

Again with your neighbours


How does a Computer determine what some sequence of binary
mean?
Say for instance what is 01010001
Is it 81?
Is it Q (ascii code 81)?
Remembering Data
● Computers only do what told
● So, we need to tell them to remember something

● Assignment operator (=)


○ Used to store data
○ Into objects
■ Called variables
● Because we can store any data in them
● So they can vary
Variables
● Have a name
○ That can be used to identify them
○ Hence called identifiers
● Have a memory associated with them
○ Like a house
○ With an address
Exercise

Again with your neighbours

Should all possible identifiers be allowed?


Keywords
● Special words
● Special meaning

Clearly should not be an identifier

● print
● int
● float
● …
Identifiers
● Can have
○ Letters (a-z) (A-Z)
○ Digits (0-9)
○ Underscore (_)
● Must not begin with a digit
● Keywords are not allowed
● Are case sensitive
○ Akshay is different from akshay
Variables
● Should be defined first
○ x=7
● Can be used later
○ x+2

● type(x) will give current type


Type Casting
● Convert objects from one type to the other
○ If compatible
○ What happens if not?
● Implicit casting
○ Python does it for you
● Explicit casting
○ You do it

You might also like