Me 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

INTRODUCTION TO COMPUTERS

AND PROGRAMMING
Meeting 4
Dr. Ahmed Ezzat Labib
READING INPUT FROM THE KEYBOARD

• the input function in an assignment statement that follows this general format:
variable = input(prompt)
• prompt is a string that is displayed on the screen.
• The string’s purpose is to instruct the user to enter a value
• variable is the name of a variable that references the data that was entered on the
keyboard.
EXAMPLE 1

>>> name = input('What is your name? ')

What is your name? Holly

>>> print(name)

Holly
>>>
EXAMPLE 2

# Get the user's first name.


first_name = input('Enter your first name: ')
# Get the user's last name.
last_name = input('Enter your last name: ')
# Print a greeting to the user. Program Output

print('Hello', first_name, last_name) Enter your first name: Vinny


Enter your last name: Brown
Hello Vinny Brown
READING NUMBERS WITH THE INPUT
FUNCTION
• The input function always returns the user’s input as a string, even if the user enters
numeric data.
• Python has built-in functions that you can use to convert a string to a numeric type.

Function Description
int(item) You pass an argument to the int( ) function and it
returns the argument’s value converted to an int.

float(item) You pass an argument to the float( ) function and


it returns the argument’s value converted to a
float.
READING NUMBERS WITH THE INPUT
FUNCTION
String s = input('How many hours did you work? ')

Integer num = int(s)

Equivelant To

num = int(input('How many hours did you work? '))


EXAMPLE
# Get the user's name, age, and income. Program Output (input in yellow)

name = input('What is your name? ') What is your name? Chris


age = int(input('What is your age? ')) What is your age? 25
What is your income? 75000.0
income = float(input('What is your income? ')) Here is the data you entered:
Name: Chris
# Display the data. Age: 25
print('Here is the data you entered:') Income: 75000.0
print('Name:', name)
print('Age:', age)
print('Income:', income)
PERFORMING CALCULATIONS
Symbol Operation Description
Addition Adds two numbers
+
Subtraction Subtracts one number from another
-
Multiplication Multiplies one number by another
*
Division Divides one number by another and gives
/
the result as a floating-point number
Integer Division Divides one number by another and gives
//
the result as an integer
Remainder Divides one number by another and gives
%
the remainder
Exponent Raises a number to a power
**
OPERATOR PRECEDENCE

• The precedence of the math operators, from highest to lowest,


are:
1. Exponentiation: **

2. Multiplication, division, and remainder: * / // %


3. Addition and subtraction: + −
SOME EXPRESSIONS

Expression Value
(5 + 2) * 4 28
10 / (5 − 3) 5
8 + 12 * 6 − 2 78
(6 − 3) * (2 + 7) − 1 9
2**3**2 512
17 % 3 2
5//2 2
5/2 2.5
CONVERTING MATH FORMULAS TO PROGRAMMING
STATEMENTS
Algebraic Expression Programming Expression
𝟔𝐁 6*B
(𝟑)(𝟏𝟐) 3 * 12
𝟒𝐱𝐲 4*x*y
𝒂+𝒃 (a+b)/c
𝒙=
𝒄
𝒙 3*x/2
𝒚=𝟑
𝟐
𝒛 = 𝟑𝒃𝒄 + 𝟒 z=3*b*c+4
𝒙+𝟐 a=(x+2)/(b–1)
𝒂=
𝒃−𝟏
BREAKING LONG STATEMENTS INTO
MULTIPLE LINES
units_sold = 3
sales_amount = 20
print('We sold', units_sold, \
'for a total of', sales_amount)

We sold 3 for a total of 20


BREAKING LONG STATEMENTS INTO
MULTIPLE LINES
• Here is a statement that performs a mathematical calculation and has been broken up
to fit on two lines:

result = var1 * 2 + var2 * 3 + \


‫ﻟﻤﺎ ﻣﻴﻜﻨﺶ‬
var3 * 4 + var4 * 5 ‫ﺍﻟﻜﻼﻡ ﺑﻌﺪﻳﻬﺎ‬
‫ﻭﻳﻜﻮﻥ ﻓﻲ‬
‫ﺍﻟﺴﻄﺮ ﺍﻟﻠﻰ‬
‫ﺗﺤﺘﻴﻬﺎ‬
‫ﻣﺒﺎﺷﺮﺓ‬
‫ﺍﺳﺘﺨﺪﻣﻬﺎ‬
SUPPRESSING THE PRINT FUNCTION’S
ENDING NEWLINE
print('One', end=' ')
print('Two', end=' ')
print('Three')

One Two Three


SUPPRESSING THE PRINT FUNCTION’S
ENDING NEWLINE
print('One', end='')
print('Two', end='')
print('Three')

OneTwoThree
SUPPRESSING THE PRINT FUNCTION’S
ENDING NEWLINE
>>> print('One', 'Two', 'Three', sep='')

OneTwoThree
SUPPRESSING THE PRINT FUNCTION’S
ENDING NEWLINE "" ‫ﺧﺎﺭﺝ‬

>>> print('One', 'Two', 'Three', sep='*')

One*Two*Three
SUPPRESSING THE PRINT FUNCTION’S
ENDING NEWLINE
>>> print('One', 'Two', 'Three', sep='---')

One---Two---Three
ESCAPE CHARACTERS

print('One\nTwo\nThree')

One
Two
Three
ESCAPE CHARACTERS ""‫ﺩﺍﺧﻞ ﺍﻝ‬

Escape Character Effect

\n Causes output to be advanced to the next line.


\t Causes output to skip over to the next horizontal
tab position.
\’ Causes a single quote mark to be printed.
\” Causes a double quote mark to be printed.
\\ Causes a backslash character to be printed.
ESCAPE CHARACTERS

print('Mon\tTues\tWed')
print('Thur\tFri\tSat')

Mon Tues Wed


Thur Fri Sat
ESCAPE CHARACTERS

print("Your assignment is to read \"Hamlet\" by tomorrow.")


print('I\'m ready to begin.')

Your assignment is to read "Hamlet" by tomorrow.


I'm ready to begin.
ESCAPE CHARACTERS

print('The path is C:\\temp\\data.')

The path is C:\temp\data.


DISPLAYING MULTIPLE ITEMS WITH
THE + OPERATOR

print('This is ' + 'one string.')

This is one string


DISPLAYING MULTIPLE ITEMS WITH
THE + OPERATOR
print('Enter the amount of ' + \
'sales for each day and ' + \
'press Enter.')
To Be Cont….

You might also like