Professional Documents
Culture Documents
Lecture 2
Spring 2023
Values and Data Types
• Strings (str)
• For example;
str(17) → '17'
str(29.75) → '29.75'
• Note that: Remember that when we print a string, the quotes are
removed in the output window.
Variables
x = 12.2 x 12.2
y = 14
y 14
Variables
• Variable names can contain both letters and digits, but they have to
begin with a letter or an underscore.
• Variable names cannot contain illegal character such as the dollar sign.
Reserved Words
A. isGood
B. 4cats
C. _number
D. student id
E. name&surname
F. break
G. score2
Statements
Syntax
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation
% Remainder
Arithmetic Operators
• For example;
• 7 % 3 is 1
• 19 % 5 is 4
Quick Check
28 // 3.0
A. 9
B. 9.0
C. 1
D. 1.0
E. I don’t know.
Operator Precedence Rules
• 26 % 4
• 17 / 5
• 3 + 9 / 6 * 5 - 7
• 11 // 3
• 8 / 2 + 5 * 7 / 5 + 3
Assignment Operators
x = 11
x += 1 x = x + 1
x -= 5 x = x - 5
x *= 2 x = x * 2
x /= 7 x = x / 7
x %= 4 x = x % 4
x = 9
y = 11
x += y - 3 x = x + (y-3)
Print function
print("Hello World!")
>> Hello World!
print('Hello World!')
>> Hello World!
print(15 + 19)
>> 34
x = 7
print("The value of x is", x)
>> The value of x is 7
a = 8
b = 19
print('a =', a, "and b =", b)
>> a = 8 and b = 19
User Input
• We can instruct Python to pause and read data from the user
using the input() function
• For example;
name = input("Enter your name: ")
print("Hello", name)
• For example;
number = input("Enter a number: ")
print("The square of the number:", number**2)
a = 7
b = a + 4
a = 9
print(b)
A. 7
B. 9
C. 11
D. 13
E. I don’t know.
Comments
• It is used to make it easier for the user to understand the written code.
• For example;
# This code calculates area of the circle
# the formula is area = pi*radius*radius
pi = 3.14
r = 5
area = pi*r*r
print("area is", area)
Exercise 1
Write a program that will ask your name and your age and then
print "your_name is your_age years old."
Sample output:
Exercise 1 - Solution
Write a program that will ask your name and your age and then
print "your_name is your_age years old."
Exercise 2
Sample output:
Exercise 2 - Solution