0% found this document useful (0 votes)
33 views15 pages

CS111 - Fundamentals of CS General Program - General

Uploaded by

qwer353666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views15 pages

CS111 - Fundamentals of CS General Program - General

Uploaded by

qwer353666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

CS111 – Fundamentals of Computer Science

Introduction to Python
Sabah Sayed
Remember: Difference between an algorithm, a
program, and a process

• A program is a representation of an algorithm


• A process is a program in execution
– i.e., A process is an activity of executing an algorithm or a
program

Represented Executed
Problem
Solved Algorithm as Program as Process
by
Why convert algorithm to a program?

Programming
language
What is a program?
• A set of instructions (i.e., algorithm) that are used to solve a
problem and are written in a programming language
– e.g., FORTRAN, COBOL, Lisp, Basic, Pascal, C, C++, Java, C#, Python,

• The person who write computer programs is called
Programmer/developer
• The computer programs are called software

Program
The steps to solve a problem
• Writing a program in a programming language is composed of 2
phases:
1. Implementation phase (by Developer): A developer write a program using
syntax of a specific programming language
2. Compilation phase (by Compiler): By
Developer
• A compiler checks the syntax errors inside a program By
• Convert a program into machine language (i.e., 0’s and 1’s) Compiler
Compiler vs. Interpreter
• Compiler reads the entire program and convert it into
machine code
– Used by some programming language, e.g., C++
• Interpreter reads one statement at a time from the program
and convert it into machine code
– Used by some programming language, e.g., python
• The machine code is the code that the computer can
understand and run directly.
Compiler vs. Interpreter (cont.)
Why Python?
• Easier syntax, more readable, shorter programs.
• Has large libraries of various built-in functions.
• Portable, runs on Unix, Mac, and windows.
• Scalable, used for small and large programs.
• Interactive, can write and test programs from terminal.
• https://www.python.org/downloads/
Installing Python

• Go to the page https://thonny.org/


– Download thonny IDE for windows and install it
• IDE: Integrated Development Environment
Open Thonny IDE

Your variables will


be displayed here
Script mode: write a complete
python program (e.g., test.py)

Interactive mode: write python


statements and execute one
statement at a time
Simple examples using the shell (i.e., interactive
mode)
• The quotation marks in the program mark the
beginning and end of the text to be displayed;
they don’t appear in the result
• The parentheses indicate that print is a
function (pre-written block of computer code that
will perform a specific action or set of actions )
• Note:
– When you ask Python to run a function we say
that you have "called" the function
– print is considered built-in function
– If you call print () with no argument, then a new
line (i.e., line break) will be printed
Operators
• Assignment operator
• Operator = is called the 'assignment operator‘ used to store the data on the right
side of the statement into the variable that on the left side
• Arithmetic operators
– operators +, -, *, and / perform addition, subtraction, multiplication, and division
– operator ** raises a number to a power
– operator % perform modulus (get reminder of division)
– Operator // provides integer division (returns the quotient and ignores the
remainder (i.e., round down))
• Logical operators (true/false)
== (equal), != (not equal), > (greater), >= (at least), < (less) and <= (at most).
– Combine multiple logical operators using and, or, not
– e.g., (x > 3 and x < 7) ( x < 0 or x > 10) (not x < 0)
Variables
• A variable: is a storage location inside the memory and has a unique name to use it
• You can create a variable by using the following syntax
– variablename = somedata (Note: this will create a variable and initialize it)
• e.g., courseName=“fundamentals of CS”
• Note when specifying a variable name
– Variable names can’t contain spaces (though you can use the underscore character
("_") in place of a space)
– The 1st character of a variable name must be a letter or the underscore character.
– No special characters are allowed in a variable name besides alphanumeric
(alphabet or numeric) characters and the underscore ("_") character (no special
characters like &, !, +, $, etc.)
– Python is case sensitive, so two variables with the same name but different case
(i.e. name vs Name) are two different variables
– You can't use any of Python's built in "reserved words" (i.e. you can't create a
variable called "print" because that would conflict with the print function)
Examples
1
courseName=“fundamentals of CS" fundamentals of CS
print (courseName)

You must define


2 a variable before
print (foo) using it
foo = "Hello, world!" Otherwise, a
syntax error will
appear
Write the 1st python program
• Open thonny IDEL file new file
Save as with the name
“myfistprogram.py”
• Note: python files have the extension
(.py)
• Write the following code in the created
file
• Run this code by click on the Run
menuRun current script (or F5) o/p
• The o/p will appear in the interactive
mode as following
• Note: the statements will be executed
sequentially

You might also like