-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from edilsonmatola/python-basics
Pyhton Data Type Conversion
- Loading branch information
Showing
6 changed files
with
57 additions
and
9 deletions.
There are no files selected for viewing
9 changes: 0 additions & 9 deletions
9
python_basics/4.arithmetic_operators/5.data_conversion/converting_to_integer.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# We use the float() function to convert data of other types to floating-point numbers. For example, | ||
|
||
integer_number = -15 | ||
|
||
# converting to float | ||
float_number = float(integer_number) | ||
|
||
print(float_number) # Output: -15.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# We cannot convert all types of strings to integers. Let's take an example. | ||
|
||
string_number = 'Howdy!' | ||
|
||
# converting to integer | ||
|
||
integer_number = int(string_number) # Error! | ||
|
||
print(integer_number) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
""" | ||
* Create a program to convert a variable of one type to another. | ||
* Create a variable named x and assign a string value '5' to it. | ||
* Create another variable y and assign '10' to it. | ||
* Convert the value stored in x and y to integers and multiply them. | ||
* Print the result. | ||
""" | ||
|
||
x = '5' | ||
|
||
y = '10' | ||
|
||
x = int(x) | ||
|
||
y = int(y) | ||
|
||
product = x * y | ||
|
||
print(product) # Output: 50 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
string_number = '-15.65' | ||
|
||
# converting to float | ||
# float_number will be -15.65 | ||
|
||
float_number = float(string_number) | ||
|
||
print(float_number) # Output: -15.65 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
string_number = '15' | ||
|
||
# converting to integer | ||
# integer_number will contain 15 | ||
|
||
integer_number = int(string_number) | ||
|
||
print(integer_number) # Output: 15 |