0% found this document useful (0 votes)
3 views4 pages

Python Basics H.W

The document outlines an in-lab assignment for a course on Computer Aided Math, focusing on Python basics. It includes exercises on variables, string operations, input handling, arithmetic and logical operators, and writing simple programs. Each exercise requires students to input code and report the output, demonstrating their understanding of Python programming concepts.

Uploaded by

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

Python Basics H.W

The document outlines an in-lab assignment for a course on Computer Aided Math, focusing on Python basics. It includes exercises on variables, string operations, input handling, arithmetic and logical operators, and writing simple programs. Each exercise requires students to input code and report the output, demonstrating their understanding of Python programming concepts.

Uploaded by

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

German Jordanian University

School of Applied Technical Sciences


Department of Mechatronics Engineering
ME0224 Computer Aided Math for ME

Yousef Alshattali 20222102005


Student`s Name: ------------------------ ID. No.: ---------------------------------

Experiment 1: Python Basics


In-lab Assignment
Exercise 1: The following code shows how you can use a variable to store values in Python.
Input this code in Python’s command editor and report the output in the designated box.

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:

Exercise 5: Write a Python program to ask a student about:


1. His/her name
2. Major
Then print a message like: “Marwa AlFaouri is a Mechatronics
Engineering student”
Code:

1 # %% Write your code here:

2 name = input("Name: ")


major = input("Major: ")
3
print(name, "is a", major, "student.")
4

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:

1 # %% Write your code here:

2 weight_kg = float(input("Enter your weight in kg: "))


weight_lb = weight_kg * 2.205
3 print("Your weight in pounds:", weight_lb)
4

Exercise 7: Write a Python program to calculate midpoint (x,y) of a line.


Code:

1 # %% Write your code here:

2 x1 = float(input("Enter x1: "))


y1 = float(input("Enter y1: "))
3 x2 = float(input("Enter x2: "))
y2 = float(input("Enter y2: "))
4
5 midx = (x1 + x2) / 2
midy = (y1 + y2) / 2
6
print("Midpoint:", "(", midx, ",", midy, ")")
7

10

ALFAOURI, MARWA 4

You might also like