Python small exercise --- write a function that accepts an int parameter, judge whether the parameter is a prime number, and return a Boolean value.

tags: Python


#number1 is the function name, def defines a function, a is the parameter required by the function, which is a local variable
#A prime number is a natural number that can only be divisible by 1 and itself, except for 0 and 1
def number1(a):
  if a < 2:
	return False
  elif a == 2:
	return True
  else:
	for i in range(2, a):
		if a % i == 0:
			return False
		else:
			return True
#Need to use input to get a parameter, and it is of type int
a = int(raw_input("please input your number:"))
#The function name is defined as number1, number1(a) can get the result, but you need to add print to output the result
print number1(a)

Boolean values ​​refer to True and False

The python range() function can create a list of integers, generally used in a for loop

range(start, stop[, step]);
start: The counting starts from start. The default is to start from 0. For example, range(5) is equivalent to range(0, 5);
stop: Count to the end of stop, but stop is not included. For example: range(0, 5) is [0, 1, 2, 3, 4] without 5
step: step length, the default is 1. For example: range(0, 5) is equivalent to range(0, 5, 1)

Intelligent Recommendation

The definition of the Python function, the function parameter passes, the return value of the function, the function parameter definition \ default \ variable number parameters

functionIt is possible to complete a piece of code for a specific function. Through the function, you can realize the multiplexing of the code, hide the detail, and improve maintainability and readabi...

Python writes the function Prime(n), for a known positive integer n, judge whether the number is a prime number, if it is a prime number, return True, otherwise return False. Using the above function, write a program to output all twin primes between 1-100 (the difference between twin primes is 2)

Idea: A prime number is a number that only contains two factors of 1 and itself, so the solution is to get all the numbers of this number by looping, and divide it by itself. Set up a detection mechan...

js implements a function that can return whether the input parameter is a prime number (basic interview question 1)

Solving this question does not require advanced front-end knowledge. It mainly examines whether the interviewer’s basic knowledge is solid and logically rigorous (a bit biased algorithm). Topic:...

Write a function to return the number of 1 in the parameter binary

Write a function to return the number of 1 in the parameter binary analysis: (1) Enter a number (2) Determine if it is 0. (3) If it is not 0, it will be modulo 2, and the process of modulo 2 is equiva...

Write a function to return the number of 1s in the parameter binary

public class TestDemo1{ public static int numberOfOne(int num){ int count=0; while(num!=0){ if (num%2==1){ count++; } num/=2; }return count; } } Another simple wording public class TestDemo1{ public s...

More Recommendation

Function Parameter Return Value

  First, the function 2. Parameters Third, the return value...

Function in the parameter return value

1. Return to the Promise instance object The instance object returned will call the next Then 2. Return to normal value The returned normal value will be passed directly to the next THEN, and the valu...

Python judge whether a number is prime

Idea: Enter a number n to determine whether it can be divided by a number between 2 and n-1. If it is possible, the number is not a prime number. (Prime number definition: in natural numbers greater t...

Python function, definition, parameter, return value

In Python, some code with certain functions can be written as a function. The function can reduce the redundancy of the code to a certain extent and save the time for writing the code. Because of the ...

Python function, parameter, variable, return value

Definition: A group of statements that complete a particular function, which can be called multiple times in different places in the program by function name. Function: Reduce programming difficulty, ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top