Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lesson 2 String in Python

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

Lesson 2

Numbers
and
Variables

Prepared by:

Jennifer O. Contreras
Python Casting
Python Casting
– Casting in python is therefore done using
constructor functions:
– int() - constructs an integer number from an
integer literal, a float literal (by rounding down to
the previous whole number), or a string literal
(providing the string represents a whole number)
– float() - constructs a float number from an integer
literal, a float literal or a string literal (providing the
string represents a float or an integer)
– str() - constructs a string from a wide variety of
data types, including strings, integer literals and
float literals
Python Booleans
Boolean Values
– Booleans represent one of two values: TRUE or FALSE
– When you compare two values, the expression is
evaluated and Python returns the Boolean answer:
String
– string is a sequence of characters
– Strings are what we call text, or keyboard characters,
in a programming language; they are groups (or
“strings”) of letters, numbers, and symbols.
– Strings are basically just a bunch of words.
– The words can be in English or any other language that
is supported in the Unicode standard
String literals
– String literals in python are surrounded by either single quotation
marks, or double quotation marks.
– 'hello' is the same as "hello".
– You can display a string literal with the print() function:
Assign String to a Variable

– Assigning a string to a variable is done with


the variable name followed by an equal sign
and the string:
Multiline Strings

– You can assign a multiline string to a variable


by using three quotes or three single quotes:
Python if ... else
– Python supports the usual logical conditions from mathematics:
– 'hello' is the same as "hello".
– You can display a string literal with the print() function:
– Equals: a == b
– Not Equals: a != b
– Less than: a < b
– Less than or equal to: a <= b
– Greater than: a > b
– Greater than or equal to: a >= b
Python if ... else
– These conditions can be used in several ways, most commonly in "if
statements" and loops.
– An "if statement" is written by using the if keyword.
Indentation
– Python relies on indentation (whitespace at the beginning of a line) to
define scope in the code. Other programming languages often use
curly-brackets for this purpose.
Python loop

– while loops – The while Loop


– With the while loop we can execute a set of
– for loops
statements as long as a condition is true
The break statement
– With the break statement we can stop the loop even if the
while condition is true:
The continue statement
– With the continue statement we can stop the current
iteration, and continue with the next:
The else statement
– With the else statement we can run a block of code once
when the condition no longer is true:
Python loop
– The for Loop
– A for loop is used for iterating over a sequence (that is either
a list, a tuple, a dictionary, a set, or a string).
Say my name
Exercise #2
– Write a Python program that will identify if a
number is an odd number or even number.
– Sample Run:
Exercise #2 Answer
– Write a Python program that will identify if a number is an odd number
or even number.
– Sample Run:
Exercise #3
– Write a Python program that will compute for the
average of 3 numbers
– Sample Run:
Exercise #3 Answer
– Write a Python program that will compute for the
average of 3 numbers
– Sample Run:
Exercise #4
– Write a Python program that will the grade of a student
based on the following:
– Average of three quizzes = 40%
– Midterm exam is = 25%
– Final exam is = 35%
Exercise #4 Answer
– Write a Python program that will the grade of a student
based on the following:
– Average of three quizzes = 40%
– Midterm exam is = 25%
– Final exam is = 35%
Exercise #5
– Using the program in Exercise #4, include the equivalent
letter grade of the student.

if grade >= 90 the letter grade is “A”


if grade >= 80 the letter grade is “B”
if grade >= 70 the letter grade is “C”
if grade >= 60 the letter grade is “D”
if grade >= 50 the letter grade is “E”
else
the letter grade is “F”
Exercise #5
– Using the program in Exercise #4, include the equivalent
letter grade of the student.
if grade >= 90 the letter grade is “A”
if grade >= 80 the letter grade is “B”
if grade >= 70 the letter grade is “C”
if grade >= 60 the letter grade is “D”
if grade >= 50 the letter grade is “E”
else
the letter grade is “F”
Exercise #7

You might also like