Download presentation
Presentation is loading. Please wait.
1
CSC 131: Introduction to Computer Science
Dr. Curry Guinn
2
Quick Info Dr. Curry Guinn CIS 2025 guinnc@uncw.edu
Office Hours: MTR: 10:00am-11:00m and by appointment Teaching Assistant: Zach Krepps Tuesday, 8:00am-9:30am, 12:45pm-3:45pm Thursday, 8:00am-9:30am Office: GTA office – off of CIS 2004 lab
3
For Next Class, Thursday
Homework 3 is due Thursday, 11:59pm, September 7
4
Today A brief aside as to when computers will have more “brain power” than human beings Chapter 3 Selection Boolean Expressions Iteration – The while loop
5
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons
6
When will computers surpass humans in computational ability?
Notice this isn’t quite asking when computers will be “smarter” or more “intelligent”
7
How the Brain Works Neuron Cell body: soma, contains nucleus
Single long “output” fiber: axon The axon does eventually branch into axon tips Dendrites are small fibers Connecting junction: synapses, connect dendrites
8
Neuron Activation Chemical reactions change the electrical potential of the cell body When the electrical potential reaches a certain threshold, a pulse is sent down the axon: action potential Synapses which increase the potential: Excitatory Synapses which decrease the potential: Inhibitory
9
Learning in the Brain Synaptic connections can become stronger
Neurons can form new connections with other neurons Neuron groups can actually shift within the brain (although to what effect?)
10
Can Digital Computers Ever Be as Smart as Humans?
There is a significant gap between the computational power of the human brain and current desktop computers. A rough estimate tells us that the human brain has 100 million times the computational power of a 2 Ghz Intel computer (How this estimate might be computed is explained later.). The fastest super computers, Tianhe-2, for example, has been clocked at 33 petaflop/s Petaflop = 1 quadrillion = 1015 How does that compare?
11
How much can the brain computer per second?
The human brain consists of approximately 100 billion (1011) neurons Each neuron may be connected to many other neurons. Let’s suppose the average is 10,000 (104). 1,000 trillion connections (1015) = 1 quadrillion It takes a neuron 1/200 second to re-fire So, at most, the brain could compute 200 * 10,000 * 100 billion (or 200 million billion) computations per second 200,000 teraflops or 200 quadrillion computations or 200 petaflops Theoretical maximum
12
Your PC 2 Ghz = 2 billion calculations per second
200,000 teraflops/2 billion = 100,000,000 Core 4.5Ghz ~ 95 gigaflops 200,000 trillion/95 billion = 2.1 million
13
Tianhe-2 computer 16,000 nodes with a total of 3,120,000 cores. Space:
720 square meters Weight: > 150 tons When will computers be faster?
15
So every 2 years … a supercomputer …
16
Your PC
18
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.1 What is a Control Structure?
19
Boolean Expressions The Boolean data type contains two Boolean values, denoted as True and False in Python. A Boolean expression is an expression that evaluates to a Boolean value. Boolean expressions are used to denote the conditions for selection and iterative control statements. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions
20
Relational Operators in Python
Note that these operators not only apply to numeric values, but to any set of values that has an ordering, such as strings. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions
21
Let’s Try It What do each of the following relational expressions evaluate to? Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions
22
Membership Operators Python provides a convenient pair of membership operators. These operators can be used to easily determine if a particular value occurs within a specified list of values. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions
23
>>> 'Dr.' in 'Dr. Madison' True
The membership operators can also be used to check if a given string occurs within another string, >>> 'Dr.' in 'Dr. Madison' True As with the relational operators, the membership operators can be used to construct Boolean expressions. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions
24
Boolean Operators George Boole, in the mid-1800s, developed what we now call Boolean algebra. His goal was to develop an algebra based on true/false rather than numerical values. Boolean algebra contains a set of Boolean (logical) operators, denoted by and, or, and not. These logical operators can be used to construct arbitrarily complex Boolean expressions. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions
25
Logical and is true only when both its operands are true — otherwise, it is false. Logical or is true when either or both of its operands are true, and thus false only when both operands are false. Logical not simply reverses truth values — not False equals True, and not True equals False. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions
26
Let’s Try It What do each of the following relational expressions evaluate to? Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions
27
Operator Precedence and Boolean Expressions
We saw the notion of operator precedence related to the use of arithmetic expressions. Operator precedence applies to Boolean operators as well. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions
28
Operator Precedence and Boolean Expressions
As we saw earlier, in the table, high-priority operators are placed before lower-priority operators. Thus we see that all arithmetic operators are performed before any relational or Boolean operators. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions
29
If not all subexpressions,
As with arithmetic expressions, it is good programming practice for use parentheses, even if not needed, to add clarity and enhance readability, If not all subexpressions, Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions
30
Fundamental logically equivalent Boolean expressions.
The last two equivalences are referred to as De Morgan’s Laws. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions
31
Let’s Try It From the Python shell, enter the following and observe the results. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions
32
Selection Control A selection control statement is a control statement providing selective execution of instructions. A selection control structure is a given set of instructions and the selection control statement(s) controlling their execution. We look at the if statement providing selection control in Python next. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control
33
If Statement An if statement is a selection control statement based on the value of a given Boolean expression. Note that if statements may omit the “else” part. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control
34
Indentation in Python One fairly unique aspect of Python is that the amount of indentation of each program line is significant. In most programming languages, indentation has no affect on program logic—it is simply used to align program lines to aid readability. In Python, however, indentation is used to associate and group statements. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control
35
A header in Python is a specific keyword followed by a colon
A header in Python is a specific keyword followed by a colon. In the example, the if-else statement contains two headers, “if which 5 = 'F':” containing keyword if, and “else:” consisting only of the keyword else. Headers that are part of the same compound statement must be indented the same amount—otherwise, a syntax error will result. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control
36
The set of statements following a header in Python is called a suite (commonly called a block ). The statements of a given suite must all be indented the same amount. A header and its associated suite are together referred to as a clause. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control
37
Let’s Try It From IDLE, create and run a Python program containing the code on the left and observe the results. Modify and run the code to match the version on the right and again observe the results. Make sure to indent the code exactly as shown. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control
38
Multi-Way Selection Python provides two means of constructing multi-way selection — one involving multiple nested if statements, and the other involving a single if statement and the use of elif headers. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control
39
Multi-Way Selection by Use of Nested If Statements
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control
40
Let’s Try It From IDLE, create and run a simple program containing the code below and observe the results. Make sure to indent the code exactly as shown. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control
41
Multi-Way Selection by Use of elif Header
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control
42
Iterative Control An iterative control statement is a control statement providing the repeated execution of a set of instructions. An iterative control structure is a set of instructions and the iterative control statement(s) controlling their execution. Because of their repeated execution, iterative control structures are commonly referred to as “loops.” We look at one specific iterative control statement next, the while statement. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control
43
While Statement A while statement is an iterative control statement that repeatedly executes a set of statements based on a provided Boolean expression (condition). All iterative control needed in a program can be achieved by use of the while statement. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control
44
As long as the condition of a while statement is true, the statements within the loop are (re)executed. Once the condition becomes false, the iteration terminates and control continues with the first statement after the while loop. Note that it is possible that the first time a loop is reached, the condition may be false, and therefore the loop would never be executed. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control
45
Execution steps for adding the first three integers by use of the previous while loop.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control
46
Input Error Checking The while statement is well suited for input error checking in a program. This is demonstrated in the revised version of the temperature conversion program. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control
47
The while loop is used to force the user to re-enter if neither an ‘F’ (for conversion to Fahrenheit) or a ‘C’ (for conversion to Celsius) is not entered. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control
48
Let’s Try It In IDLE, create and run a simple program containing the code below and observe the results. Make sure to indent the code exactly as shown. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control
49
Infinite Loops An infinite loop is an iterative control structure that never terminates (or eventually terminates with a system error). Infinite loops are generally the result of programming errors. For example, if the condition of a while loop can never be false, an infinite loop will result when executed. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control
50
Following is an example program containing an infinite loop.
The while loop is an infinite loop (for any value of n 1 or greater) because the value of current is not incremented Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control
51
Let’s Try It From IDLE, create and run a simple program containing the code below and observe the results. Indent the code exactly as shown. To terminate an executing loop, hit ctrl-C. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control
52
Definite vs. Indefinite Loops
A definite loop is a program loop in which the number of times the loop will iterate can be determined before the loop is executed. Following is an example of a definite loop. Although it is not known what the value of n will be until the input statement is executed, its value is known by the time the while loop is reached. Thus, it will execute “n times.” Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control
53
An indefinite loop is a program loop in which the number of times that the loop will iterate cannot be determined before the loop is executed. In this case, the number of times that the loop will be executed depends on how many times the user mistypes the input. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control
54
Boolean Flags and Indefinite Loops
Often the condition of a given while loop is denoted by a single Boolean variable, called a Boolean flag. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control
55
Boolean variable valid_entries on line 12 in the following program is an example of the use of a Boolean flag for controlling a while loop. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control
56
Coin Change Exercise Program
Let’s Apply It Coin Change Exercise Program The following program implements an exercise for children learning to count change. It displays a random value between 1 and 99 cents, and asks the user to enter a set of coins that sums exactly to the amount shown. The program utilizes the following programming features: ➤ while loop ➤ if statement ➤ Boolean flag ➤ random number generator Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control
57
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control
58
For Next Class, Thursday
Homework 3 is due Thursday, 11:59pm, September 7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.