Introduction To Programming with Python-1
-
Upload
syed-farjad-zia-zaidi -
Category
Software
-
view
122 -
download
3
description
Transcript of Introduction To Programming with Python-1
Introduction to Programming with Python – Class 1SYED FARJAD ZIA ZAIDI
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.
Class Material
• Chapter 1, 2, 3 - Python for Informatics: Exploring Information
Reading
• Rock, Paper, ScissorsAssignment
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
Rules for defining a Variable:
Names must start with a letter or _. Names must contain only letters, digits, and _.
Questions?
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”
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):
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.
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”
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”)
Questions?
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
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.
Questions?
Flow Control of the Program
Selection (If/Else) Repetition (Loops)
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
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.
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.
Questions?
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
Questions?
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
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
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!
Questions?