Menentukan Jenis Variabel Show Mungkin ada saat-saat ketika kita ingin menentukan tipe pada variabel. Sebenarnya hal ini bisa dilakukan menggunakan casting. Python adalah bahasa berorientasi objek, dan karena itu ia menggunakan kelas untuk mendefinisikan tipe data, termasuk tipe primitifnya. Karena itu, casting dalam python dilakukan menggunakan fungsi konstruktor:
Contoh: x = int(1) y = int(2.8) z = int("3") print(x) print(y) print(z) Float x = float(1) y = float(2.8) z = float("3") w = float("4.2") print(x) print(y) print(z) print(w) String x = str("s1") y = str(2) z = str(3.0) print(x) print(y) print(z) python program for reverse of a number using type castingIn this tutorial, we are going to learn how to reverse a given number within two lines of code in Python using the type casting method. Table of Contents
How to reverse a number (optimum solution) in PythonBefore jump into the program 1st, you have some knowledge of typecasting in Python. following are the way of typecasting in python: let’s consider two variable x=’123′ (string variable) 1)integer to string: 2)string to an integer: 3)integer to float: In similar way in this program we are going to use type casting . Now move on the program. #take integer as a string input from users. x=input("Please enter a integer ") Now use type cast concept to convert a string because the reverse of number is easier than the reverse of the string so we directly taking a number as a string then reverse them. the reverse of given number which is taken by the user in a string format. #reverse of string instead of integer. y=x[::-1] Now again typecast the string into integer and print it as an output. #type cast from string to integer. y=int(y) print("Reverse of given number = ",y) let’s combine the whole code together : # Reverse of a number in python within very less line of code. #take integer as a string input from users. x=input("Please enter a integer ") #reverse of string instead of integer. y=x[::-1] #type cast from string to integer. y=int(y) print("Reverse of given number = ",y) Output: Please enter a integer 156 Reverse of given number = 651 You may also read:
Write a program to reverse the digits of an integer. Examples : Input : num = 12345 Output: 54321 Input : num = 876 Output: 678 Flowchart: ITERATIVE WAY Algorithm: Input: num (1) Initialize rev_num = 0 (2) Loop while num > 0 (a) Multiply rev_num by 10 and add remainder of num divide by 10 to rev_num rev_num = rev_num*10 + num%10; (b) Divide num by 10 (3) Return rev_num Example: num = 4562 Program: C++
C
Java
Python
C#
PHP
Javascript
Output Reverse of no. is 2654 Time Complexity: O(n), where n is the input number. Auxiliary Space: O(1) RECURSIVE WAY : C++
C
Java
Python3
C#
PHP
Javascript
Time Complexity: O(log(n)) Auxiliary Space: O(log(n)), where n is the input number. Using Recursion, without extra variable C++
Java
Python3
Using String in java We will convert the number to a string using StringBuffer after this, we will reverse that string using the reverse() method corner case
C++
Java
Python3
C#
Javascript
Time Complexity: O(log10n) where n is the input number Auxiliary Space: O(1) Reverse digits of an integer with overflow handled Note that the above program doesn’t consider leading zeroes. For example, for 100 programs will print 1. If you want to print 001 then see this comment from Maheshwar. Using Slicing in Python: C++
Python3
C#
Javascript
Java
Time Complexity: O(n), where n is the input number Auxiliary Space: O(1) Try extensions of above functions that should also work for floating-point numbers. How do you reverse a float number?Convert the float into an integer and devisor model, reverse the integer part using standard number reversing also and then apply devisor.. Eg: 3.14159.. Get decimal location by chceking the number this number is lessthan 3 so exponent can be 10^1.. If a float is 956.123 then exponent would be 10^3.. Reverse the int -> 951413.. How do I reverse a number in Python?Algorithm. Input Integer: number.. (1) Initialize variable revs_number = 0.. (2) Loop while number > 0.. (a) Multiply revs_number by 10 and add remainder of number.. divide by 10 to revs_number.. revs_number = revs_number*10 + number%10;. (b) Divide num by 10.. (3) Return revs_number.. What is __ reversed __ in Python?Python reversed() method The reversed() method returns the reversed iterator of the given sequence. It is the same as the iter() method but in reverse order. Internally, it calls the __reversed__() method of the sequence class. How do you reverse a 4 digit number in Python?In this program, while loop is used to reverse a number as given in the following steps: First, the remainder of the num divided by 10 is stored in the variable digit . Now, the digit contains the last digit of num , i.e. 4. digit is then added to the variable reversed after multiplying it by 10. |