SlideShare a Scribd company logo
IWD 2021:
Building an Arcade Game
Using Python Workshop
By
Eng. Mahdi AlFaraj
Mariam AlMahdi
0
Before we start learning let’s
Download
“Python” & “Pycharm”
1
Environment
Website: python.org/downloads/
2
For Mac OS
Environment
3
1. From Downloads file choose python-3.9.2 file
2. Click continue
3. Click agree.
4. Insert your password then click install.
For windows
4
Environment
Downloading PyCharm
code editor
The Python IDE Integrated development environment
Website:
https://www.jetbrains.com/pycharm/do
wnload/#section=mac
5
Download PyCharm (Cont.)
1. Click on “Download” 2. Choose “Community” edition click “Download.
6
Mac users
3. Go to downloads and click on
PyCharm folder.
4. Drag PyCharm icon to
Applications folder.
5. This window will appear because it is
downloaded from the internet.
7
Windows users
3. Click on the downloaded PyCharm folder then click “next”.
4. Click “next”.
5. Check the 64-bit launcher then press ”next”.
8
6. Click ”install”
Windows users
7. Click on “finish”.
8. Click “Accept”.
9. Check ”OK”.
9
10. Click ”Don’t send”
• Choose the first choice then click next , next, next and next again.
• Finally, this is the main page of PyCharm. Click on create new
project.
10
Basics
11
What are we going to learn?
• Basic programming terminologies.
• Variables, String Numbers.
• print function.
• Writing comments.
• If/else statement.
• Loops.
• Range function.
• Modules and modules’ methods.
• Object-oriented programming.
• Functions as parameters.
12
Basic Programming terms
• code or source code: The sequence of instructions in a program.
• syntax: The set of legal structures and commands that can be used in
a particular programming language.
• output: The messages printed to the user by a program.
• console: The text box onto which output is printed.
13
Variables
• Rules for naming variables in Python:
• Variable name cannot be a Python keyword
• Variable name cannot contain spaces
• First character must be a letter or an underscore
• After first character may use letters, digits, or underscores
• Variable names are case sensitive
• Variable name should reflect its use
14
• Defining a variable:
• variable = expression
• Example: age = 29
Strings
• Strings (str)
• Must be enclosed in single (‘) or double (“) quote marks
• For example:
15
my_string = "This is a double-quoted string."
my_string = 'This is a single-quoted string.’
my_string= ‘ This is a “single-quoted” string.’
my_string= “ This is a ‘double-quoted’ string.”
Numbers
• Numbers in Python can be in a form of:
• Integer, for example:
Age = 37
• Float, for example:
GPA = 3.67
• You can do all of the basic operations
with numbers.
• + for addition
• - for subtraction
• * for multiplication
• / for division
• ** for exponent
• () for parenthesis
16
• Example: Output:
print(3+4)
print(1.6-9.5)
print(2**2)
print(5.5/2)
result= 2+ 3*4
print(result)
result= (2+ 3)*4
print(result)
7
-7.9
4
2.75
14
20
Variables, Strings, and Numbers
• Example:
• Calculate the following formula:
𝑎2
+ 𝑏3
× 𝑐
where a= 5, b=4, c=8
save the values in variables.
17
Comments
• Comments: notes of explanation within a program
• Ignored by Python interpreter
• Intended for a person reading the program’s code
• Begin with a # character
• Example:
• Output
# This line is a comment.
print("This line is not a comment, it is
code.")
This line is not a comment, it is code.
18
If statement
Making Decisions – Controlling Program Flow
• To make interesting programs, you must be able to make decisions about
data and take different actions based upon those decisions.
• if statement: Executes a group of statements only if a certain condition
is true. Otherwise, the statements are skipped.
• Syntax of the If statement:
if condition:
Statement
Statement
• First line known as the if clause
• Includes the keyword “if” followed by “condition”
• The condition can be true or false
• When the if statement executes, the condition is tested, and if it is true the block statements
are executed. otherwise, block statements are skipped.
19
if/ else statements
• The syntax of the if statement is as follows:
if boolean expression :
STATEMENT
STATEMENT
• Boolean expression: expression tested by if statement to determine if
it is true or false
• Example: a > b
• true if a is greater than b; false otherwise
• Relational operator: determines whether a specific relationship exists
between two values
• Example: greater than (>)
20
if/ else statements
• Boolean expressions use relational operators:
• Boolean expressions can be combined with logical operators:
21
If statement
• Example:
• Output:
• The indented block of code following an if statement is executed if
the Boolean expression is true, otherwise it is skipped.
22
gpa = 3.4
if gpa > 2.0:
print ("Your application is accepted.”)
Your application is accepted.
if/else Statements
• Dual alternative decision structure:
• two possible paths of execution
• One is taken if the condition is true, and the other if the condition is False
• If you have two mutually exclusive choices and want to guarantee that only
one of them is executed, you can use an if/else statement.
• The else statement adds a second block of code that is executed if the
Boolean expression is False.
• Syntax:
if condition:
statements
else:
other statements
• Rules of if/else statments
• if clause and else clause must be aligned
• Statements must be consistently indented
23
if/else statements
• Syntax:
if boolean expression :
STATEMENT
STATEMENT
else:
STATEMENT
STATEMENT
24
if/else
• if/else statement: Executes one block of statements if a certain
condition is True, and a second block of statements if it is False.
• Example:
25
gpa = 1.4
if gpa > 2.0:
print("Welcome to University!")
else:
print("Your application is denied.")
print(“Good luck”)
Loops
• A loop statement allows us to execute a
statement or group of statements
multiple times.
• Two types of loops:
• for loop:
Executes a sequence of statements multiple
times and abbreviates the code that manages
the loop variable.
• while loop:
Repeats a statement or group of statements
while a given condition is TRUE. It tests the
condition before executing the loop body.
26
range function
• The range function specifies a range of integers:
range (stop) - the integers between 0 (inclusive) and
stop (exclusive)
• Syntax:
for var in range(stop):
statements
• Repeats for values 0 (inclusive) to stop (exclusive)
for i in range(5):
... print(i)
0
1
2
3
4 27
for Loop
• for loop: Repeats a set of statements over a group of values.
• Syntax:
• Rules of loops:
• We indent the statements to be repeated with tabs or spaces.
• VariableName gives a name to each value, so you can refer to it in the statements.
• GroupOfValues can be a range of integers, specified with the range function.
• Example: Output:
for < var > in <sequence or group of valuse>:
<statements>
28
for x in range(1, 6):
print( x, ” squared is ", x * x )
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
Deference between “for” and “range” function
• Example:
• Output:
• Example:
• Output:
for x in (1 , 5):
print(x," Hello")
1 Hello
5 Hello
for x in range(1 , 5):
print(x," Hello")
1 Hello
2 Hello
3 Hello
4 Hello 29
Loop control statements
break Jumps out of the closest enclosing loop
continue Jumps to the top of the closest enclosing loop
pass Does nothing, empty statement placeholder
30
Nested Loop
• Loops defined within another loop is called Nested loop. When an outer loop contains an inner loop
in its body it is called Nested looping.
• Syntax:
for <expression>:
for <expression>:
body
• for example:
Stops summing at 4
because it is grater than
5 and does not count 5
as an output
2+1 =3
3+1=4
4+1=5 it will give
output 4 and
ends the output
result as 4
31
for a in range (1,5):
for b in range (1,a+1):
print(a)
while
• while loop: Executes a group of statements as long as a condition is
True.
• good for indefinite loops (repeat an unknown number of times)
• Syntax:
while condition:
statements
• Example: Output:
32
number = 1
while number < 200:
print (number)
number = number * 2
1
2
4
8
16
32
64
128
Modules & Modules’ methods
• The highest-level structure of Python
• Each file with the py suffix is a module
• Each module has its own namespace
• Example:
• import mymodule Brings all elements of mymodule in, but must refer to
as mymodule.
• Modules:
• Turtle Module
• Random Module
• Math Module
33
Turtle Module
• turtle is a pre-installed Python library that enables users to create
pictures and shapes by providing them with a virtual canvas. It is very
user friendly and easy to use.
• We use it in our game to generate the game window and the player,
enemies and bullet objects.
• To use it, we must import it at the start of our code using the
command:
34
Examples of turtle methods
• Methods:
• .color
• .shape
• .bgpic
• .penup
• For example:
bullet = turtle.Turtle()
bullet.color("yellow")
bullet.shape("triangle")
bullet.penup()
bullet.speed(0)
35
Random Module
• Python has a built-in module that you can use to create random numbers
called random.
• We will use it to generate a random number for enemy positions in the
game.
• We use this command to import the random module:
• For example:
x = random.randint(-200, 200)
y = random.randint(100, 250)
36
Random Module
Syntax example:
import random
random.random() # returns a float between 0 to 1
random.randint(1, 6) # returns an int between 1 to 6
members = [‘John’, ‘Bob’, ‘Mary’]
leader = random.choice(members) # randomly picks an item
37
Math Module
• Python has a built-in module that you can use for mathematical tasks
called math.
• The math module offers a set of methods and constants that we can use.
Think of functions like cosine, sine, square root and powers. Constants
include the number pi and Euler’s number.
• We use this command to import the math module:
• Example:
distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-
t2.ycor(),2))
38
Basics of math module
• Python has useful commands for performing calculations.
To use many of these commands, you must write the following at the top of your Python
program:
from math import *
39
Object-Oriented Programming
• Everything is an object
• Everything means everything, including functions and classes (more
on this later!)
• Data type is a property of the object and not of the variable
40
Functions are objects
• Can be assigned to a variable
• Can be passed as a parameter
• Can be returned from a function
• Functions are treated like any other variable in Python, the def
statement simply assigns a function to a variable
• Function names are like any variable
• The same reference rules hold for them as for other objects
41
Function as parameters
def foo(f, a) :
return f(a)
def bar(x) :
return x * x
from funcasparam import *
foo(bar, 3)
Output:
9
• Note that the function foo takes
two parameters and applies the
first as a function with the
second as its parameter
42
Functions
def max(x,y) :
if x < y :
return x
else :
return y
43
Global and local variables
• A global variable is a variable declared outside of a function
• Scope: this variable can be used in the entire program
• A local variable us a
variable declared inside
of a function.
• Scope: this variable is used
only in the function it
is declared in.
44
num1= 0
num2= num1+6
c=9
def my_function(a,b):
global c
c=0
c+=1
a=b+c
num3=num1*num2
(num1,num2) are global variables
(num1,num2,num3)
are global variables
(a,b,c) are local variables because
they are inside a defined function
Global c will refer to c variable outside the
function and will change its value to 0
Let’s try to develop an arcade game using
what we’ve learned so far 
45
References
• http://bit.ly/2Gp80s6
• Ms. Fatimah Al-Rashed – Python lab
46

More Related Content

PPTX
12. Exception Handling
Intro C# Book
 
PPTX
09. Methods
Intro C# Book
 
PPTX
05. Conditional Statements
Intro C# Book
 
PPT
Control statements
raksharao
 
PPTX
03 and 04 .Operators, Expressions, working with the console and conditional s...
Intro C# Book
 
PPTX
20.1 Java working with abstraction
Intro C# Book
 
PPTX
09. Java Methods
Intro C# Book
 
PPTX
12. Java Exceptions and error handling
Intro C# Book
 
12. Exception Handling
Intro C# Book
 
09. Methods
Intro C# Book
 
05. Conditional Statements
Intro C# Book
 
Control statements
raksharao
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
Intro C# Book
 
20.1 Java working with abstraction
Intro C# Book
 
09. Java Methods
Intro C# Book
 
12. Java Exceptions and error handling
Intro C# Book
 

What's hot (20)

PPT
conditional statements
James Brotsos
 
PPTX
06.Loops
Intro C# Book
 
PPT
Ch02 primitive-data-definite-loops
James Brotsos
 
PPT
Parameters
James Brotsos
 
PPTX
10. Recursion
Intro C# Book
 
PPTX
02. Data Types and variables
Intro C# Book
 
PPTX
20.5 Java polymorphism
Intro C# Book
 
PPTX
tick cross game
sanobersheir
 
PDF
Acm aleppo cpc training fifth session
Ahmad Bashar Eter
 
PDF
Introduction to python
Marian Marinov
 
PDF
Acm aleppo cpc training introduction 1
Ahmad Bashar Eter
 
PPTX
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
PPTX
02. Primitive Data Types and Variables
Intro C# Book
 
PPTX
19. Data Structures and Algorithm Complexity
Intro C# Book
 
DOCX
Simulado java se 7 programmer
Miguel Vilaca
 
PPTX
Java Tutorial: Part 1. Getting Started
Svetlin Nakov
 
PDF
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...
Udayan Khattry
 
PDF
Cheat Sheet java
arkslideshareacc
 
PDF
The Ring programming language version 1.5.1 book - Part 18 of 180
Mahmoud Samir Fayed
 
DOCX
Python unit 3 and Unit 4
Anandh Arumugakan
 
conditional statements
James Brotsos
 
06.Loops
Intro C# Book
 
Ch02 primitive-data-definite-loops
James Brotsos
 
Parameters
James Brotsos
 
10. Recursion
Intro C# Book
 
02. Data Types and variables
Intro C# Book
 
20.5 Java polymorphism
Intro C# Book
 
tick cross game
sanobersheir
 
Acm aleppo cpc training fifth session
Ahmad Bashar Eter
 
Introduction to python
Marian Marinov
 
Acm aleppo cpc training introduction 1
Ahmad Bashar Eter
 
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
02. Primitive Data Types and Variables
Intro C# Book
 
19. Data Structures and Algorithm Complexity
Intro C# Book
 
Simulado java se 7 programmer
Miguel Vilaca
 
Java Tutorial: Part 1. Getting Started
Svetlin Nakov
 
Oracle Certified Associate (OCA) Java SE 8 Programmer II (1Z0-809) - Practice...
Udayan Khattry
 
Cheat Sheet java
arkslideshareacc
 
The Ring programming language version 1.5.1 book - Part 18 of 180
Mahmoud Samir Fayed
 
Python unit 3 and Unit 4
Anandh Arumugakan
 
Ad

Similar to Building arcade game using python workshop (20)

PPTX
#Code2Create: Python Basics
GDGKuwaitGoogleDevel
 
PPTX
Python Introduction controll structures and conprehansion
ssuser26ff68
 
PDF
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
PPTX
Conditional Statements.pptx
Gautam623648
 
PPTX
Dr.C S Prasanth-Physics ppt.pptx computer
kavitamittal18
 
PPTX
python ppt.pptx
ssuserd10678
 
PPTX
Operators loops conditional and statements
Vladislav Hadzhiyski
 
PPTX
Computer Studies 2013 Curriculum framework 11 Notes ppt.pptx
mbricious
 
PPTX
C Programming with oops Concept and Pointer
Jeyarajs7
 
PDF
Lecture 3 Conditionals, expressions and Variables
Syed Afaq Shah MACS CP
 
PPTX
Review of C programming language.pptx...
SthitaprajnaLenka1
 
PPTX
2. overview of c#
Rohit Rao
 
PPTX
Presgfdjfwwwwwwwwwwqwqeeqentation11.pptx
aarohanpics
 
PDF
Unit 4-Exception Handling in Python.pdf
Harsha Patil
 
PPTX
fundamentals of softweare engeneering and programming in C.pptx
utsavyadav2006
 
PPTX
Python Basics by Akanksha Bali
Akanksha Bali
 
PPTX
python BY ME-2021python anylssis(1).pptx
rekhaaarohan
 
PPTX
Pi j1.3 operators
mcollison
 
PPTX
Going loopy - Introduction to Loops.pptx
Amy Nightingale
 
PPTX
pds first unit module 2 MODULE FOR ppt.pptx
bmit1
 
#Code2Create: Python Basics
GDGKuwaitGoogleDevel
 
Python Introduction controll structures and conprehansion
ssuser26ff68
 
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
Conditional Statements.pptx
Gautam623648
 
Dr.C S Prasanth-Physics ppt.pptx computer
kavitamittal18
 
python ppt.pptx
ssuserd10678
 
Operators loops conditional and statements
Vladislav Hadzhiyski
 
Computer Studies 2013 Curriculum framework 11 Notes ppt.pptx
mbricious
 
C Programming with oops Concept and Pointer
Jeyarajs7
 
Lecture 3 Conditionals, expressions and Variables
Syed Afaq Shah MACS CP
 
Review of C programming language.pptx...
SthitaprajnaLenka1
 
2. overview of c#
Rohit Rao
 
Presgfdjfwwwwwwwwwwqwqeeqentation11.pptx
aarohanpics
 
Unit 4-Exception Handling in Python.pdf
Harsha Patil
 
fundamentals of softweare engeneering and programming in C.pptx
utsavyadav2006
 
Python Basics by Akanksha Bali
Akanksha Bali
 
python BY ME-2021python anylssis(1).pptx
rekhaaarohan
 
Pi j1.3 operators
mcollison
 
Going loopy - Introduction to Loops.pptx
Amy Nightingale
 
pds first unit module 2 MODULE FOR ppt.pptx
bmit1
 
Ad

More from GDGKuwaitGoogleDevel (10)

PDF
معسكر أساسيات البرمجة في لغة بايثون.pdf
GDGKuwaitGoogleDevel
 
PPTX
#Code2 create c++ for beginners
GDGKuwaitGoogleDevel
 
PPTX
i/o extended: Intro to <UX> Design
GDGKuwaitGoogleDevel
 
PDF
#Code2Create:: Introduction to App Development in Flutter with Dart
GDGKuwaitGoogleDevel
 
PDF
Wordpress website development workshop by Seham Abdlnaeem
GDGKuwaitGoogleDevel
 
PPTX
WTM/IWD 202: Introduction to digital accessibility by Dr. Zainab AlMeraj
GDGKuwaitGoogleDevel
 
PPTX
GDG Kuwait - Modern android development
GDGKuwaitGoogleDevel
 
PDF
DevFest Kuwait 2020 - Building (Progressive) Web Apps
GDGKuwaitGoogleDevel
 
PPTX
DevFest Kuwait 2020- Cloud Study Jam: Kubernetes on Google Cloud
GDGKuwaitGoogleDevel
 
PPTX
DevFest Kuwait 2020 - GDG Kuwait
GDGKuwaitGoogleDevel
 
معسكر أساسيات البرمجة في لغة بايثون.pdf
GDGKuwaitGoogleDevel
 
#Code2 create c++ for beginners
GDGKuwaitGoogleDevel
 
i/o extended: Intro to <UX> Design
GDGKuwaitGoogleDevel
 
#Code2Create:: Introduction to App Development in Flutter with Dart
GDGKuwaitGoogleDevel
 
Wordpress website development workshop by Seham Abdlnaeem
GDGKuwaitGoogleDevel
 
WTM/IWD 202: Introduction to digital accessibility by Dr. Zainab AlMeraj
GDGKuwaitGoogleDevel
 
GDG Kuwait - Modern android development
GDGKuwaitGoogleDevel
 
DevFest Kuwait 2020 - Building (Progressive) Web Apps
GDGKuwaitGoogleDevel
 
DevFest Kuwait 2020- Cloud Study Jam: Kubernetes on Google Cloud
GDGKuwaitGoogleDevel
 
DevFest Kuwait 2020 - GDG Kuwait
GDGKuwaitGoogleDevel
 

Recently uploaded (20)

PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 

Building arcade game using python workshop

  • 1. IWD 2021: Building an Arcade Game Using Python Workshop By Eng. Mahdi AlFaraj Mariam AlMahdi 0
  • 2. Before we start learning let’s Download “Python” & “Pycharm” 1
  • 4. For Mac OS Environment 3 1. From Downloads file choose python-3.9.2 file 2. Click continue 3. Click agree. 4. Insert your password then click install.
  • 6. Downloading PyCharm code editor The Python IDE Integrated development environment Website: https://www.jetbrains.com/pycharm/do wnload/#section=mac 5
  • 7. Download PyCharm (Cont.) 1. Click on “Download” 2. Choose “Community” edition click “Download. 6
  • 8. Mac users 3. Go to downloads and click on PyCharm folder. 4. Drag PyCharm icon to Applications folder. 5. This window will appear because it is downloaded from the internet. 7
  • 9. Windows users 3. Click on the downloaded PyCharm folder then click “next”. 4. Click “next”. 5. Check the 64-bit launcher then press ”next”. 8 6. Click ”install”
  • 10. Windows users 7. Click on “finish”. 8. Click “Accept”. 9. Check ”OK”. 9 10. Click ”Don’t send”
  • 11. • Choose the first choice then click next , next, next and next again. • Finally, this is the main page of PyCharm. Click on create new project. 10
  • 13. What are we going to learn? • Basic programming terminologies. • Variables, String Numbers. • print function. • Writing comments. • If/else statement. • Loops. • Range function. • Modules and modules’ methods. • Object-oriented programming. • Functions as parameters. 12
  • 14. Basic Programming terms • code or source code: The sequence of instructions in a program. • syntax: The set of legal structures and commands that can be used in a particular programming language. • output: The messages printed to the user by a program. • console: The text box onto which output is printed. 13
  • 15. Variables • Rules for naming variables in Python: • Variable name cannot be a Python keyword • Variable name cannot contain spaces • First character must be a letter or an underscore • After first character may use letters, digits, or underscores • Variable names are case sensitive • Variable name should reflect its use 14 • Defining a variable: • variable = expression • Example: age = 29
  • 16. Strings • Strings (str) • Must be enclosed in single (‘) or double (“) quote marks • For example: 15 my_string = "This is a double-quoted string." my_string = 'This is a single-quoted string.’ my_string= ‘ This is a “single-quoted” string.’ my_string= “ This is a ‘double-quoted’ string.”
  • 17. Numbers • Numbers in Python can be in a form of: • Integer, for example: Age = 37 • Float, for example: GPA = 3.67 • You can do all of the basic operations with numbers. • + for addition • - for subtraction • * for multiplication • / for division • ** for exponent • () for parenthesis 16 • Example: Output: print(3+4) print(1.6-9.5) print(2**2) print(5.5/2) result= 2+ 3*4 print(result) result= (2+ 3)*4 print(result) 7 -7.9 4 2.75 14 20
  • 18. Variables, Strings, and Numbers • Example: • Calculate the following formula: 𝑎2 + 𝑏3 × 𝑐 where a= 5, b=4, c=8 save the values in variables. 17
  • 19. Comments • Comments: notes of explanation within a program • Ignored by Python interpreter • Intended for a person reading the program’s code • Begin with a # character • Example: • Output # This line is a comment. print("This line is not a comment, it is code.") This line is not a comment, it is code. 18
  • 20. If statement Making Decisions – Controlling Program Flow • To make interesting programs, you must be able to make decisions about data and take different actions based upon those decisions. • if statement: Executes a group of statements only if a certain condition is true. Otherwise, the statements are skipped. • Syntax of the If statement: if condition: Statement Statement • First line known as the if clause • Includes the keyword “if” followed by “condition” • The condition can be true or false • When the if statement executes, the condition is tested, and if it is true the block statements are executed. otherwise, block statements are skipped. 19
  • 21. if/ else statements • The syntax of the if statement is as follows: if boolean expression : STATEMENT STATEMENT • Boolean expression: expression tested by if statement to determine if it is true or false • Example: a > b • true if a is greater than b; false otherwise • Relational operator: determines whether a specific relationship exists between two values • Example: greater than (>) 20
  • 22. if/ else statements • Boolean expressions use relational operators: • Boolean expressions can be combined with logical operators: 21
  • 23. If statement • Example: • Output: • The indented block of code following an if statement is executed if the Boolean expression is true, otherwise it is skipped. 22 gpa = 3.4 if gpa > 2.0: print ("Your application is accepted.”) Your application is accepted.
  • 24. if/else Statements • Dual alternative decision structure: • two possible paths of execution • One is taken if the condition is true, and the other if the condition is False • If you have two mutually exclusive choices and want to guarantee that only one of them is executed, you can use an if/else statement. • The else statement adds a second block of code that is executed if the Boolean expression is False. • Syntax: if condition: statements else: other statements • Rules of if/else statments • if clause and else clause must be aligned • Statements must be consistently indented 23
  • 25. if/else statements • Syntax: if boolean expression : STATEMENT STATEMENT else: STATEMENT STATEMENT 24
  • 26. if/else • if/else statement: Executes one block of statements if a certain condition is True, and a second block of statements if it is False. • Example: 25 gpa = 1.4 if gpa > 2.0: print("Welcome to University!") else: print("Your application is denied.") print(“Good luck”)
  • 27. Loops • A loop statement allows us to execute a statement or group of statements multiple times. • Two types of loops: • for loop: Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. • while loop: Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body. 26
  • 28. range function • The range function specifies a range of integers: range (stop) - the integers between 0 (inclusive) and stop (exclusive) • Syntax: for var in range(stop): statements • Repeats for values 0 (inclusive) to stop (exclusive) for i in range(5): ... print(i) 0 1 2 3 4 27
  • 29. for Loop • for loop: Repeats a set of statements over a group of values. • Syntax: • Rules of loops: • We indent the statements to be repeated with tabs or spaces. • VariableName gives a name to each value, so you can refer to it in the statements. • GroupOfValues can be a range of integers, specified with the range function. • Example: Output: for < var > in <sequence or group of valuse>: <statements> 28 for x in range(1, 6): print( x, ” squared is ", x * x ) 1 squared is 1 2 squared is 4 3 squared is 9 4 squared is 16 5 squared is 25
  • 30. Deference between “for” and “range” function • Example: • Output: • Example: • Output: for x in (1 , 5): print(x," Hello") 1 Hello 5 Hello for x in range(1 , 5): print(x," Hello") 1 Hello 2 Hello 3 Hello 4 Hello 29
  • 31. Loop control statements break Jumps out of the closest enclosing loop continue Jumps to the top of the closest enclosing loop pass Does nothing, empty statement placeholder 30
  • 32. Nested Loop • Loops defined within another loop is called Nested loop. When an outer loop contains an inner loop in its body it is called Nested looping. • Syntax: for <expression>: for <expression>: body • for example: Stops summing at 4 because it is grater than 5 and does not count 5 as an output 2+1 =3 3+1=4 4+1=5 it will give output 4 and ends the output result as 4 31 for a in range (1,5): for b in range (1,a+1): print(a)
  • 33. while • while loop: Executes a group of statements as long as a condition is True. • good for indefinite loops (repeat an unknown number of times) • Syntax: while condition: statements • Example: Output: 32 number = 1 while number < 200: print (number) number = number * 2 1 2 4 8 16 32 64 128
  • 34. Modules & Modules’ methods • The highest-level structure of Python • Each file with the py suffix is a module • Each module has its own namespace • Example: • import mymodule Brings all elements of mymodule in, but must refer to as mymodule. • Modules: • Turtle Module • Random Module • Math Module 33
  • 35. Turtle Module • turtle is a pre-installed Python library that enables users to create pictures and shapes by providing them with a virtual canvas. It is very user friendly and easy to use. • We use it in our game to generate the game window and the player, enemies and bullet objects. • To use it, we must import it at the start of our code using the command: 34
  • 36. Examples of turtle methods • Methods: • .color • .shape • .bgpic • .penup • For example: bullet = turtle.Turtle() bullet.color("yellow") bullet.shape("triangle") bullet.penup() bullet.speed(0) 35
  • 37. Random Module • Python has a built-in module that you can use to create random numbers called random. • We will use it to generate a random number for enemy positions in the game. • We use this command to import the random module: • For example: x = random.randint(-200, 200) y = random.randint(100, 250) 36
  • 38. Random Module Syntax example: import random random.random() # returns a float between 0 to 1 random.randint(1, 6) # returns an int between 1 to 6 members = [‘John’, ‘Bob’, ‘Mary’] leader = random.choice(members) # randomly picks an item 37
  • 39. Math Module • Python has a built-in module that you can use for mathematical tasks called math. • The math module offers a set of methods and constants that we can use. Think of functions like cosine, sine, square root and powers. Constants include the number pi and Euler’s number. • We use this command to import the math module: • Example: distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()- t2.ycor(),2)) 38
  • 40. Basics of math module • Python has useful commands for performing calculations. To use many of these commands, you must write the following at the top of your Python program: from math import * 39
  • 41. Object-Oriented Programming • Everything is an object • Everything means everything, including functions and classes (more on this later!) • Data type is a property of the object and not of the variable 40
  • 42. Functions are objects • Can be assigned to a variable • Can be passed as a parameter • Can be returned from a function • Functions are treated like any other variable in Python, the def statement simply assigns a function to a variable • Function names are like any variable • The same reference rules hold for them as for other objects 41
  • 43. Function as parameters def foo(f, a) : return f(a) def bar(x) : return x * x from funcasparam import * foo(bar, 3) Output: 9 • Note that the function foo takes two parameters and applies the first as a function with the second as its parameter 42
  • 44. Functions def max(x,y) : if x < y : return x else : return y 43
  • 45. Global and local variables • A global variable is a variable declared outside of a function • Scope: this variable can be used in the entire program • A local variable us a variable declared inside of a function. • Scope: this variable is used only in the function it is declared in. 44 num1= 0 num2= num1+6 c=9 def my_function(a,b): global c c=0 c+=1 a=b+c num3=num1*num2 (num1,num2) are global variables (num1,num2,num3) are global variables (a,b,c) are local variables because they are inside a defined function Global c will refer to c variable outside the function and will change its value to 0
  • 46. Let’s try to develop an arcade game using what we’ve learned so far  45