0% found this document useful (0 votes)
22 views20 pages

Week 2

This document provides an overview of the COMP 150 – Intro. to Programming course taught by Dr. Faisal Naeem at the University of the Fraser Valley. It lists the course website and discusses some of the key topics that will be covered, including Python data types like numeric, string, list, and boolean data types. It also provides examples of how to work with these different data types in Python.

Uploaded by

Hi No
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views20 pages

Week 2

This document provides an overview of the COMP 150 – Intro. to Programming course taught by Dr. Faisal Naeem at the University of the Fraser Valley. It lists the course website and discusses some of the key topics that will be covered, including Python data types like numeric, string, list, and boolean data types. It also provides examples of how to work with these different data types in Python.

Uploaded by

Hi No
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

COMP 150 – Intro.

to Programming

Faisal Naeem, PhD


University of the Fraser Valley
Computer Information Systems Department

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:

•Numeric data types: int, float, complex


•String data types: str
•Sequence types: list, tuple, range
•Mapping data type: dict
•Boolean type: bool

2 September 15, 2020 Fall, 2020


Numeric Data Types in Python
:
The numeric data type in Python represents the data that has a numeric value. A numeric value can be an
integer, a floating number, or even a complex number.

•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))

Note – type() function is used to determine the type of data type.


3 September 15, 2020 Fall, 2020
String Data Type

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)

4 September 15, 2020 Fall, 2020


Accessing elements of string
String1 = "GeeksForGeeks"
print("Initial String: ")
print(String1)
print("\nFirst character of String is: ")
print(String1[0])
print("\nLast character of String is: ")
print(String1[-1])

5 September 15, 2020 Fall, 2020


Boolean Data Type

6 September 15, 2020 Fall, 2020


Boolean Data Type

7 September 15, 2020 Fall, 2020


List Data Type
Lists are just like arrays, declared in other languages which is an ordered collection of data. It is
very flexible as the items in a list do not need to be of the same type.
List = []
print("Initial blank List: ")
print(List)
List = ['GeeksForGeeks']
print("\nList with the use of String: ")
print(List)
List = ["Geeks", "For", "Geeks"]
print("\nList containing multiple values: ")
print(List[0])
print(List[2])
List = [['Geeks', 'For'], ['Geeks']]
print("\nMulti-Dimensional List: ")
print(List)

8 September 15, 2020 Fall, 2020


Arithematic Operations

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

# Formula: (a + b) * c # Formula: ((x + y) * (x - y)) / z


result = (a + b) * c result = ((x + y) * (x - y)) / z
print("(a + b) * c =", result) print("((x + y) * (x - y)) / z =", result)

9 September 15, 2020 Fall, 2020


Arithmetic Operations

1 September 15, 2020 Fall, 2020


Operator Precedence Rule

1 September 15, 2020 Fall, 2020


Arithmetic Operations

1 September 15, 2020 Fall, 2020


Numeric Operations

1 September 15, 2020 Fall, 2020


Reading an input from Console

# Get user input # Get a string input from the user


user_input = input("Enter something: ") user_string = input("Enter a string: ")
# Display the input # Display the entered string
print("You entered:", user_input) print("You entered:", user_string)

# Get numerical input from the user


user_number = float(input("Enter a number: "))

# Perform a calculation
result = user_number * 2

# Display the result


print(f"Twice of {user_number} is: {result}")

1 September 15, 2020 Fall, 2020


Class Activity

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.

1 September 15, 2020 Fall, 2020


Test the output of program !

A program that displays the sales tax with two digits after
the decimal point

# Prompt the user for input


purchaseAmount = input("Enter purchase amount: ")
tax = purchaseAmount * 0.06
# Display tax amount with two digits after decimal point
print("Sales tax is",int(tax * 100) / 100)

1 September 15, 2020 Fall, 2020


Class Activity

# Get the purchase amount from the user


purchaseAmount = float(input("Enter purchase amount: "))

# Calculate sales tax (assuming 6% tax rate)


tax = purchaseAmount * 0.06

# Display tax amount with two digits after the decimal point
print("Sales tax is:", format(tax, ".2f"))

1 September 15, 2020 Fall, 2020


Class Activity

1 September 15, 2020 Fall, 2020


Class Activity

(Financial application: payroll) Write a program that reads the following


information and prints a payroll statement:
Employee’s name (e.g., Smith)
Number of hours worked in a week (e.g., 10)
Hourly pay rate (e.g., 9.75)
Federal tax withholding rate (e.g., 20%)
State tax withholding rate (e.g., 9%)

Hint: Calculate gross pay and then find federal and State tax on it individually.
Then deduct it from gross pay:

1 September 15, 2020 Fall, 2020


Questions?

20 September 15, 2020 Fall, 2020

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy