0% found this document useful (0 votes)
36 views7 pages

Muqnit Ur Rehman - Assignment - 1

The document provides examples of Python code for various programming concepts including: 1. Assigning values to variables, taking user input, and printing output. 2. Calculating the volume of a cylinder after getting user input for height and diameter. 3. Playing a guessing game that involves adding numbers and calculating totals. 4. Interpreting sample Python programs for beginners, such as calculating temperature conversion and determining if a number is even or odd.

Uploaded by

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

Muqnit Ur Rehman - Assignment - 1

The document provides examples of Python code for various programming concepts including: 1. Assigning values to variables, taking user input, and printing output. 2. Calculating the volume of a cylinder after getting user input for height and diameter. 3. Playing a guessing game that involves adding numbers and calculating totals. 4. Interpreting sample Python programs for beginners, such as calculating temperature conversion and determining if a number is even or odd.

Uploaded by

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

`

Assignment 01
Fall 2023 PF (due: 09-10-23)
Roll No: BSDSF23wxyz
Name: MUQNIT UR REHMAN

• What is the difference between compilation and interpretation, explain with


example(s)?
Compilation :
Compilation before a program is executed, its whole source code is
converted into machine code or intermediate code through the
compilation process. The compiler produces an executable file or binary
code after checking the code for syntax faults. There is only one
compilation procedure. Unless modifications are made to the source
code, the code can be executed several times after it has been
generated without needing to be recompiled. Because the code has
already been converted into instructions appropriate to a certain
processor, compilation often leads to speedier execution.
Examples:
`

Examples of compiled programming languages include C, C++, and


Rust. When writing a C program, you utilize a compiler to convert your
source code into an executable binary (for example, GCC for C).
Interpretation:
• Without creating a separate built binary, interpretation is the
process of having an interpreter run the source code line-by-line. At
runtime, the interpreter examines the code for errors; if one is found,
execution is terminated. Interpretation, which is frequently connected to
scripting languages, offers greater flexibility since it can dynamically
execute code. Because problems are disclosed when they occur, code
written in interpreted languages is often simpler to debug.
Examples
• Examples of interpreted languages are Python and JavaScript.
Python code is not translated into an executable binary when it is written.
The Python interpreter is used instead, which reads and runs your code
line by line. You will notice error messages at runtime if there are bugs in
your code.
• What is the difference between a compiler and an interpreter, in the context
of translation of a computer programming?

Compiler:
Developers can build programs in high-level languages that are
understandable by people, but compilers can translate such high-level
languages into machine-readable code.
C++, Rust, and other compiled languages are examples.
Interpreter:
Instead of creating a separate executable file, an interpreter interprets and
runs the program line by line or statement by statement. It reads a statement
from the source code, converts it to machine code, runs that statement, and
then moves on to the next statement. This procedure keeps on till the software
is finished.
`

Example of interpreted languages include Python, Ruby, and JavaScript.


I. Provide at least 12 examples of different assignment statements in Python
language, we have discussed in our class sessions.
I. A=(“welcome! ”)
II. x=5
III. pi = 3.14
IV. name = "Alice"
V. is_sunny = True
VI. fruits = ("apple", "banana", "cherry")
VII. coordinates = (3, 4)
VIII. person = ("name": "Ahmad", "age": 30)
IX. sum = 9 + 3
X. greeting = "Hello, " + "world!"
XI. a = 10
XII. b=a

• Provide at least 5 examples of input statements in Python language, each


reasonably different from other, i.e., the difference should be other than
name of variables used?
• input_value = input("Enter a number: ")
• age = int(input("Enter your age: "))
• Seat Type= input("Enter your Seat type(A, B, C, D, or F): ")
• numbers = input("Enter a list of numbers separated by spaces:
").split()
• temperature = float(input("Enter the temperature (in Celsius):
"))
• Describe, without using loops (i.e., only using assignments and output
instructions) how can we print counting from 1 to many thousands.
def print_counting(n):
if n <= 0:
`

return
print(n)
print_counting(n - 1)

print_counting(1000)

• Write a program that interactively get a positive integer from user and if that
number is even number it prints True otherwise prints False.
Z = int(input("Enter a positive integer: "))
if Z < 0:
("Input must be a positive integer.")
if Z % 2 == 0:
print("True")
else:
print("False")
• Write a program that interactively get user’s last name and then first name
and later print them in correct order (i.e., first_name last_name) on one line.
X= input("Enter your first name: ")
Y = input("Enter your last name: ")

Z= (X+Y)
print("Full name:", Z)
`

• Write a program that compute volume of a cylinder after interactively getting


its length (or height) and diameter of its base.
height = float(input("Enter the height of the cylinder: "))
diameter = float(input("Enter the diameter of the cylinder: "))
radius = diameter / 2
volume = 3.14159265359 * (radius ** 2) * height
print("Volume of cylinder is:", volume)
• Write a program that plays a game with you with following interaction.
Hi, guess a number between 50 and 50000 in your mind
Guessed? ___ [enter yes or y]

Good, add same from my side and remember the total


Added? ___ [enter yes or y]

Good, just 30 more and remember the total


Added? ___ [enter yes or y]

Nice, give half of above total as charity, and give my side back to me
Given? __ [enter yes or y]

Hurray‼, let me tell you, you left with 15


Isn’t? __ [enter yes/no or y/n]

Thanks, bye

print("Hi, guess a number between 50 and 50000 in your mind")


user_guess = int(input("Guessed? ___ "))
total = user_guess + user_guess
added = input("Added? ___ ")
`

if added == "yes" or added == "y":


print("Good, just 30 more and remember the total")
total += 30
added = input("Added? ___ ")
if added == "yes" or added == "y":
print("Nice, give half of the above total as charity, and give my side
back to me")
half_of_total = total // 2
given = input("Given? ___ ")
if given == "yes" or given == "y":
print("Hurray‼, let me tell you, you left with 15")
remaining_money = half_of_total - 15
is_correct = input("Isn’t? (yes/no or y/n) ")
if is_correct == "yes" or is_correct == "y":
print("Congratulations! You have won the game!")
else:
print("Sorry, the remaining money amount is incorrect.")
else:
print("Please give the money to charity and give my side back to
me.")
else:
print("Please add 30 to the total and remember the total.")
else:
print("Please add the same number to your guess.")
`

• Search (Google) for PYTHON sample programs for absolute beginners, select
about 5 from them, interpret them in Mu Editor (or somehow) and submit
their code (sequence of instruction).
No1:
print("I am micke")
No2:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = num1 + num2
print("The sum is:", result)
No3:
text = input("Enter a sentence: ")
uppercase_text = text.upper()
print("Uppercase:", uppercase_text)
No4:
fahrenheit = float(input("Enter the temperature in Fahrenheit: "))
celsius = (fahrenheit - 32) * 5/9
print("The temperature in Celsius is:", celsius)
No5:
num = int(input("Enter a number: "))
if num % 2 == 0:
print(f"{num} is an even number.")
else:
print(f"{num} is an odd number.")

You might also like