March, 2023 – AUT
www.aut.edu
Programming
Python
2
© AUT – www.aut.edu
Python and Game design
Workshop
Dr. Kamil KLAIME
3
© AUT – www.aut.edu
 The workshop will cover the following topics during 4 sessions:
1. Programming Concept (session 1)
2. Programming Languages-Python (session 1)
3. Numbers and variables in python (session 1)
4. Conditions and loops (session 2)
5. Games design under python using Pygame library
(session 3)
6. Introduction to Unity 3D (session 4)
Content
4
© AUT – www.aut.edu
FROM OEA to AUT-North
5
© AUT – www.aut.edu
What Is a Program?
 Usually, one or more algorithms written in a programming language that
can be translated to run on a real machine
 We sometimes call programs software
6
© AUT – www.aut.edu
What Is a Programming Language?
 A programming language is somewhat like a
natural language, but with a very limited set of
statements and strict syntax rules.
 Has statements to implement sequential,
conditional and iterative processing - algorithms
 Examples: FORTRAN, COBOL, Lisp, Basic,
Pascal, C, C++, Java, C#, Python, …
7
© AUT – www.aut.edu
Compiler
 A compiler is a program that converts a program written in a
programming language into a program in the native language, called
machine language, of the machine that is to execute the program.
8
© AUT – www.aut.edu
From Algorithms to Hardware (with compiler)
Algorithm
Program
A real computer
Translate (by a human being)
Translate (by compiler program)
9
© AUT – www.aut.edu
The Program Development Process (Control
Flow)
Edit
Compile
Run
Syntax errors
Input Output
Runtime errors
10
© AUT – www.aut.edu
Three kinds of errors
 Syntax error : Some statement in the program is not a legal statement
in the language.
 Runtime error : An error occurs while the program is executing,
causing the program to terminate (divide by zero, etc.)
 Logic error : The program executes to completion, but gives incorrect
results.
11
© AUT – www.aut.edu
Interpreter
 An alternative to a compiler is a program called an interpreter. Rather
than convert our program to the language of the computer, the
interpreter takes our program one statement at a time and executes a
corresponding set of machine instructions.
12
© AUT – www.aut.edu
Interpreter
Edit
Interpreter
Syntax or runtime errors
Input
Output
13
© AUT – www.aut.edu
Python
 Python is a real-world, production language that is freely
available for most computers.
http:www.python.org
 you can run and execute python online or on your machine
using many platform, ex : Thonny
14
© AUT – www.aut.edu
Python online
https://pynative.com/online-python-code-editor-to-execute-python-code/
15
© AUT – www.aut.edu
Network :: OEA-Tripoli
Password ::OEA@06961
WIFI
16
© AUT – www.aut.edu
Python
 Python uses an interpreter. Not only can we write complete
programs, we can work with the interpreter in a statement by
statement mode enabling us to experiment quite easily.
17
© AUT – www.aut.edu
Language terminology
 Syntax: The formal rules for legal statements in the language.
 Semantics: The meaning of the statements - what happens when the
statement is executed.
18
© AUT – www.aut.edu
Three major control constructs of programming (Execution flow of
instructions)
 Sequential: Simply do steps one after the other in order they are
listed.
 Conditional: Decide which statement to do next based on some
true/false test.
 Iterative: A set of statements is repeated over and over until some
condition is met.
19
© AUT – www.aut.edu
Sequential Operations “Atomic”
 Input
 Computation
 Output
20
© AUT – www.aut.edu
The Basic Pattern
 Most of our programs will use the basic pattern of
 Get some user input
 Perform some algorithm on the input
 Provide results as output
21
© AUT – www.aut.edu
Identifiers
 Identifiers are names of various program
elements in the code that uniquely identify the
elements. They are the names of things like
variables or functions to be performed. They're
specified by the programmer and should have
names that indicate their purpose.
 In Python, identifiers
 Are made of letters, digits and underscores
 Must begin with a letter or an underscore
 Examples: temperature, myPayrate, score2
22
© AUT – www.aut.edu
Keywords
 Keywords are reserved words that have special meaning in the
Python language. Because they are reserved, they can not be used
as identifiers. Examples of keywords are if, while, class, import.
23
© AUT – www.aut.edu
Variables in Python
 A variable has
 A name – identifier
 A data type - int, float, str, etc.
 Storage space sufficient for the type.
24
© AUT – www.aut.edu
Numeric Data Types
 int
This type is for whole numbers, positive or negative. Examples: 23,
-1756
 float
This type is for numbers with possible fraction parts. Examples:
23.0, -14.561
25
© AUT – www.aut.edu
Integer operators
The operations for integers are:
+ for addition
- for subtraction
* for multiplication
/ for integer division: The result of 14/5 is 2
% for remainder: The result of 14 % 5 is 4
 *, /, % take precedence over +, -
