ComputerSysAndProgramming_4
ComputerSysAndProgramming_4
Tarek Aly
01126182476
http://41.32.221.109:8080/DrTarekAly.htm
E-mail: tarekmmmmt@{pg.cu.edu.eg; egyptpost.org; gmail.com; yahoo.com}
Contents
Overview of Programming
Basic Program Elements Library Functions
What Are Computer and Library Variables
Computing?
Variables and
An Algorithm Is Assignment
Automation The docstring
What Is a Program? import Statements
Why Python? Evaluating Expressions
Developing Python Programs Type Conversion
Basic Elements: Data Functions
Basic Operations: Arithmetic Composing Expressions
Built-In Functions Getting Help on a
Function
Examples:
Make a pizza from scratch
Give directions to the soccer field
Change a flat tire on an automobile
Example: Make Pancakes
1. Beat two eggs
2. Add 2 Tb. brown sugar
3. Add 1/2 cup of milk
4. Add 1 Tb. melted butter
5. Add 1 cup self-rising flour
6. Pour batter onto griddle and cook
until done
Automation
If we can design an algorithm to
solve a problem . . .
Examples:
abs(-5)
max(33, 66)
Example:
import math
math.sqrt(2)
pi = 3.14
34 * 34 * pi
Example:
import math
3 * math.pi
Or:
3 * pi
IDLE editor
Python source code
Python compiler
Byte code
Outputs to user
docstring
import statements
input statements
computation statements
output statements
print(math.pi)
print(math.sqrt(2))
print(pi)
print(pi)
print(sqrt(2))
print(10 + x * y ** 2)
int(3.72) # Returns 3
abs(-5) # Returns 5
c = math.sqrt(a ** 2 + b ** 2)
print('The hypotenuse is', c)
>>> dir(math)
['__doc__', '__file__', '__name__', 'acos', 'asin',
'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees',
'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp',
'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi',
'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan',
'tanh']
>>>
The dir function returns a list of all of the named
components in a module
2023-11-09 Computer Sys. And Programming 40/112
Getting Help on a Function
>>> import math
>>> dir(math)
['__doc__', '__file__', '__name__', 'acos',
'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh',
'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod',
'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf',
'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt',
'tan', 'tanh']
>>> help(math.sqrt)
sqrt(x)