Galle, LK--:--5/6 sites live

Basic Data Types in Python

Model එකකට තේරෙන්නෙ නැති shape එකක් තියෙන data එකකින් ඉගෙන ගන්න බෑ. int, float, string, bool අතර වෙනස දැනගැනීම තමයි, ගණනය කිරීමක් නිශ්ශබ්දව වැරදි වෙන්නෙ ඇයි කියලා තේරෙන්නෙ.

A model can't learn from data it doesn't understand the shape of. Knowing int vs float vs string vs bool is what lets you spot why a calculation is silently wrong.

2 May 20263 min readSelf Guide · Stage 13#Python & Data Basics#Python Fundamentals#Machine learning

Lesson 3 of 64 · Part 1 · Python & Data Basics · Python Fundamentals · bilingual notes (English + සිංහල)

Why this matters. A model can't learn from data it doesn't understand the shape of. Knowing int vs float vs string vs bool is what lets you spot why a calculation is silently wrong.

සිංහලෙන්: Model එකකට තේරෙන්නෙ නැති shape එකක් තියෙන data එකකින් ඉගෙන ගන්න බෑ. int, float, string, bool අතර වෙනස දැනගැනීම තමයි, ගණනය කිරීමක් නිශ්ශබ්දව වැරදි වෙන්නෙ ඇයි කියලා තේරෙන්නෙ.

What you'll use this for. Almost every bug in a Pandas pipeline traces back to a column being the wrong data type — this lesson is what lets you actually fix that.

සිංහලෙන්: Pandas pipeline එකක වෙන bugs ගොඩක් හේතුව column එකක data type එක වැරදි එක — මේ පාඩම තමයි ඒක fix කරන්න උදව් කරන්නෙ.

Key principles

  • Types decide what an operator means. "3" + "3" gives "33", but 3 + 3 gives 6. Same symbol, completely different result — because the types differ.
    Type එක තමයි operator එකේ තේරුම තීරණය කරන්නෙ

Walkthrough

Basic Data Types in Python:

  1. Integer
  2. Floating Point
  3. Complex
  4. Boolean
  5. String
ExamplePython
# integers
a = 8
print(a)

Output

Output
8
ExamplePython
type(a)

Output

Output
int
ExamplePython
# floating point
b = 2.3
print(b)
type(b)

Output

Output
2.3

Output

Output
float
ExamplePython
# complex numbers
c = 1 + 3j
print(c)
type(c)

Output

Output
(1+3j)

Output

Output
complex

Conversion of one data type to another

ExamplePython
# int to float
x = 10
print(x)
type(x)

Output

Output
10

Output

Output
int
ExamplePython
y = float(x)
print(y)
type(y)

Output

Output
10.0

Output

Output
float
ExamplePython
# float to int
x = 5.88
print(x)
type(x)

Output

Output
5.88

Output

Output
float
ExamplePython
y = int(x)
print(y)
type(y)

Output

Output
5

Output

Output
int

Boolean

  1. True
  2. False
ExamplePython
a = True
print(a)
type(a)

Output

Output
True

Output

Output
bool
ExamplePython
b = False
print(b)
type(b)

Output

Output
False

Output

Output
bool
ExamplePython
a = 7 < 3
print(a)
type(a)

Output

Output
False

Output

Output
bool
ExamplePython
a = 7 > 3
print(a)
type(a)

Output

Output
True

Output

Output
bool

String

ExamplePython
print("Machine Learning")

Output

Output
Machine Learning
ExamplePython
print('Machine Learning')

Output

Output
Machine Learning
ExamplePython
my_string = "Machine Learning"
print(my_string)
type(my_string)

Output

Output
Machine Learning

Output

Output
str
ExamplePython
print("Hello"*5)

Output

Output
HelloHelloHelloHelloHello

Slicing

ExamplePython
my_string = "Programming"
ExamplePython
print(my_string[1:5])    # values from index 1 to 5-1 will be sliced

Output

Output
rogr
ExamplePython
# step
print(my_string[0:10:2])

Output

Output
Pormi

String Concatenation

ExamplePython
word_1 = 'Machine '
word_2 = 'Learning'

print(word_1+word_2)

Output

Output
Machine Learning

Keep going: ← Lesson 2 · Lesson 4 → · Track it in the Self Guide