x + y * z will do y*z first
 Use parentheses to dictate order you want.
(x+y) * z will do x+y first.
26
© AUT – www.aut.edu
Integer Expressions
 Integer expressions are formed using
 Integer Constants
 Integer Variables
 Integer Operators
 Parentheses
27
© AUT – www.aut.edu
Python Assignment Statements
 In Python, = is called the assignment operator
and an assignment statement has the form
<variable> = <expression>
 Here
 <variable> would be replaced by an actual variable
 <expression> would be replaced by an expression
 Python: age = 19
28
© AUT – www.aut.edu
Python Assignment Statement
 Syntax: <variable> = <expression>
 Note that variable is on left
 Semantics:
Compute value of expression
Store this as new value of the variable
 Example: Pay = PayRate * Hours
Payrate
10
Hours
40
Pay
400
29
© AUT – www.aut.edu
Python Session
30
© AUT – www.aut.edu
Python Session
31
© AUT – www.aut.edu
What about floats?
 When computing with floats, / will indicate regular division with
fractional results.
 Constants will have a decimal point.
 14.0/5.0 will give 2.8 while 14/5 gives 2.
32
© AUT – www.aut.edu
Comments
 Often we want to put some documentation in our program. These
are comments for explanation, but not executed by the computer.
 If we have # anywhere on a line, everything following this on the line
is a comment – ignored
33
© AUT – www.aut.edu
Numerical Input
 To get numerical input from the user, we use an
assignment statement of the form
<variable> = input(<prompt>)
 Here
 <prompt> would be replaced by a prompt for the user inside
quotation marks
 If there is no prompt, the parentheses are still needed
 Semantics
 The prompt will be displayed
 User enters number
 Value entered is stored as the value of the variable
34
© AUT – www.aut.edu
Print Statement
 For output we use statements of the form
print <expression>
 Semantics
 Value of expression is computed
 This value is displayed
 Several expressions can be printed – separate them by commas
35
© AUT – www.aut.edu
Example - Fahrenheit to Centigrade
 We want to convert a Fahrenheit temperature to Centigrade.
 The formula is C = (F -32) x 5/9
 We use type float for the temperatures.
36
© AUT – www.aut.edu
Python Session

