Week 2
Week 2
to Programming
http://www.ufv.ca/cis/
January 4, 2024
Python Data Types:
There are different types of data types in Python. Some built-in Python data types are:
•Integers – This value is represented by int class. It contains positive or negative whole numbers (without
fractions or decimals). In Python, there is no limit to how long an integer value can be.
a =5
print("Type of a: ", type(a))
•Float – This value is represented by the float class. It is a real number with a floating-point representation.
It is specified by a decimal point.
b = 5.0
print("\nType of b: ", type(b))
•Complex Numbers – A complex number is represented by a complex class. It is specified as (real part) +
(imaginary part)j. For example – 2+3j
c = 2 + 4j
print("\nType of c: ", type(c))
Strings in Python are arrays of bytes representing Unicode characters. A string is a collection
of one or more characters put in a single quote, double-quote, or triple-quote. In Python
there is no character data type, a character is a string of length one. It is represented by str
class.
Creating String
Strings in Python can be created using single quotes, double quotes, or even triple quotes .
String1 = 'Welcome to the Geeks World'
String with the use of Single Quotes:
print("String with the use of Single Quotes: ")
Welcome to the Geeks World
print(String1)
String with the use of Double Quotes:
I'm a Geek
String1 = "I'm a Geek" <class 'str'>
print("\nString with the use of Double Quotes: ") String with the use of Triple Quotes:
print(String1) I'm a Geek and I live in a world of "Geeks"
print(type(String1)) <class 'str'>
String1 = '''Geeks Creating a multiline String:
For Geeks
Life''' For
print("\nCreating a multiline String: ") Life
print(String1)
Program 1: Program 2:
a=5 a = 10
b=3 b=4
sum_result = a + b difference_result = a - b
print("Sum:", sum_result) print("Difference:", difference_result)
Program 3: Program 4:
# Arithmetic expression with parentheses # Arithmetic expression with parentheses
a=5 x=7
b=3 y=4
c=2 z=2
# Perform a calculation
result = user_number * 2
Activity 1:
Write a program to find the area of circle A=2*pie*r^2. In the first program define the value of r.
Activity 2:
Write a program to find the area of circle A=2*pie*r^2. In the second program user will input or enter the value
of r.
A program that displays the sales tax with two digits after
the decimal point
# Display tax amount with two digits after the decimal point
print("Sales tax is:", format(tax, ".2f"))
Hint: Calculate gross pay and then find federal and State tax on it individually.
Then deduct it from gross pay: