Introduction to Python Programming Concepts and Basics

Course A201:
Introduction to Programming
09/16/2010
Outlines for today
A new type of value: Boolean
Concept of keyword and indentation
Several basic concepts about Branching
IF - ELSE – IF and WHILE loop
Keep trace: what’s the stop condition
How and when to use “break”  and “continue”
Demonstration of “Guess My Number Game”
Boolean and Comparison Operators
Boolean values: 
True
 or 
False
Statements that will generate boolean values:
1 == 1
   
True
2 <= 0
   
False
4 > 1
   
True
9 != 99
   
True
“1”  
==  1
   
False
 
These are Comparison
 operators
keyword
In Assignment 1, question 2, the last variable is
for
”, which seams legit but actually not
     This is a 
KEYWORD
Keywords are a set of words that have
special/predefined meanings:
for
, 
if
, 
else
, 
while
, 
break
, 
import
, 
from
They will appear in orange in .py files in IDLE.
Indentation
In Python, proper indentations
are crucial ->
Python use indentations to
determine the structure of if-
else blocks, while blocks, etc.
If you forget to have
indentations, it will give you
syntax error
Enter tab
 
if user == 
“bye”
:
  
print ( 
“bye!”
 )
 
elif user == “hi”:
  
print( 
“Hi”
 )
 
else:
  
print(
“…”
)
  
Conditional: IF
 
if user == 
“bye”
:
  
print ( 
“bye!”
 )
 
elif user == “hi”:
  
print( 
“Hi”
 )
 
else:
  
print(
“…”
)
  
Don’t forget the COLON
You
 can have only IF, or IF-
ELSE, or IF-ELIF, or IF-ELIF-
ELSE
You
 can have another IF
block inside a IF block:
if …:
 
if …:
 
    some statements
Conditional: WHILE
 
gpa = 0.0
 
while gpa <= 4.0:
  
gpa = float(raw_input(
“Enter GPA: ”
 ))
  
print(
“Not high enough”
 )
 
While [
this condition
] is true, keep executing the
[
statements in here
]
Creating loops, make repetitive tasks easier
Keep trace
What’s wrong with this program:
 
health = 10
 
trolls = 0
 
damage = 3
 
while health != 0:
  
trolls += 1
  
health -= damage
Keep trace
Health
 
trolls
  
damage
 
health!= 0
10 
  
0 
  
3 
  
True
7
   
1 
  
3 
  
True
4 
   
2
  
3 
  
True
1 
   
3 
  
3 
  
True
-2 
  
4 
  
3 
  
True
-5 
  
5 
  
3 
  
True
-7 
  
6 
  
3 
  
True
Keep trace
What’s wrong with this program:
 
health = 10
 
trolls = 0
 
damage = 3
 
while health != 0:
  
trolls += 1
  
health -= damage
 
<- This condition wil
l be true
FOREVER!!!
Keep trace
What’s wrong with this program:
 
health = 10
 
trolls = 0
 
damage = 3
 
while health 
>
 0:
  
trolls += 1
  
health -= damage
 
<- You
 have to confirm that this
condition will be false at some
time
Usage of break
 
gpa = 0.0
 
while 
True
:
  
gpa = float(raw_input(
“Enter GPA: ”
 ))
  
if gpa > 4.0:
   
break
Break out of the loop
Usage of continue
 
counter = 0
 
while counter <= 10:
  
count += counter
  
if count == 5
   
continue
  
print(counter)
 
 
print(“-” * 20)
The output won’t include 5.
 
 
 When you reach continue,
 the
rest of code (
in the red
rectangle
) will be omitted,
computer goes straight to nex
t
interation.
Guess My Number Game
pick a random number
while the player hasn't guessed the number,
let the player guess
If guess is right, congratulate the player
Lab work
Write a program that flips a coin 100 times
and then tells you the number of head and
tails
Modify your program so that it prompts the
user for the number of times to flip the coin
On paper in English first, on computer in
Python next
Optional team work
Have a nice evening
!
See you tomorrow~
Slide Note
Embed
Share

Explore the fundamentals of Python programming with topics covering Boolean values, comparison operators, keywords, indentation rules, conditional statements (IF, WHILE), and keep-trace scenarios in code implementation. Understanding these concepts is essential for beginners in programming.

  • Python
  • Programming Basics
  • Boolean Values
  • Conditional Statements
  • Indentation Rules

Uploaded on Sep 21, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Course A201: Introduction to Programming 09/16/2010

  2. Outlines for today A new type of value: Boolean Concept of keyword and indentation Several basic concepts about Branching IF - ELSE IF and WHILE loop Keep trace: what s the stop condition How and when to use break and continue Demonstration of Guess My Number Game

  3. Boolean and Comparison Operators Boolean values: True or False Statements that will generate boolean values: 1 == 1 True 2 <= 0 False 4 > 1 True 9 != 99 True 1 == 1 False These are Comparison operators

  4. keyword In Assignment 1, question 2, the last variable is for , which seams legit but actually not This is a KEYWORD Keywords are a set of words that have special/predefined meanings: for, if, else, while, break, import, from They will appear in orange in .py files in IDLE.

  5. Indentation In Python, proper indentations are crucial -> Python use indentations to determine the structure of if- else blocks, while blocks, etc. If you forget to have indentations, it will give you syntax error Enter tab if user == bye : print ( bye! ) elif user == hi : print( Hi ) else: print( )

  6. Conditional: IF if user == bye : print ( bye! ) elif user == hi : print( Hi ) else: print( ) Don t forget the COLON You can have only IF, or IF- ELSE, or IF-ELIF, or IF-ELIF- ELSE You can have another IF block inside a IF block: if : if : some statements

  7. Conditional: WHILE gpa = 0.0 while gpa <= 4.0: gpa = float(raw_input( Enter GPA: )) print( Not high enough ) While [this condition] is true, keep executing the [statements in here] Creating loops, make repetitive tasks easier

  8. Keep trace What s wrong with this program: health = 10 trolls = 0 damage = 3 while health != 0: trolls += 1 health -= damage

  9. Keep trace Health 10 7 4 1 -2 -5 -7 trolls 0 1 2 3 4 5 6 damage 3 3 3 3 3 3 3 health!= 0 True True True True True True True

  10. Keep trace What s wrong with this program: health = 10 trolls = 0 damage = 3 while health != 0: trolls += 1 health -= damage <- This condition will be true FOREVER!!!

  11. Keep trace What s wrong with this program: health = 10 trolls = 0 damage = 3 while health > 0: trolls += 1 health -= damage <- You have to confirm that this condition will be false at some time

  12. Usage of break gpa = 0.0 while True: gpa = float(raw_input( Enter GPA: )) if gpa > 4.0: break Break out of the loop

  13. Usage of continue counter = 0 while counter <= 10: count += counter if count == 5 continue print(counter) print( - * 20) When you reach continue, the rest of code (in the red rectangle) will be omitted, computer goes straight to next interation. The output won t include 5.

  14. Guess My Number Game pick a random number while the player hasn't guessed the number, let the player guess If guess is right, congratulate the player

  15. Lab work Write a program that flips a coin 100 times and then tells you the number of head and tails Modify your program so that it prompts the user for the number of times to flip the coin On paper in English first, on computer in Python next Optional team work

  16. Have a nice evening! See you tomorrow~

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#