You are on page 1of 42

INTRODUCTORY PYTHON

PROGRAMMING

LEARN BASICS OF PYTHON


YOU WILL LEARN WHAT?

Basic Syntax of Python

Print Formatting

Conditional Statement in Python

Looping Statement in Python

List, Tuples and Dictionaries

Functions in Python
Python Pros

No 1 Programming Language

We can use python for developing desktop GUI applications and
Web Applications

Readable and maintainable code

Multiple Programming paradigms

Compatible with major platfoms and systems

May Open Source Framework and Tools


What is 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.
History of Python

Python was introduced by Guido Van Rossum in 1989.

In the early 1980s, Van Rossum used to work at CWI (Centrum voor
Wiskunde en Informatica) as an implementer of the programming language
called ABC.

In the late 1980s, while working on a new distributed operating system
called AMOEBA there are things that ABC could not make

So Van Rossum himself started designing a new simple scripting language
that could overcome the flaws of ABC which he called PYTHON

Back in the 1970s, there was a popular BBC comedy tv show called Monty
Python’s Fly Circus and Van Rossum happened to be the big fan of that
show.
Python Programming Timeline
Debugging

• is trying to find why your code doesn’t behave what you want it to.
Basic Debugging

1. Print Debugging – form of debugging where print statements


are added throughout the code to check how the program is
flowing.

2. Scope Debugging – form of debugging where print statements


are added to check the status of the variables in the program at
different stages to see how they are changing.

3. Rubber Duck Debugging - form of debugging where


the programmer explains the logic, goals, and operations to an
inanimate listener to methodically step through the code
Advanced Debugging Methods

1. Step-by-Step Execution - Some development environments


will allow you to run your code one line at a time. You can
watch the lines execute, allowing you to visually see the status
of the programs execution.

2. Variable visualization - Some development environments


will show you a simple chart of all the data stored in memory;
this removes the need for you to print out variables manually
and check their values. Combined with step- by-step execution,
this would allow you to watch how data is changed as the
program runs, removing the need to print out the sum several
times in the scope debugging example.
Advanced Debugging Methods

3. In-line debugging - Some development environments are


sophisticated enough to debug simple errors while you’re
actually writing the code. PyCharm. for example. can underline
your code live as you’re writing it to call out certain error.
Types of Error in Python

1. NameError – occurs when you use a variable name that doesn’t


exist.

2. TypeError – is one type of our error when we’re trying to do


something that doesn’t make sense.
Types of Errors in Python

3. AttributeError – is the result when we try to do something in


our Code that doesn’t make sense. The difference between
TypeError and AttributeError can be a little technical.

4. Syntax Error – the last common type of error well cover for
now. Syntax error is kind of catch all error. It refers to lots of
different things that can be done wrong, all based on violating
Python’s internal grammar.
Variables in Python

 Variables are containers of values. Refer to a value stored in


memory and are created when first assigned

 you can assign value to the variables using assignment operator ‘=’ .

 variables can be reassigned at any time

 variable type is not specified

 types can be changed with a reassignment


Rule/Syntax in Naming Variables

 Must begin with a letter (a - z, A - B) or underscore _

 Other characters can be letters, numbers or _

 names can not contain any of these symbols: :'",<>/?|\!@#%^&*~-+\

 Are case sensitive: capitalization counts!

 names can not contain spaces, use _ instead

 avoid using Python built-in keywords like list and str

 Can be any reasonable length


Coding Standard of Some IT Industries

 Use noun or noun phrase in naming variable

 Use abbreviation sparingly

 Use Camel Case

 Make your identifier related to what value it has


Assigning values can be done like this

Simple Type
x=1

Assignment can be done en masse:


x=y=z=1

Multiple assignments can be done on one line:


x, y, z = 1, 2.39, 'cat'
Basic Data Types in Python

 Numbers: Integers and floating point (64-bit)

 Strings, using double or single quotes: "cat" 'dog‘

 Boolean: True and False

 Lists, dictionaries, and tuples

 These hold collections of variables


 Specialty types: files, network connections, objects etc.
