Questions
Q1:-How to create a comment in Python?
ans:-
1)Single Line Comment-
'#' is used to create a single line comment in
python.
e.g:-
# This is Single Line Comment.
Anything written here is ignored by Python
print("Hello Students!")
2) Multi-Line Comment:-
We can add a multiline comment in python code using
triple quotes
e.g:-
'''
This is a
Multi-Line Comment.
Anything written
here is ignored by Python
'''
print("Hello Students!")
Q2:-way to obtain user input from command line
ans:-
To take input from the user from command line we make
use of a built-in function input().
e.g1
#accept command line input
num=input();
print(num);
Q3:-List standard mathematical operators in python and explain
them
ans:-
i)Addition(+):
adds two operands
Syntax:-a + b
ii)Subtraction(-):
subtracts two operands
Syntax:-a - b
iii)Multiplication(*):
multiplies two operands
Syntax:-a * b
iv)Division -float(/):
divides the first operand by the
second
Syntax:-a / b
v)Division-floor(//):
divides the first operand by the
second
Syntax:-a // b
vi)Modulus(%):
returns the remainder when first
operand is divided by the second
Syntax:-a % b
Q5:- Explain while loop. Give example
ans:-
Repeats a block of statements while a given condition
is TRUE.
Syntax:-
while (Condition):
#block of statements
e.g
#Program to find factorial using
while
print('Enter num to find
factorial:-');
num=input();
i=1
fact=1;
while i < num:
i=i+1;
fact=fact*i
print('Factorial:-');
print(fact);
Questions 1. How to create a comment in python? 2. The way to obtain user input...
python #Ignore line 2, it is just there to align with zybooks requirements print("5") #Create a list with the following numbers and assign it to a variable name: 1,2,3,4,5,6 #Convert the list to a tuple and assign it to a variable name (dirrerent from the list's name) #Try the following and press the run program button to which ones work and which ones cause errors #If a statement causes an error, use a # to comment it out #Add the...
In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...
Hello can anyone help solve this please in Python the way it's asked? Question 22 40 pts List Comprehensions Write the solution to a file named p2.py and upload it here. Comprehensions will show up at the midterm exam, so it's important to practice. a) Write a list comprehension that returns a list [0, 1,4, 9, 16, .. 625] starting from range(0, 26). (lots of list elements were omitted in ...) b) Write a list comprehension that returns the list...
This script will create a dictionary whose keys are all the directories listed in thePATH system variable, and whose values are the number of files in each of these directories. The script will also print each directory entry sorted by directory name. The script will use the following five functions to get the required results get_environment_variable_value() get_dirs_from_path() get_file_count() get_file_count_for_dir_list() print_sorted_dictionary() get_environment_variable_value() The header for this function must be This function must accept a shell variable as its only parameter The...
1. Pleae write the following code in Python 3. Also, please show all the outputs. . Write a simple Rectangle class. It should do the following: Accepts length and width as parameters when creating a new instance Has a perimeter method that returns the perimeter of the rectangle Has an area method that returns the area of the rectangle Don't worry about coordinates or negative values, etc. Python provides several modules to allow you to easily extend some of the...
1) We have returned to our ingredients dictionary. We want to display the ingredients in alphabetical order. Dictionaries cannot be sorted, so we need to use the items method to convert the dictionary into a list of tuples. Then we can sort that new list of tuples, then display the ingredients. Use items and list to convert ingredients into a list of tuples. Sort the new list, called ingred_list Write a for loop which goes through each tuple in ingred_list....
Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...
True or False: Python programs require two or more modules. 1. 2. The keyword while means the same thing as While in Python. 3. In Python, _MyVar15 is a valid variable name. 4. To make your program more secure, use obscure variable names such as xz14dEE 5. Indenting code that should be executed when an if statement evaluates as true makes your program easier to read, but the indentation is not necessary. 6. A dictionary object contains zero or more...
Help needed related python task ! Thanx again How you're doing it • Write a function write_to_file() that accepts a tuple to be added to the end of a file o Open the file for appending (name your file 'student_info.txt') o Write the tuple on one line (include any newline characters necessary) o Close the file • Write a function get_student_info() that o Accepts an argument for a student name o Prompts the user to input as many test scores...
CPT 180 Chapter 2 Assignment 3 Directions Using the following guidelines, create a python program. 1. Create a program named printRandomNumbers that gets input from the user and then produces a list of random numbers. 2. Import the random module 3. Add three comment lines at the top of the program that contain: a. Program Name b. Program Description c. Programmer's Name (You) 4. Prompt the user for the following: a. Number of random numbers required b. Lower limit of...