PYTHON 3.0 HELP!! NOT JAVA -Create a list consisting of the negative values in t
ID: 3849495 • Letter: P
Question
PYTHON 3.0 HELP!! NOT JAVA
-Create a list consisting of the negative values in the list numbers. Associate the new list with the variable negatives.
-The factors of an integer are those numbers that evenly divide into the integer. Given the integer n, create a list of all the factors of n, excluding 1and n itself. The list should be ordered from smallest to largest. Associate the new list with the variable factors.
-The factors of an integer are those numbers that evenly divide into the integer. Given the integer n, create a list of all the factors of n, excluding 1and n itself. Associate the new list with the variable factors. The list should be ordered from largest to smallest.
Explanation / Answer
NOTE:
Out of three questions provided, i did not clearly understand the first questoin. So has written two solutions for first part. If you have any questions or concerns please let me know.
Code-1:
#!/usr/local/bin/python3
# Create a list consisting of the negative values in the list numbers. Associate the new list with the variable negatives.
def main():
negatives = []
i = -1
# creating a list of 100 negative numbers
while i != -100:
negatives.append(i)
# incrementing negative numbers
i -= 1
print(negatives)
if __name__=='__main__':
main()
Execution and output:
Unix Terminal> python3 negatives.py
[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, -19, -20, -21, -22, -23, -24, -25, -26, -27, -28, -29, -30, -31, -32, -33, -34, -35, -36, -37, -38, -39, -40, -41, -42, -43, -44, -45, -46, -47, -48, -49, -50, -51, -52, -53, -54, -55, -56, -57, -58, -59, -60, -61, -62, -63, -64, -65, -66, -67, -68, -69, -70, -71, -72, -73, -74, -75, -76, -77, -78, -79, -80, -81, -82, -83, -84, -85, -86, -87, -88, -89, -90, -91, -92, -93, -94, -95, -96, -97, -98, -99]
Code-1:
#!/usr/local/bin/python3
# Create a list consisting of the negative values in the list numbers. Associate the new list with the variable negatives.
def main():
negatives = [-1, -10, -20, -30, -40, -50]
print(negatives)
if __name__=='__main__':
main()
Execution and output:
Unix Terminal> python3 negatives_1.py
[-1, -10, -20, -30, -40, -50]
Unix Terminal>
Code-2:
#!/usr/local/bin/python3
# The factors of an integer are those numbers that evenly divide into the integer.
# Given the integer n, create a list of all the factors of n, excluding 1 and n itself.
# The list should be ordered from smallest to largest. Associate the new list with the variable factors.
def main():
# initializing an empty list factors
factors = []
# taking input from the user
n = int(input('Enter the integer n? '))
# iterating from numbers 2 to n-1
for i in range(2, n):
# checking if n is divisible by i. If it is then append to the factors list
if n % i == 0:
factors.append(i)
# sorting the numbers from smallest to largest
factors.sort()
print('Factors of the number excluding 1 and n are: ')
# printing the factors of number
for num in factors:
print(num, end=' ')
print(' ')
if __name__=='__main__':
main()
Execution and output:
Unix Terminal> python3 factors.py
Enter the integer n? 40
Factors of the number excluding 1 and n are:
2 4 5 8 10 20
Unix Terminal> python3 factors.py
Enter the integer n? 100
Factors of the number excluding 1 and n are:
2 4 5 10 20 25 50
Unix Terminal> python3 factors.py
Enter the integer n? 900
Factors of the number excluding 1 and n are:
2 3 4 5 6 9 10 12 15 18 20 25 30 36 45 50 60 75 90 100 150 180 225 300 450
Unix Terminal> python3 factors.py
Enter the integer n? 39
Factors of the number excluding 1 and n are:
3 13
Code-3:
#!/usr/local/bin/python3
# The factors of an integer are those numbers that evenly divide into the integer.
# Given the integer n, create a list of all the factors of n, excluding 1 and n itself.
# The list should be ordered from smallest to largest. Associate the new list with the variable factors.
def main():
# initializing an empty list factors
factors = []
# taking input from the user
n = int(input('Enter the integer n? '))
# iterating from numbers 2 to n-1
for i in range(2, n):
# checking if n is divisible by i. If it is then append to the factors list
if n % i == 0:
factors.append(i)
# sorting the numbers from largest to smallest
factors.sort(reverse=True)
print('Factors of the number excluding 1 and n are: ')
# printing the factors of number
for num in factors:
print(num, end=' ')
print(' ')
if __name__=='__main__':
main()
Execution and output:
Unix Terminal> python3 factors_2.py
Enter the integer n? 90
Factors of the number excluding 1 and n are:
45 30 18 15 10 9 6 5 3 2
Unix Terminal> python3 factors_2.py
Enter the integer n? 15
Factors of the number excluding 1 and n are:
5 3
Unix Terminal> python3 factors_2.py
Enter the integer n? 900
Factors of the number excluding 1 and n are:
450 300 225 180 150 100 90 75 60 50 45 36 30 25 20 18 15 12 10 9 6 5 4 3 2
Unix Terminal>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.