How to Check the Data Type?

 A built-in function, type(), returns the type of the data assigned


to a variable. It’s unusual to need to use this in a program, but
it’s available if you need it!
Operators

 operator is a character that represents an action or process, as for


example * is an arithmetic operator that represent multiplication

 Python supports a wide variety of operators which act like functions,


i.e. they do something and return a value:
Arithmetic Operators

 Operators used to make basic mathematical computations


Comparison Operators

 Used to evaluate a comparison between two Boolean expressions

Operator Function
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Logical Operators (not,or,and)

 Used to combine to Boolean Expression and Evaluate the result


depending on what operator was declared

 It also reverse the output of Boolean Expressions into its opposite value
Logical not (not)

 not(5 == 5) # evaluates to false because the expression is true.

 not(6 <= 4) # evaluates to true because (6 <= 4) would be false.

 not(True) # evaluates to false


# !false evaluates to true.
Logical or (or)

a b a or b

true true true

true false true

false true true

false false false


Logical and (and)

a b a and b

true true true

true false false

false true false

false false false


Variable Modifying Operators
Comments in Python

 # is the Python comment character. On any line everything after


the # character is ignored by Python.

 IT IS VERY IMPORTANT TO USE COMMENT FOR


REFERRENCE
Operator Precedence

 Refers to the worth or arrangement of execution for different


operators
Exercises
Exercises
Basic Python User Input Statement

 input() – use to accept input() from the user. With one


parameter.
Ex for str:
name = input(“Enter your name:”)
print(name)
Ex for int:
age = int(input(“Enter your age:”))
print(age)
Ex for float:
grade= float(input(“Enter your grade in Programming:”))
print(grade)
Print Formatting

 We can use modulo % to inject strings into your print statements.


Note:
%s – str
%r – string representation
%d – int
%f – float
> padding and precision ex: 1.1f
Print Formatting

 Formatting with the .format() method.

Ex. Simple Formatting


age = int(input(“Enter your age:”))
print(“Your age is {}”.format(age))

Ex. Multiple Formatting


name= int(input(“Enter your name:”))
age = int(input(“Enter your age:”))
print(“Mr/Mrs {0}, Your age is {1}”.format(name,age))
Print Formatting

 Formatting with the .format() method.

Ex. Multiple Formatting


name= int(input(“Enter your name:”))
age = int(input(“Enter your age:”))
print(“Mr/Mrs {0}, Your age is {1}”.format(name,age))

Ex. Multiple Formatting


name= int(input(“Enter your name:”))
age = int(input(“Enter your age:”))
print(“Mr/Mrs {a}, Your age is {b}”.format(a=name, b=age))
String Functions and Built in Methods

What are String Functions AND Built in Methods?

 this are functions and methods for Strings to make something on


it. It can find the index and count the number of character in a
single String.

Sample String Function


 len(str) – use to find length of string
mystr = “Hello”
len(mystr )
Sample Built in Methods

 str.upper() – use to convert all the string character into upper


case
mystr = “Hello”
mystr.upper()

 str.lower() – use to convert all the string character into lower case
mystr = “Hello”
mystr.lower()

 str.split() – use to separate each word.


mystr = “Hello World”
mystr.split()
Sample Built in Methods

 str.title() – it makes the first character in upper case


mystr = “Hello”
mystr.title()

 str. endswith() – it checks the last character if equal to the given


string parameter
mystr = “Hello”
mystr.endswith(“o”)
String Operators

 + concatenates String
Ex.
lastName = ‘Salavacion’
firstName = ‘Kaye Harris’
fullName = firstName + “ ” + lastName
print(fullName)
 += appends Strings
Ex.
name= ‘Kaye Harris’
name += “ Salavacion”
print(name)
 * duplicate number of character
Ex.
message= “Ha”
print(message * 8)
String Indexing

 We know strings are a sequence, which means Python can use


indexes to call parts of the sequence.

 In Python, we use brackets [] after an object to call its index

mystr = “Hello”
s[0]
#output H
s[1]
#output e
s[2]
#output l
String Slicing
String Slicing
String Slicing

You might also like