Hướng dẫn print square in python
Show
Nội dung chính
In Mathematics, a square is a result of multiplying a number by itself. That multiplication is done just once, like so: n x n. That makes squaring the same as raising the number to the power of 2. For example, 8×8 is 64, just as 8 squared (82) is 64. One feature of squares is that they are always positive. This is because of the negative times. Another negative always gives the positive. For instance, -8 squared, or (-8)2, is also 64. The square of a number is that number multiplied by itself. To calculate the square of a number in Python, we have three different ways.
Python square: Using multiplying two timesTo find a square of the number, multiply the number by itself. This method is the easiest way to calculate squares in Python. # input a number digit = int(input("Enter an integer number: ")) # calculate square square = digit*digit # print print(f"Square of {digit} is {square}") OutputEnter an integer number: 10 Square of 10 is 100 In this example, we have used the python input method to get the input from the user and then calculate the square based on multiplying by itself. Square a number using an Exponent operator.We can find the square of a given number is to use Exponent Operator (**); it returns the exponential power. So this operator is represented by **. # input a number digit = int(input("Enter an integer number: ")) # calculate square using exponent operator square = digit**2 # print print(f"Square of {digit} is {square}") OutputEnter an integer number: 11 Square of 11 is 121 We have calculated a square using an exponent operator(**) in this example. Also, Statement x**y will be calculated as “x to the power of y”. Using math.pow() method to calculate squareMath.pow(x, y) is a built-in method of math library, and it returns the value of “x to the power y“. To use the pow() method, we need to import the math library in the program and then import the pow function from the math module. from math import pow # input a number digit = int(input("Enter an integer number: ")) # calculate square square = int(pow(digit, 2)) # print print(f"Square of {digit} is {square}") OutputEnter an integer number: 19 Square of 19 is 361 Math.pow() function raises some value to absolute power. Its first argument is a number we want to raise; the second argument is an exponent. When we square with the pow() method, our second argument is always 2. The input() function will return the string, so we have converted the string to int. Then we have used math.pow() method to get the square of the number. Find the Square of Python list.We can get the squares of list values using list comprehension. numbers = [11, 21, 19, 30, 46] squaredValues = [number ** 2 for number in numbers] # print print('Original Values: ', numbers) print('Squared Values: ', squaredValues) OutputOriginal Values: [11, 21, 19, 30, 46] Squared Values: [121, 441, 361, 900, 2116] This example first makes a list of named numbers. Its contents are various integer values. Then we generate a new list with a list comprehension. The code between square brackets ([ and ]) squares each number value with the exponent (**) operator. Those number values are generated by an in-line for loop expression: for the number in numbers. This goes through our original numbers list and makes each element available as the number variable, one at a time. After that list comprehension, the squared list has each value squared. We then output the original and squared values with the Python print() function. We could also get the square values differently. For instance, with simple multiplication, you can achieve your result. See the following code. numbers = [11, 21, 19, 30, 46] squaredValues = [number * number for number in numbers] # print print('Original Values: ', numbers) print('Squared Values: ', squaredValues) OutputOriginal Values: [11, 21, 19, 30, 46] Squared Values: [121, 441, 361, 900, 2116] In this example, we have defined a Python list and then used list comprehension to create the square of list items. Python square of the arrayTo find the square of an array, we need to use the Numpy library. The Numpy square() method in Python is a mathematical function to calculate the square value of each element in the array. import numpy as np arr = np.array([11, 19, 21, 29, 46]) print("Square Value of arr1 : \n", np.square(arr)) print("\n") arr2 = [-19, -21] print("Square Value of arr2 : \n", np.square(arr2)) OutputSquare Value of arr1 : [ 121 361 441 841 2116] Square Value of arr2 : [361 441] In this example, we have imported numpy as np and then created an array using the np.array() method. Then used, the np.square() method to get the square value of every array element. ConclusionIn this tutorial, we have seen how to calculate the square of a number using the exponent operator, multiply by itself, and math.pow() function. Then we have seen how to find the square of each element of the list using list comprehension. That’s it for this tutorial. See alsoPython sqrt() Python sum() Python Modulo operator What does square () do in python?Square Roots in Mathematics The Python ** operator is used for calculating the power of a number. In this case, 5 squared, or 5 to the power of 2, is 25. The square root, then, is the number n, which when multiplied by itself yields the square, x. In this example, n, the square root, is 5. How do you make a square pattern in python?Pattern - 9: Square Pattern using with number. rows = int(input("Enter the number of rows: ")). for i in range(1, rows + 1):. for j in range(1, rows + 1):. # Check condition if value of j is smaller or equal than.. # the j then print i otherwise print j.. if j <= i:. print(i, end=' '). What is the symbol for square in python?The square root symbol or square root sign is a mathematical symbol, denoted by '√'. This symbol is known as radical, in words. ... Root Symbol (in Word). |