introduction to programming with python-1

26
Introduction to Programming with Python – Class 1 SYED FARJAD ZIA ZAIDI

Upload: syed-farjad-zia-zaidi

Post on 09-Dec-2014

122 views

Category:

Software


3 download

DESCRIPTION

Slides for Lecture 1 of the course: Introduction to Programming with Python offered at ICCBS. It covers the following topics: 1.) Variables, Statements and Expressions 2.) Functions 3.) Flow Control

TRANSCRIPT

Page 1: Introduction To Programming with Python-1

Introduction to Programming with Python – Class 1SYED FARJAD ZIA ZAIDI

Page 2: Introduction To Programming with Python-1

Class Objectives

Class Objective

Review variables, statements, expressions, functions, algorithms and technical terms.

Understanding Flow Control.

Write a simple program that performs addition, subtraction, multiplication and division using Functions.

Page 3: Introduction To Programming with Python-1

Class Material

• Chapter 1, 2, 3 - Python for Informatics: Exploring Information

Reading

• Rock, Paper, ScissorsAssignment

Page 4: Introduction To Programming with Python-1

Variables

Definition:

A named piece of memory that can store a value.

Statement:

A statement written in a programming language stores a value in a variable.

Example:

X = 5

Y = 1

Expression:

A variable that has a value can be used in an expression.

Example:

X = X + 4

Y = X + 1

Page 5: Introduction To Programming with Python-1

Rules for defining a Variable:

Names must start with a letter or _. Names must contain only letters, digits, and _.

Page 6: Introduction To Programming with Python-1

Questions?

Page 7: Introduction To Programming with Python-1

Functions

Definition:

A function is a block of organized, reusable code that is used to perform some action. 

Defining a function:

You can define a function in Python using the Keyword ‘def’.

General Form of Function:

def function_name(parameters):

body

Example:

def MyFirstFunction():

print “This is my first function in Python Programming Language”

Page 8: Introduction To Programming with Python-1

Defining Functions

You can define functions to provide the required functionality. Here are simple rules to define a function in Python.

Function blocks begin with the keyword ‘def’ followed by the function name and parentheses ‘( )’ .

Example:

def MyFirstFunction():

Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.

Example:

def MySecondFunction(passedInput):

Page 9: Introduction To Programming with Python-1

Defining Functions

The first statement of a function can be an optional statement - the documentation string of the function or docstring.

Example:

def MyFirstFunction():

“””This is the 'docstring' which defines

the function for other programmers to

understand easily”””

The code block within every function starts with a colon (:) and is indented.

Page 10: Introduction To Programming with Python-1

Defining Functions

The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.

General Form of Return Statement:

return [expression]

Example:

def MyFunction():

“”“A function that returns a message”””

return “This is a function that returns a message”

Page 11: Introduction To Programming with Python-1

Calling a function

Function calls are expressions and the result can be stored in a variable. The general form of a function call:

function_name(arguments)

Example:

MyFirstFunction()

MySecondFunction(“Input Parameter”)

Page 12: Introduction To Programming with Python-1

Questions?

Page 13: Introduction To Programming with Python-1

Source Code vs Object Code

Source Code Object Code

Source Code is a text file version of a computer program or software that contains instructions that the computer follows to do something

Object code, or sometimes an object module, is what a computer compiler produces

Source code is written in a programming language which a human can read and change

In a general sense object code is a sequence of statements or instructions in a computer language, usually a machine code language (i.e., 1's and 0's)

Most source code is compiled when it is finished

Object code is the resulting code after compiling source code

Page 14: Introduction To Programming with Python-1

Compiler

A compiler is a computer program (or set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code). The most common reason for wanting to transform source code is to create an executable program.

Page 15: Introduction To Programming with Python-1

Questions?

Page 16: Introduction To Programming with Python-1

Flow Control of the Program

Selection (If/Else) Repetition (Loops)

Page 17: Introduction To Programming with Python-1

Conditional Execution In order to write useful programs, we almost always need to

check some conditions and change the program accordingly. Conditional Statements gives us this ability. The simplest form is if statement.

General Form:

if [expression1]:

body1

elif [expression2]:

body2

else:

bodyN

Page 18: Introduction To Programming with Python-1

Python Comparison OperatorsOperator

Description

== Checks if the value of two operands are equal or not, if yes then condition becomes true.

!= Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.

< Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.

> Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.

>= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.

<= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

Page 19: Introduction To Programming with Python-1

Python Logical OperatorsOperator

Description

and Called Logical AND operator. If both the operands are true then then condition becomes true.

or Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true.

not Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

Page 20: Introduction To Programming with Python-1

Questions?

Page 21: Introduction To Programming with Python-1

Simple Calculator Definition:

A calculator is a machine which allows people to do math operations more easily. For example, most calculators will add, subtract, multiply, and divide.

Instructions for writing the Program: There should be 4 functions in your program:

Add(number1, number2)

Subtract(number1, number2)

Multiply(number1, number2)

Divide(number1, number2)

The template for the program can be accessed here:

http://www.codeskulptor.org/#user37_44o34rWsnC_18.py

Page 22: Introduction To Programming with Python-1

Questions?

Page 23: Introduction To Programming with Python-1

Rock Paper Scissors – 1st Mini-Project

Rock-paper-scissors is a hand game that is played by two people. The players count to three in unison and simultaneously "throw” one of three hand signals that correspond to rock, paper or scissors. The winner is determined by the rules:

Rock smashes scissors

Scissors cuts paper

Paper covers rock

Page 24: Introduction To Programming with Python-1

Instruction

In our first mini-project, we will build a Python function rps(name) that takes as input the string name, which is one of "rock", "paper", "scissors“. The function then simulates playing a round of rock-paper-scissors by calling a helper function gen_random_num() that generate own random choice from these alternatives and then determining the winner using simple if / else statements.

The mini-project template is here:

http://www.codeskulptor.org/#user37_HJPdXx35jy_0.py

Page 25: Introduction To Programming with Python-1

Example runs

Player chooses rock

Computer chooses scissors

Player wins!

Player chooses paper

Computer chooses scissors

Computer wins!

Player chooses scissors

Computer chooses paper

Player wins!

Page 26: Introduction To Programming with Python-1

Questions?