Computing Programming With Python (W2)
Computing Programming With Python (W2)
CLASS
CONTENTS
Introduction
● Topics:
a. A basic calculator
● Topics: f-strings
● What is f-strings?
● Examples how we can use
f-strings?
● Why we use it better than only
print() function?
Review Quiz
● What is programming?
● Why programming is important?
● What is code?
● What is anaconda?
● What is python?
● What is the difference between compiler and
interpreter?
● What is IDLE?
Solution
1. Largest and Smallest: Generate twenty random integers from 0 to 100 and output the largest and
smallest integers.
2. Even Integer Output: Generate a single random number, from 0 to 100, then output all the even
integers from 0 to the random number.
3. Odd Integer Output: Generate a single random number, from 0 to 100. If the number is greater than
40 then output all the odd integers from 40 to the random number. If the number is less than 40,
output all the odd integers from 0 to 40.
4. Upload to Smart WSU system your results in a .ipynb or .py format
Exercise 1 Exercise 2 Exercise 3
import random
import random
# Generate twenty random import random
# Generate a single random number
integers between 0 and 100
between 0 and 100
random_integers = # Generate a single random
random_number = random.randint(0,
[random.randint(0, 100) for _ number between 0 and 100
100)
in range(20)] random_number =
random.randint(0, 100)
# Check if the random number is
# Output the largest and
greater than 40
smallest integers # Output all the even integers
if random_number > 40:
largest_integer = from 0 up to and including the
# Output all the odd integers
max(random_integers) random number
from 40 up to and including the
smallest_integer = for i in range(0,
random number
min(random_integers) random_number+1, 2):
for i in range(41,
print(i, end=' ')
random_number+1, 2):
print("Random integers:",
print(i, end=' ')
random_integers)
else:
print("Largest integer:",
# Output all the odd integers
largest_integer)
from 0 to 40
print("Smallest integer:",
for i in range(1, 41, 2):
smallest_integer)
print(i, end=' ')
Python Interpreter
● Interpreter languages
provides an interactive
environment
● Results are printed
directly on the screen
Data Types
Name Type Description
# Dictionaries
dict1 = {"name": "John", "age": 30, "city": "New York"}
dict2 = {"apple": 2, "banana": 4, "cherry": 6}
# Tuples
tuple1 = ("apple", "banana", "cherry")
tuple2 = (1, 2, 3, 4, 5)
# Sets
set1 = {"apple", "banana", "cherry"}
set2 = {1, 2, 3, 4, 5}
● 13 + 20 * 2
● 25 * 2 / 5 But we can force a specific order using parentheses
● 30 - 16
● 40 + 5 + 10
● (3 + 4) * 2
Our expressions use arithmetic operators ● 2 * (5 - 1)
● +
● -
● *
● /
● %
● **
Math commands
If we want to perform calculations python has some useful commands or
functions
Function Description
abs(value) Get absolute value
ceil(value) Rounds up a value
cos(value) Return the cosine value in radians
floor(value) Rounds down a value
log(value) Return the logarithm base e
sqrt(value) Square root of a value
round(value) Round a value to the nearest whole number
min(value1, value2) Return the smaller number between two values
max(value1, value2) Return the bigger number between two values
int(value1) Convert a value into an integer
float(value1) Convert a value into a floating point number
Syntax:
Given_name = value1
Examples:
print Syntax:
print “Value1”
print value1
input
a variable
Syntax:
value1 = input(“String of characters”)
str = input()
print(str)
f = open(‘/docs/mydata.txt’,w) x Open a file for exclusive creation. If the file already exists, this operation fails.
print(f.read()) a Open a file in the append mode and add new content at the end of the file.
Q&A
for various applications, including data
analysis, web development, and machine
learning.
● Python has a simple syntax and is easy to
Every question is welcome be learn for beginners.
free to express your thoughts ● Students should choose an IDE or text editor
to write and execute Python code, such as
PyCharm, Visual Studio Code or Anaconda
Gracias!!!
Thank you
감사합니다