0% found this document useful (0 votes)
5 views

Programming - Week 1

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)
5 views

Programming - Week 1

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/ 34

Fundamentals of

Programming
Week 1 - Introduction to
Fundamentals of Programming

23.09.2024 | DOT 1003 | Mahmut Can Kovan


Agenda
● About This Course
● What is Program?
● What is Programming?
● Which programming language(s) suit best for this course?
● Python Basics
○ comments
○ print
○ user input
○ basic arithmetic operations
○ integers, strings, floats
○ f-strings

2
About This Course
● Our Target ● Grading and Exams
○ Basic Programming Principles ○ Midterm (%40)
○ Git Basics ■ 3 assignments
○ Python Basics ■ 1 exam
○ Deploy Game(s) with Python ○ Final (%60)
■ 3 assignments
■ 1 exam

3
Quiz
● What is your expectations for this course?

4
What is “Program”
● Set of commands
● each command tell computer to take some action(s)
● executed line-by-line
● What are the possible actions?

5
What is “Programming”
print("War. War never changes.")
● Creating the “Program” which
computer understands and
execute >War. War never changes.

● If computer doesn’t understand


what you write, your program
does not compile or compile but
print(War. War never changes.)

not work or ?
File "<string>", line 1
print(War. War never
changes.)
^
SyntaxError: invalid syntax

6
First Programming Task
● Write a program which prints this
quote from Fallout:

>Robins will wear their feathery fire,


>Whistling their whims on a low fence-wire;
>And not one will know of the war,
>not one

7
Comments
print("War") #will print War
● You can write a code which print("")
print("War never changes")
compiler ignores #written by Mahmut Can Kovan

● If compiler ignores, why do we


use “comments”? >War
>
>War never changes

8
Print Basic Arithmetic Operations
print(3 + 5)
● You can print basic arithmetic print(10 * 3)
print(2 * 7 + 5)
operations inside the print
command (function, method...)
>8
>30
>19

print("2 * 7 + 5")

>???

9
Programming Task 1
● Please calculate and print these:
○ hours in a week
○ minutes in a week
○ seconds in a week

>Hours in a week:
>168
>Minutes in a week:
>10080
>Seconds in a week:
>604800

10
User Input
name = input("What is your name?")
● any information taken from user print("Hi there, " + name)

is an “input”
● Italic text is user input >What is your name? Heisenberg
>Hi there, Heisenberg
● The word “name” in this program
is a variable.
● What is variable?
○ Storage name for values
○ can be used later name = input("What is your name?")
print("Hi there, " + name + "!")
○ can be changed

>Hi there, Heisenberg!

11
Programming Task 2
● collect user info about discord
and print like below:

>What is your name? Mahmut Can Kovan


>What is your email address? [email protected]
>What is your nickname? mahmut
>
>##Let’s review your information##
>Your name: Mahmut Can Kovan
>Your email address: [email protected]
>Your nickname: mahmut
>
>Mahmut Can Kovan | [email protected] | mahmut

12
Programming Task 3
● ask user for some information
and print this quote

>Name: Mario
>Victim: Princess
>Thank you, Mario!
>But our Princess is in another castle!

13
Variables
● In python, variable names should be look like below:
○ variable = ...
○ variable_name = ...
● Name variables according to what they are used for
● A variable name should begin with a letter, and it can only contain
letters, numbers and underscores _.
● Lowercase and uppercase letters are different characters. The
variables name, Name and NAME are all different variables.
● Use only lowercase characters in variable names.

14
Integers and Strings
number1 = 100
● Integers are used for arithmetic number2 = "100"

operations print(number1)

A string can contain numbers but


print(number2)

you can’t use them for arithmetic >100
>100
operations

number1 = 42
number2 = "42"
print(number1 + number1)
print(number2 + number2)

>84
>4242

15
Integers and Strings
my_num = "42"
● You can use “+” operator with print(my_num / 2)

string to combine strings together TypeError: unsupported operand type(s) for /:


(string concatenation) 'str' and 'int'

● What happens if:


my_calc = 42 * 10
○ “/” operator with string print("The result is " + my_ans)
○ “+” operator with string and integer
TypeError: must be str, not int

my_calc = 42 * 10
print("The result is " + str(my_calc))
print("The result is ", my_calc)

>The result is 420


>The result is 420
16
Printing with f-strings
● another way for formatting text my_calc = 42 * 10
print(f"The result is {my_calc}")
● have more flexibility and control
over what we print out
>The result is 420

● f-strings may not work if you use


older version of python

17
Programming Task 4
● convert this code to f-strings
● remove unnecessary spaces

name = "Courier"
age = 34
city = "New Vegas"
print("Hi", name, ", you are", age, "years old. You live in", city, ".")
#f-strings alternative

>Hi Courier , you are 34 years old. You live in New Vegas .
>Hi Courier, you are 34 years old. You live in New Vegas.

