Python Programming Language Study Sheet
Python Programming Language Study Sheet
Study Sheet
Design Lesson
Kemal İKİZOĞLU
v1: 25.12.2023
v2:10.03.2024
Table of contents:
Algorithms:......................................................................................................................................... 4
How can we create an algorithm?.....................................................................................................4
Pseudocode:...................................................................................................................................4
Flowchart........................................................................................................................................ 4
Use Python programming language in Information Technology fields:....................................... 5
Python Commands.............................................................................................................................5
print(“value”):.................................................................................................................................. 5
Escape Characters:........................................................................................................................ 5
Math Operators:..................................................................................................................................6
Inputs:..................................................................................................................................................6
Variable:...............................................................................................................................................6
Variable Create Criteria:.....................................................................................................................7
Variable Types:................................................................................................................................... 7
Programming Samples:..................................................................................................................... 7
Calculator:.......................................................................................................................................7
Average Calculator:........................................................................................................................ 8
Triangle’s Perimeter and Area Calculator:...................................................................................... 8
Square’s Perimeter and Area Calculator:....................................................................................... 9
String Tips:..........................................................................................................................................9
Data Transform:................................................................................................................................ 10
IF Statements:...................................................................................................................................10
IF - ELSE Statements:........................................................................................................................ 11
Comparison Operators:...................................................................................................................... 12
IF - ELIF - ELSE Statements:............................................................................................................. 13
Traffic Light Color Example:..........................................................................................................13
Cinema Ticket Example:...............................................................................................................14
Logical Operators:.............................................................................................................................. 15
Students’ Grades Example:.......................................................................................................... 15
Loops:................................................................................................................................................16
For Loop:...................................................................................................................................... 16
Break Command:................................................................................................................................17
Random Python Library:..................................................................................................................18
Guess Game:................................................................................................................................19
While Loop:................................................................................................................................. 20
Infinite Loop:................................................................................................................................. 20
Guess Game:................................................................................................................................22
User Login Example:.................................................................................................................... 22
Online Hate Speech:.........................................................................................................................23
Digital Footprint:...............................................................................................................................23
Strong Password Criteria:............................................................................................................... 23
Which information must be secret on the net?............................................................................. 23
Which information can I share on the net?....................................................................................24
Algorithms:
Computers and mobile devices are simply machines that perform three main tasks. It takes the
entered information (INPUT), processes it (PROCESSING) and outputs a result (OUTPUT) from
this processed data.
The algorithm is a set of rules of obtain the expected output from the given input. Before writing
an application, we write procedures using the logical steps required to solve the problem with an
algorithm. The algorithm shows us all the process steps from start to finish.
Pseudocode:
Pseudocode is an informal way of programming description that does not require any strict
programming language syntax or underlying technology considerations.
Pseudocode Sample:
1- Start (think first step)
2- Need two numbers (get or think)
3- Sum these numbers
4- Write result to screen
5- End
Flowchart
The flowchart is a diagrammatic representation of an algorithm, a step-by-step approach to
solving a task. The flowchart shows the steps as boxes of various kinds and their order by
connecting the boxes with arrows.
Flowchart Sample:
Use Python programming language in Information
Technology fields:
● Artificial Intelligence
● Data Analytics
● Data Visualization
● Programming Applications
● Web Development
● Game Development
● Language Development
● Search Engine Optimization
Python Commands
print(“value”):
Print command writes results on the screen. print (“Hello World”)
Escape Characters:
\n : When you use this command, continuing values are written to the bottom line.
Command: Result:
\t : When you use this command, gives one tab space between values.
Command: Result:
# : Single line comment. When you use in your code, you don't see any result on screen. It’s just
for programmers and you.
Exponentiation(**) x ** y print(2 ** 5) : 32
If you use a different math process inside a single row, you should use common parenthesis in
your code.
Inputs:
When you get ınformation from people, we use this command. input(“description of input”)
Sample:
Variable:
Variable may represent a number, a vector, a matrix, a function, the argument of a function, a
set, or an element of a set. Data that keeps a given value in the computer's memory while
programming and allows us to use it wherever we want is called.
Variable Create Criteria:
● It can be short like x, y or long like first name and surname.
Variable Samples:
● x_=12 ● va riable=10
● alfa=”Loremipsum” ● 9Class=17
● L2= True ● ÇayTime=5
● User_Pass=”120293" ● Sandviç=Monday
● _LHZ = “Car” ● Let’s=Friends
● $letsay=”Friday” ● Name Surname
● Name_Surname
Variable Types:
Integer (int) : The integer variable type uses whole numbers and numbers. 10, 100,
453,10931231123
Float (float) : The float variable type uses decimal numbers. 3.14, 62.5, 10.9
String (str) : The string variable type uses characters, text, sentences. Python, Hello, ENKA,
Boolean: The boolean variable type uses logic mean as True or False. Is this right? True.
Programming Samples:
Calculator:
We are getting value informations from users. We need to use input command here. When we
get each number here we transfer it to a variable for remember each value.
Code: Screen Result:
Average Calculator:
We are getting two exam points and four quiz point values from users. We need to use input
command here. When we get each point value here we transfer to a variable for remember
each value again.
exam1 = int(input("Enter the score for Exam 1: ")) Enter the score for Exam 1: 12
exam2 = int(input("Enter the score for Exam 2: ")) Enter the score for Exam 2: 14
quiz1 = int(input("Enter the score for Quiz 1: ")) Enter the score for Quiz 1: 11
Enter the score for Quiz 2: 22
quiz2 = int(input("Enter the score for Quiz 2: "))
Enter the score for Quiz 3: 33
quiz3 = int(input("Enter the score for Quiz 3: ")) Enter the score for Quiz 4: 44
quiz4 = int(input("Enter the score for Quiz 4: ")) Your average score is 20.25
We are calculating the perimeter and area of a triangle. In this sample, we think that each three
sides of triangle is equal. ( a side = b side = c side)
String Tips:
Merge with plus symbol: Merge with comma symbol:
print(" Square Area : " + str(area) + "m2" ) print("Square's Area : " , area , "m2")
Samples:
Data Transform:
Data transform provides change between data types. If you have a float value, you can convert to integer
value.
Example:
3.14 is the float value. But I need integer value. When I use int(3.14) this value, I can reach an integer value.
This is 3. Reverse is possible too. 8 is an integer value. I need a float value. When I use float(8) this value, I
can reach a float of this value. This is 8.0 .
Integer value: 10. When I use float(10) this value is transforming a float value. Result is 10.0
Float value: 71.2. When I use int(71.2) this value is transforming an integer value.Result is 71.
IF Statements:
In computer programming, we use the if statement to run a block code only when a certain condition
is met. For example, assigning grades (A, B, C) based on marks obtained by a student.
Base: Example
Samples:
Code Result
point= int(input("Please enter lesson point: ")) Please enter lesson point: 90
You passed this lesson.
if point > 85:
print("You passed this lesson.")
IF - ELSE Statements:
The if statement alone tells us that it will execute a block of statements if a condition is true, but not if
the condition is false. However, if we want to do something else in case the condition is false, we
can use the else statement together with the if statement.
Base: Example
Samples:
Code Result
Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Samples:
Code Result
Code Result
Code Result
if value < 20 :
print("Value is less than 20. ")
else:
print(“Try again”)
Code Result
color = input("What is the traffic light color? : ") What is the traffic light color? : red
Car has to stop.
if color == "red":
print("Car has to stop")
else:
print("Unrecognized signal!")
If you write to the program as green, you will get a “Car is allowed to go” message on the result
screen.
Code Result
else:
film = "Wrong Selection!"
print("\nTicket Details:")
print("------------------")
print("Your Name: ", ns)
print("Film Name: ", film)
print("Have fun!")
Logical Operators:
and: This returns True if both x and y are true
Sample: point <=100 and point >= 85
Code Result
point = int(input("Write your exam point: ")) Write your exam point: 75
Grade B
if point <=100 and point >= 85:
print("Grade A")
else:
print("Unrecognized point!")
If you write 90 point the program, you will see "Grade A" message on the screen. If you write K character the
program, you will see "Unrecognized point!" message on the screen.
Loops:
In computer programming, a loop is a sequence of instruction s that is continually repeated until a
certain condition is reached. Typically, a certain process is done, such as getting an item of data and
changing it, and then some condition is checked such as whether a counter has reached a
prescribed number.There are two loop types here: For and While Loop.
For Loop:
A for in loop is used for iterating over a sequence. (That is either a list, a tuple, a dictionary)
Base: Example
Number value might be just a number(10) or between two numbers in the range(10,20).
Code Result
for i in range(5): 1
print(i) 2
3
4
Code Result
50
for i in range(50,60,2): 52
print(i) 54
56
58
Important Note: Range can get three value. First value is start point of number. Second value is end point of
number. Third value is step number.
Code Result
for i in range(50,70,4): 50
print(i) 54
58
62
66
Code Result
sum = 0 1
3
for i in range(1,10): 6
sum = sum + i 10
15
print(sum)
21
28
36
45
My sum value is 0. In each loop, my sum value is summing previous loop numbers.
Break Command:
The break command allows you to terminate and exit a loop.
Code Result
for i in range(10): 0
if i == 4: 1
break 2
print(i) 3
Code Result
for i in range(10): 0
if i % 2 == 0: 2
print(i) 4
6
8
Code Result
for i in range(10): 1
if i % 2 == 1: 3
print(i) 5
7
9
Code Result
import random
random.randrange(number1 , number2)
Code Result
import random 14
s = random.randrange(10,20)
print(s)
Sample: Let’s generate two numbers between 10 - 20 numbers. Multiple these numbers and show
the result on the screen.
Code Result
print(“Number 1 is “, s1)
print(“Number 2 is ”, s2)
print(result)
Guess Game:
Let’s we prepare a guess game with For Loop. Game criterias:
Code Result
if number == guess:
print("Congrats!")
break
else:
print("The number was ", number, ".")
While Loop:
A while loop is used to execute a set of statements as long as a condition is true.
Base: Example
Code Result
Infinite loop!
a=1 Infinite loop!
Infinite loop!
while a == 1: Infinite loop!
print("Infinite loop!") Infinite loop!
Infinite loop!
…….
Code Result
i=1 1
2
while i < 6 : 3
print(i) 4
i += 1 5
Code Result
Code Result
i=1 1
2
while i < 5: 3
print(i) 4
i += 1 Loop completed
print("Loop completed")
Code Result
Guess Game:
Let’s we prepare a guess game with For Loop. Game criterias:
Code Result
number = 5
sum = 0 Enter a number between 0-10: 5
Try again, 5
while True: Your number's sum is 5
value = int(input("Enter a number between 0-10: ")) Enter a number between 0-10: 3
if value == number: Try again, 3
print("You found it!") Your number's sum is 8
break Enter a number between 0-10: 7
sum+=value Try again, 7
print("Try again", value) Your number's sum is 15
print("Your number's sum is ", sum) Enter a number between 0-10: 6
You found it!
In this example, we have username, password information. When user writes a true username and
password, show a message "Access granted!" on the result screen. If just username is wrong, show
a message is "wrong username". If just password is wrong, show "wrong password" message on the
result screen.
Code Result
username = "admin"
password = "piyt10" Enter your username: kemal
attempt = 0 Enter your password! 2024
Access denied! Please enter true
while attempt < 3: username and password!
uname = input("Enter your username: ")
passw = input("Enter your password!") Enter your username: admin
attempt = attempt + 1 Enter your password! 2222
Wrong password!
if uname == username and passw == password :
print("Access granted!\n") Enter your username: admin
break Enter your password! piyt10
elif uname != username and passw == password : Access granted!
print("Wrong username!\n")
elif uname == username and passw != password :
print("Wrong password!\n")
else:
print("Access denied! Please enter true
username and password!\n")
Digital Footprint:
The digital footprint is our data trace consisting of the pages we visit on the internet. For
example, a comment written on Facebook, a like given, or sharing a YouTube video on Twitter,
shooting or watching short videos on Youtube creates our footprint.
Strong Password Criteria:
To create a strong password, the following criteria should be taken into consideration:
● Identity number,
● Home address,
● Birthday, birthplace,
● Mobile number,
● Credit Card number,
● Contracts,
● Your school,
● Your district
● Bank accounts