Python Basics H.W
Python Basics H.W
1 # %% Variables
2 x = 1 # storing integer
3 y = 2.5 # storing real number
4 z = 'string' # storing string or text
5 n = 'a' # storing a character
6 b = True # storing a boolean value
7 print(x,y,z,n,b) # printout on the screen
8 print(type(x))
9 print(type(y))
10 print(type(z))
11 print(type(b))
Output:
int
float
chr
bool
Exercise 2: The following code shows some operations that can be performed using strings.
Input this code in Python’s command editor and report the output in the designated box.
1 # %% Strings
2 course = "computer aided math"
3 print(course.title())
4 print(course.upper())
5 print(course.lower())
6 print(len(course))
7 print(course.find('a'))
8 print(course.replace('a','A'))
9 print(course[0])
10 print(course[-1])
11 print(course[0:14])
12 print('Python' in course)
13 code = "ME0224"
14 print(code + course)
15 print("ME\n0224")
ALFAOURI, MARWA 1
16 print("ME\t0224")
Output:
Computer Aided Math
COMPUTER AIDED MATH
computer aided math
19
10
computer Aided mAth
c
h
computer aided
Flase
ME0224computer aided math
ME
0244
ME 0244
Exercise 3: input() always returns a string. If you want a numeric type, then you need to
convert the string to the appropriate type with the built-in int(), float(), or
complex() function. Input the following code in Python’s command editor and report the
output in the designated box.
1 # %% Inputs
2 number = input('Enter a number: ')
3 print(number + 100)
4 number = int(input('Enter a number: '))
5 print(number + 100)
Output:
TypeError
e.g. the number is 10
result: 110
Exercise 4: In this exercise, you'll learn everything about different types of operators in
Python, their syntax and how to use them with examples. Input this code in Python’s
command editor and report the output in the designated box.
1 # %% Arithmetic Operators
2 x = int(input('Enter a value of x: '))
3 y = int(input('Enter a value of y: '))
4 print('x + y = ', x+y)
5 print('x - y = ', x-y)
6 print('x * y = ', x*y)
7 print('x / y = ', x/y)
8 print('x // y = ', x//y)
9 print('x ** y = ', x**y)
ALFAOURI, MARWA 2
10 # %% Comparison Operators
11 print('x > y is ', x>y)
12 print('x < y is ', x<y)
13 print('x == y is ', x==y)
14 print('x != y is ', x!=y)
15 print('x >= y is ', x>=y)
16 print('x <= y is ', x<=y)
17 # %% Logical Operators
18 a = True
19 b = False
20 print('a and b is ', a and b)
21 print('a or b is ', a or b)
22 print('not a is ', not a)
Output:
ALFAOURI, MARWA 3
Exercise 6: Write a Python program to ask a user their weight (in kilograms), convert it to
pounds and print on terminal.
Code:
10
ALFAOURI, MARWA 4