18
Programming Task 5
● convert this code to f-strings >My name is Ozan Akyol, I am 18 years old
● The program should give exactly the >
>My skills are
output on the side: > - python (beginner)
> - 2d art (beginner)
name = "Ozan Akyol" >
age = 18
>My salary expectation is 2000-3000 euros/month
skill1 = "python"
level1 = "beginner"
skill2 = "2d art"
level2 = "beginner"
lower = 2000
upper = 3000

print("my name is ", name, " , I am ", age, "years old")


print("my skills are")
print("- ", skill1, " (", level1, ")")
print("- ", skill2, " (", level2, ")")
print("My salary expectation is", lower, "-", upper, "euros/month")

19
Floating Point
● refers to numbers with a decimal num1 = 1
num2 = 12.5
point num3 = 4.2
ans = (num1 + num2 + num3) / 3
● can be used much in the same print(f"The result is {ans}")

way as integer >The result is 5.8999999999999995

20
Programming Task 6
● use two number and make these
calculation and print like below
● numbers should store in the
variable

>number 1 is: 3
>number 2 is: 5
>
>3 + 5 = 8
>3 - 5 = -2
>3 * 5 = 15
>3 / 5 = 0.6

21
Arithmetic Operations
● most common arithmetic operators in Python

Operator Description Example and Result

+ Addition 12 + 0.5 = 12.5

- Subtraction 6 - 3 = 3

* Multiplication 15 * 20 = 300

/ Division(Floating point) 7 / 2 = 3.5

// Division(Integer) 7 // 2 = 3

% Modulo 7 % 2 = 1

** Expoentiation 2 ** 8 = 256
22
User Input as an Integer
input_str = input("Which year were you born? ")
● We can cast string as integer year = int(input_str)
print(f"Your age is: {2024 - year}" )
using int() function
● This basic function convert string >Which year were you born? 1990
to integer >Your age is 34

23
Programming Task 7
● Please create a program which calculate user’s Body Mass Index

>Enter your weight: 73


>Enter your height: 173
>
>Your BMI is 24.725182932941294

24
Programming Task 8
● Please create a program which calculate the game’s age like below:

>Game name: Half-Life


>Which year was this game released? 1998
>
>Half-Life is 26 years old.

25
Programming Task 9
● Please fix the code below
# Fix the code >Please type in the first number: 3
number = int(input("Please type in the first number: ")) >Please type in the second number: 4
number = int(input("Please type in the second number: ")) >Please type in the third number: 5
number = int(input("Please type in the third number: ")) The product is 60
product = number * number * number

print("The product is", product)

26
Programming Task 10
● Please write a program which calculate the food expenses of a
student.

>How many times a week do you eat at the student cafeteria? 5


>The price of a typical student lunch? 25
>How much money do you spend on groceries in a week? 300
>
>Average food expenditure:
>Daily: 60.714285714285715 liras
>Weekly: 425 liras
>Montly: 1821.4285714285716

27
Programming Terminology
Statement print("Hi!")

● A statement is a part of the number = 42

program which executes


something. if name == "Anna":
print("Hi!")
● It often, but not always, refers to number = 2

a single command.

28
Programming Terminology
Block if age > 17:
# beginning of the conditional block
print("You are of age!")
● A block is a group of consecutive age = age + 1
print("You are now one year older...")
statements # end of the conditional block

● at the same level in the structure print("This here belongs to another block")

of the program
● In Python blocks are expressed by
indenting all code in the block by
the same amount of whitespace.

29
Programming Terminology
# the variable x is assigned the value of the
Expression #expression 1 + 2
x = 1 + 2
● a bit of code that results in a
determined data type # the variable y is assigned the value of the
#expression '3 times x plus x squared'
y = 3 * x + x**2

Expression Value Type [Python Data Type]

2+10+30 42 integer [int]

"abc"+"de" "abcde" string [str]

11/2 5.5 float number [float]

2*5>9 True Boolean Value [bool]


30
Programming Terminology
Function print("this is an argument")

● a special code parts which take name = input("Please type in your name: ")

argument(s)
● return something or do
something
● executed when it is called

31
Programming Terminology
Syntax if name == "Gordon Freeman"
print("Rise and Shine Mr. Freeman")

● determines how the code of a


program should be written. File "test.py", line 1
● Each programming language has if name == "Gordon Freeman"
^
its own specific syntax. SyntaxError: invalid syntax

Example
the first line of an if statement should end in a colon
character, and the block of the statement should be
indented:

32
Programming Terminology
Debugging x = 10
y = 0
result = x / y
● if the syntax is true but program print(f"{x} divided by {y} is {result}")

still doesn’t work as intended ZeroDivisionError: integer division or


there is a bug in the program modulo by zero on line 3

● bugs can cause


○ stop the execution name = "Gordon Freeman"
○ normal execution but your program if name != "Gordon Freeman":
can’t solve the problem print("Rise and Shine Mr. Freeman")

>

33
End of the Week
Thanks Materials
[email protected]
● Think Python, 2nd Edition, How to Think
Like a Computer Scientist, Allen Downey,
Green Tea Press (2015)
● www.freecodecamp.com
Homework(s) ● www.geeksforgeeks.com
● null

34

You might also like