Skip to content

Commit

Permalink
Merge pull request #24 from edilsonmatola/python-basics
Browse files Browse the repository at this point in the history
Pyhton Data Type Conversion
  • Loading branch information
edilsonmatola authored Mar 17, 2022
2 parents a5e9197 + cc78e07 commit 6a51b0a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 9 deletions.

This file was deleted.

8 changes: 8 additions & 0 deletions python_basics/5.data_conversion/converting_to_float.py
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
9 changes: 9 additions & 0 deletions python_basics/5.data_conversion/error_converting.py
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)
24 changes: 24 additions & 0 deletions python_basics/5.data_conversion/int_float_challenge.py
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
8 changes: 8 additions & 0 deletions python_basics/5.data_conversion/string_to_float.py
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
8 changes: 8 additions & 0 deletions python_basics/5.data_conversion/string_to_integer.py
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

0 comments on commit 6a51b0a

Please sign in to comment.