Difference between revisions of "Conversions - Python"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Data Type Conversions)
(Data Type Conversions)
Line 17: Line 17:
 
[[File:Conversions in python.png|600px]]
 
[[File:Conversions in python.png|600px]]
  
In the previous section [[Variables - Python |Variables]] you needed to convert the values entered by the user to an integer in this way.
+
In the previous section [[Variables - Python |Variables]] you needed to convert the values entered by the user to an integer in this way. You may also need to convert in the opposite way:
 +
 
 +
<syntaxhighlight lang=python>
 +
text = str( number)
 +
</syntaxhighlight>
 +
 
 +
or if you wanted to convert and display the number you could do this instead:
 +
 
 +
<syntaxhighlight lang=python>
 +
print("the number entered was: " + str(number) )
 +
</syntaxhighlight>

Revision as of 10:16, 4 March 2019

Data Type Conversions

Python has built in functions to convert between different data types. To convert something to an integer you would enter:

number = int( textEntered)

or if you wanted to get the input direct from the user you could do this instead:

name = int( input(enter a number) )

Other conversion functions are available:

Conversions in python.png

In the previous section Variables you needed to convert the values entered by the user to an integer in this way. You may also need to convert in the opposite way:

text = str( number)

or if you wanted to convert and display the number you could do this instead:

print("the number entered was: " + str(number) )