Lecture 1-Intro to programming and python.pdf

  • 1.
    March, 2023 –AUT www.aut.edu Programming Python
  • 2.
    2 © AUT –www.aut.edu Python and Game design Workshop Dr. Kamil KLAIME
  • 3.
    3 © AUT –www.aut.edu  The workshop will cover the following topics during 4 sessions: 1. Programming Concept (session 1) 2. Programming Languages-Python (session 1) 3. Numbers and variables in python (session 1) 4. Conditions and loops (session 2) 5. Games design under python using Pygame library (session 3) 6. Introduction to Unity 3D (session 4) Content
  • 4.
    4 © AUT –www.aut.edu FROM OEA to AUT-North
  • 5.
    5 © AUT –www.aut.edu What Is a Program?  Usually, one or more algorithms written in a programming language that can be translated to run on a real machine  We sometimes call programs software
  • 6.
    6 © AUT –www.aut.edu What Is a Programming Language?  A programming language is somewhat like a natural language, but with a very limited set of statements and strict syntax rules.  Has statements to implement sequential, conditional and iterative processing - algorithms  Examples: FORTRAN, COBOL, Lisp, Basic, Pascal, C, C++, Java, C#, Python, …
  • 7.
    7 © AUT –www.aut.edu Compiler  A compiler is a program that converts a program written in a programming language into a program in the native language, called machine language, of the machine that is to execute the program.
  • 8.
    8 © AUT –www.aut.edu From Algorithms to Hardware (with compiler) Algorithm Program A real computer Translate (by a human being) Translate (by compiler program)
  • 9.
    9 © AUT –www.aut.edu The Program Development Process (Control Flow) Edit Compile Run Syntax errors Input Output Runtime errors
  • 10.
    10 © AUT –www.aut.edu Three kinds of errors  Syntax error : Some statement in the program is not a legal statement in the language.  Runtime error : An error occurs while the program is executing, causing the program to terminate (divide by zero, etc.)  Logic error : The program executes to completion, but gives incorrect results.
  • 11.
    11 © AUT –www.aut.edu Interpreter  An alternative to a compiler is a program called an interpreter. Rather than convert our program to the language of the computer, the interpreter takes our program one statement at a time and executes a corresponding set of machine instructions.
  • 12.
    12 © AUT –www.aut.edu Interpreter Edit Interpreter Syntax or runtime errors Input Output
  • 13.
    13 © AUT –www.aut.edu Python  Python is a real-world, production language that is freely available for most computers. http:www.python.org  you can run and execute python online or on your machine using many platform, ex : Thonny
  • 14.
    14 © AUT –www.aut.edu Python online https://pynative.com/online-python-code-editor-to-execute-python-code/
  • 15.
    15 © AUT –www.aut.edu Network :: OEA-Tripoli Password ::OEA@06961 WIFI
  • 16.
    16 © AUT –www.aut.edu Python  Python uses an interpreter. Not only can we write complete programs, we can work with the interpreter in a statement by statement mode enabling us to experiment quite easily.
  • 17.
    17 © AUT –www.aut.edu Language terminology  Syntax: The formal rules for legal statements in the language.  Semantics: The meaning of the statements - what happens when the statement is executed.
  • 18.
    18 © AUT –www.aut.edu Three major control constructs of programming (Execution flow of instructions)  Sequential: Simply do steps one after the other in order they are listed.  Conditional: Decide which statement to do next based on some true/false test.  Iterative: A set of statements is repeated over and over until some condition is met.
  • 19.
    19 © AUT –www.aut.edu Sequential Operations “Atomic”  Input  Computation  Output
  • 20.
    20 © AUT –www.aut.edu The Basic Pattern  Most of our programs will use the basic pattern of  Get some user input  Perform some algorithm on the input  Provide results as output
  • 21.
    21 © AUT –www.aut.edu Identifiers  Identifiers are names of various program elements in the code that uniquely identify the elements. They are the names of things like variables or functions to be performed. They're specified by the programmer and should have names that indicate their purpose.  In Python, identifiers  Are made of letters, digits and underscores  Must begin with a letter or an underscore  Examples: temperature, myPayrate, score2
  • 22.
    22 © AUT –www.aut.edu Keywords  Keywords are reserved words that have special meaning in the Python language. Because they are reserved, they can not be used as identifiers. Examples of keywords are if, while, class, import.
  • 23.
    23 © AUT –www.aut.edu Variables in Python  A variable has  A name – identifier  A data type - int, float, str, etc.  Storage space sufficient for the type.
  • 24.
    24 © AUT –www.aut.edu Numeric Data Types  int This type is for whole numbers, positive or negative. Examples: 23, -1756  float This type is for numbers with possible fraction parts. Examples: 23.0, -14.561
  • 25.
    25 © AUT –www.aut.edu Integer operators The operations for integers are: + for addition - for subtraction * for multiplication / for integer division: The result of 14/5 is 2 % for remainder: The result of 14 % 5 is 4  *, /, % take precedence over +, - x + y * z will do y*z first  Use parentheses to dictate order you want. (x+y) * z will do x+y first.
  • 26.
    26 © AUT –www.aut.edu Integer Expressions  Integer expressions are formed using  Integer Constants  Integer Variables  Integer Operators  Parentheses
  • 27.
    27 © AUT –www.aut.edu Python Assignment Statements  In Python, = is called the assignment operator and an assignment statement has the form <variable> = <expression>  Here  <variable> would be replaced by an actual variable  <expression> would be replaced by an expression  Python: age = 19
  • 28.
    28 © AUT –www.aut.edu Python Assignment Statement  Syntax: <variable> = <expression>  Note that variable is on left  Semantics: Compute value of expression Store this as new value of the variable  Example: Pay = PayRate * Hours Payrate 10 Hours 40 Pay 400
  • 29.
    29 © AUT –www.aut.edu Python Session
  • 30.
    30 © AUT –www.aut.edu Python Session
  • 31.
    31 © AUT –www.aut.edu What about floats?  When computing with floats, / will indicate regular division with fractional results.  Constants will have a decimal point.  14.0/5.0 will give 2.8 while 14/5 gives 2.
  • 32.
    32 © AUT –www.aut.edu Comments  Often we want to put some documentation in our program. These are comments for explanation, but not executed by the computer.  If we have # anywhere on a line, everything following this on the line is a comment – ignored
  • 33.
    33 © AUT –www.aut.edu Numerical Input  To get numerical input from the user, we use an assignment statement of the form <variable> = input(<prompt>)  Here  <prompt> would be replaced by a prompt for the user inside quotation marks  If there is no prompt, the parentheses are still needed  Semantics  The prompt will be displayed  User enters number  Value entered is stored as the value of the variable
  • 34.
    34 © AUT –www.aut.edu Print Statement  For output we use statements of the form print <expression>  Semantics  Value of expression is computed  This value is displayed  Several expressions can be printed – separate them by commas
  • 35.
    35 © AUT –www.aut.edu Example - Fahrenheit to Centigrade  We want to convert a Fahrenheit temperature to Centigrade.  The formula is C = (F -32) x 5/9  We use type float for the temperatures.
  • 36.
    36 © AUT –www.aut.edu Python Session