Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

Introduction to Computing Science and Programming I
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
16-May-15 Sudden Python Drinking from the Fire Hose.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Chapter 6 Horstmann Programs that make decisions: the IF command.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
Main task -write me a program
Introduction to C Programming
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Python Control of Flow.
Fundamentals of Python: From First Programs Through Data Structures
Python quick start guide
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
4. Python - Basic Operators
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Fundamentals of Python: First Programs
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Python Control Flow statements There are three control flow statements in Python - if, for and while.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
By the end of this session you should be able to...
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Flow of Control Part 1: Selection
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
ITERATION. Iteration Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
Python: Control Structures
loops for loops iterate over a given sequence.
Basic operators - strings
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Topics The if Statement The if-else Statement Comparing Strings
Functions.
Agenda Control Flow Statements Purpose test statement
Topics The if Statement The if-else Statement Comparing Strings
Arithmetic operations, decisions and looping
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
An Introduction to Python
Coding Concepts (Sub- Programs)
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CMSC 202 Java Primer 2.
3 Control Statements:.
CISC101 Reminders All assignments are now posted.
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
Python While Loops.
Just Enough Java 17-May-19.
Class code for pythonroom.com cchsp2cs
LOOP Basics.
Presentation transcript:

Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated. For example: x = 2 print x == 2 # prints out True print x == 3 # prints out False print x < 3 # prints out True

The "and" and "or" boolean operators allow building complex boolean expressions, for example: name = "John" age = 23 if name == "John" and age == 23: print "Your name is John, and you are also 23 years old.“ if name == "John" or name == "Rick": print "Your name is either John or Rick."

The "in" operator could be used to check if a specified object exists within an iterable object container, such as a list: name = "John" if name in ["John", "Rick"]: print "Your name is either John or Rick.“ Python uses indentation to define code blocks, instead of brackets. The standard Python indentation is 4 spaces, although tabs and any other space size will work, as long as it is consistent. Notice that code blocks do not need any termination.

Here is an example for using Python's "if" statement using code blocks: if :.... elif : # else if.... else:.... For example: x = 2 if x == 2: print "x equals two!" else: print "x does not equal to two."

A statement is evaluated as true if one of the following is correct: The "True" boolean variable is given, or calculated using an expression, such as an arithmetic comparison. An object which is not considered "empty" is passed. Here are some examples for objects which are considered as empty: An empty string: "" An empty list: [] The number zero: 0 The false boolean variable: False

Unlike the double equals operator "==", the "is" operator matches not only the values of the variables, but the instances themselves. For example: x = [1,2,3] y = [1,2,3] print x == y # Prints out True print x is y # Prints out False

The "not" operator Using "not" before a boolean expression inverts it: print not False # Prints out True print (not False) == (False) # Prints out False

For loops iterate over a given sequence. Here is an example: primes = [2,3,5,7] for prime in primes: print prime

For loops can iterate over a sequence of numbers using the "range" and "xrange" functions. The difference between range and xrange is that the range function returns a new list with numbers of that specified range, whereas xrange returns an iterator, which is more efficient. (Python 3 uses the range function, which acts like xrange). Note that the xrange function is zero based. # Prints out the numbers 0,1,2,3,4 for x in xrange(5): print x # Prints out 3,4,5 for x in xrange(3,6): print x

While loops repeat as long as a certain boolean condition is met. For example: # Prints out 0,1,2,3,4 count = 0 while count < 5: print count count += 1 # This is the same as count = count + 1

break is used to exit a for loop or a while loop, whereas continue is used to skip the current block, and return to the "for" or "while" statement. A few examples: # Prints out 0,1,2,3,4 count = 0 while True: print count count += 1 if count >= 5: break # Prints out only odd numbers - 1,3,5,7,9 for x in xrange(10): # Check if x is even if x % 2 == 0: continue print x

Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable, reuse it and save some time. Also functions are a key way to define interfaces so programmers can share their code.

As we have seen in previous sessions Python makes use of blocks. A block is a area of code of written in the format of: block_head: the_1st_block_line the_2nd_block_line... Where a block line is more Python code (even another block), and the block head is of the following format: block_keyword block_name(argument1,argument2,...) Block keywords you already know are "if", "for", and "while".

Functions in python are defined using the block keyword "def", followed with the function's name as the block's name. For example: def my_function(): print "Hello From My Function!"

Functions may also receive arguments (variables passed from the caller to the function) For example: def my_function_with_args(username, greeting): print "Hello, %s, From My Function!, I wish you %s"%(username, greeting)

Functions may return a value to the caller, using the keyword- 'return'. For example: def sum_two_numbers(a, b): return a + b

Simply write the function's name followed by (), placing any required arguments within the brackets. For example, lets call the functions written in the previous slides: my_function() #print a simple greeting my_function_with_args("Or Weis", "a great year!") #prints - "Hello, Or Weis, From My Function!, I wish you a great year!“ x = sum_two_numbers(1,2) #after this line x will hold the value 3 !

Objects are an encapsulation of variables and functions into a single entity. Objects get their variables and functions from classes. Classes are essentially a template to create your objects.

A very basic class would look something like this: class MyClass: variable = "blah" def function(self): print "This is a message inside the class.“ We'll explain why you have to include that "self" as a parameter a little bit later. First, to assign the above class(template) to an object you would do the following: myobjectx = MyClass() Now the variable "myobjectx" holds an object of the class "MyClass" that contains the variable and the function defined within the class called "MyClass".

To access the variable inside of the newly created object "MyObject" you would do the following: myobjectx.variable So for instance the code below would output the string "blah": print myobjectx.variable

You can create multiple different objects that are of the same class(have the same variables and functions defined). However, each object contains independent copies of the variables defined in the class. For instance, if we were to define another object with the "MyClass" class and then change the string in the variable above: myobjecty = MyClass() myobjecty.variable = "yackity“ Then print out both values: print myobjectx.variable # This would print "blah". print myobjecty.variable # This would print "yackity".

To access a function inside of an object you use notation similar to accessing a variable: myobjectx.function() The above would print out the message, "This is a message inside the class."

Thank you!