Week 1 Basic Parts 08092023

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Programming I - Elementary

Programming (3621251-3005),
2023
WEEK 1: BASIC PARTS OF A PROGRAM

MIKKO APIOLA 8.9.2023


ANNOUNCEMENTS
• DISCORD-SERVER IS UP, LINK IN MOODLE
• NEXT WEEK EXERCISE GROUPS IN KARELIA R3 AND R4 WILL BE IN ZOOM.
GROUP R1 (UEF) WILL BE ON CAMPUS. AFTER NEXT WEEK, WE WILL SEE …
• ENTRANCE EXAM. WAITING TO BE CONFIRMED (THE PROGRAMMING-
PART OF THE ENTRANCE EXAM WILL COVER 2ECTS. SO THOSE WHO HAVE
COMPLETED IT, YOU DO NOT HAVE TO ATTEND ANY TEACHING IN THE
FIRST THREE WEEKS, BUT YOU ARE FREE TO ATTEND IF YOU WANT
• EXAM DATES: WAITING FOR THE INFO. BUT THERE WILL BE SEVERAL
OPTIONS.
• Q: SHOULD WE HAVE A 5-10 MIN BREAK IN THE LECTURES?
CONTENTS (WEEK 1)
• DESIGNING A PROGRAM
• PRINT – FUNCTION
• COMMENTS
• INTRO TO VARIABLES
• BASIC ARITHMETICS
• STRING OPERATIONS

• WORKING IN A PROGRAMMING ENVIRONMENT


• WORKING ON THE COURSE PLATFORM
DESIGNING A PROGRAM
• IDEA? WHAT IS THE PROGRAM SUPPOSED TO DO?
• MAKE A PLAN
• WRITE CODE
• CORRECT SYNTAX ERRORS
• TEST THE PROGRAM
• CORRECT LOGICAL ERRORS (DEBUG)
• (REPEAT)

• WORKING IN A PROGRAMMING ENVIRONMENT (VSCODE, IDLE, …) GADDIS 2.1


PRINT-FUNCTION (OUTPUT)
• PRINT – FUNCTION CAN BE USED TO PRINT OUTPUT

• WHEN WE CALL THE PRINT-FUNCTION, WE PASS IT THE VALUE THAT WE WANT


TO PRINT AS AN ARGUMENT

print("Hello everyone and welcome!")

• TASK: LET’S DESIGN A PROGRAM THAT PRINTS ONE’S NAME AND ADDRESS

GADDIS 2.1
PRINTING STRINGS
• IN PROGRAMMING, A SEQUENCE OF CHARACTERS IS CALLED A STRING

print(”This is a string!")
print(’This is a string!’)

• PRINTING SPECIAL CHARACTERS, SUCH AS ’ and ”

print(”Robert's wine")
print("Albert's \”coffee\"")

(Most of the time there are many correct ways to do things!) GADDIS 2.1
COMMENTS
• COMMENTS ARE NOTES OR EXPLANATION FOR THE PROGRAMMERS.
THEY WILL BE IGNORED BY THE PYTHON INTERPRETER.

Comments begin with #

# This program will print some text


#
print("Some text")
VARIABLES
• VARIABLES ARE USED TO STORE INFORMATION IN COMPUTER’S MEMORY

• COMMON VARIABLE TYPES


year = 2023 # integer type
city = ”Joensuu” # String type
temp = 45.7 # floating point type (decimal number)
hot = True # boolean type (can only be True or False)

Four variables will be created and they will be assigned with the given values

= is called an assignment operator


GADDIS 2.1
VARIABLES
NAMING OF VARIABLES
* Can not be a Python keyword
* Cannot contain spaces
* First character must be letter
* Only letters, digits, underscores allowed

Allowed
Not Allowed
abcd1234 = 5
for = 5
Variable_3 = ”Hey” area of house = 100 GADDIS 2.1
Raining = False 123hello = True
vari*able3 = 1
VARIABLES, EXAMPLES
height = 5
width = 10
first_name = "Robert"
last_name = "Noname"

area = height * width


height = height + 1

fullname = first_name + " " + last_name

print(area)
print(height)
GADDIS 2.5
print(fullname)
BASIC ARITHMETICS
x = 5
y = 6
z = x*y
p = z**2
x = p - 1
result = ((1+2+x+z)*4)/2
print(result)

GADDIS 2.7
STRING CONCATENATION
first_name = "Sam”
last_name = "Sammy”
name = first_name + " " + last_name
print(name)

GADDIS 2.8
SEP-PARAMETER IN PRINT
MULTIPLE ARGUMENTS IN PRINT WILL BE SEPARATED BY SPACE

>> a="a"
>> b="b"
>> c="c"
>> print(a,b,c)
a b c

IT IS POSSIBLE TO CHANGE THIS:

>> print(a,b,c,sep="**")
a**b**c GADDIS 2.9
END-PARAMETER IN PRINT
>> print("Message One")
>> print("Message Two")
>> print("Message Three")
Message One
Message Two
Message Three

>> print("Message One",end=",")


>> print("Message Two",end=",")
>> print("Message Three")
Message One,Message Two,Message Three GADDIS 2.9
ESCAPE CHARACTERS
\n Causes a line change
\t Causes a horizontal tabulation
\’ A single quotation mark
\” A double quotation mark
\\ A backslash character

>> print("One\n\tTwo\n\t\tThree")
One
Two
Three GADDIS 2.9
STRING FORMATTING
>> x = format(5.249, '.2f') # round to 2 decimals
>> print(x)
5.25

... and many more

GADDIS 2.10
String Formatting: F-strings
name = "Pekka"
print(f"Hello {name}")
Hello Pekka

temp = 25.12551235
print(f"The temperature is {temp:.2f}")
The temperature is 25.13

GADDIS 2.10
EXERCISES
- IN THE ELEARN-ENVIRONMENT

- TO BE COMPLETED BY MONDAY 11.9 (23:59)

- THEN DISCUSSED ON WEEK 37 GROUP EXERCISE SESSION

- GUIDANCE AVAILABLE ON MONDAY 11.9

You might also like