Professional Documents
Culture Documents
Week 1 Basic Parts 08092023
Week 1 Basic Parts 08092023
Week 1 Basic Parts 08092023
Programming (3621251-3005),
2023
WEEK 1: BASIC PARTS OF A PROGRAM
• 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!’)
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.
Four variables will be created and they will be assigned with the given values
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"
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
>> 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("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
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