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)
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)
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...
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...
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 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...
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...
First, the function 2. Parameters Third, the 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...
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...
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 ...
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, ...