Python Basics: Data Types, Variables

Python Basics: Data Types, Variables

·

3 min read

Welcome to the first article.

In this article, I’ll be introducing you to beginner topics in python

  • Hello World
  • Data types
  • Variables and rules

Without wasting any more time. Let’s get started

Hello World

First, we’re gonna write a simple python program.

input:

print("Hello World!")

output:

Hello World!

Even if you have never programmed before, you can understand that input is gonna print Hello world. So now let us dissect the program and understand it

print() is a built-in function that prints anything in the console. The text and quotation marks surrounded by parentheses is known as a string which we are going to look at in the coming section

Don’t worry if you don’t know what a function is, we will look into that topic in my next articles.

Data Types

Data types mean the classification of data according to their type. For example, alphabets belong to string data type, integers belong to int data type, etc.

Fundamental Data Types

  • string [ str ]
  • integer [ int ]
  • float
  • Boolean [ bool ]
  • list
  • dictionary [ dict ]
  • tuple
  • set

Let’s look into the first four Data types in this article

String

It’s just a piece of text between single or double quotes or even triple quotes for text on multiple lines input :

# for strings on single Line

print("I am learning python")

# for strings on multiple Lines or long strings

print('''

WOW 
o o
---

''')

Output :

I am learning python

WOW 
o o
---

int

Also known as an integer. It is a positive or negative number with no decimal points.

#Addition
>>> print(2 + 3)
5

#Subtraction
>>> print(3 - 2)
1

#Multiplication
>>> print(2 * 3)
6

#Division
>>> print(3 / 2)
1.5

#Exponentiation
>>> print(3 ** 3)
27

# Floor Division or Integer Division
>>> print(5 // 2)
2

# Modulo
>>> print(5 % 2)
1

The first five operations are self-explanatory.

While coming to floor division it is a type of division where when // is used the output will be rounded off to the closest integer.

For example, when 5 / 2 output is 2.5 When 5 // 2 output will be rounded off 2

And Modulo operator % is used to obtain the remainder of a division problem. For example, 5 / 2 quotient is 2 and the remainder is 1

Float

Any number with a decimal point is a float.

>>> print(0.1 + 0.1)
0.2

>>> print(0.2 - 0.4)
-0.2

>>> print(2 * 0.1)
0.2

>>> print(5.5 / 2)
2.75

Boolean

It is a data type used to evaluate if a expression is True or False

print(5 > 2)
print(5 == 2)
print(5 < 2)

Output:

True
False
False

Variables

Variables store information that can be used in our programs.

Note: Assigning a value is known as Binding.

Rules for assigning Variables

  • Snake_case [space is replaced by underscore ]
  • Start with lowercase or underscore
  • only use letters, numbers, underscores
  • Case sensitive
  • Don't overthink keywords
my_name = "Abhiram"
print(my_name)

#Assigning values to variables to multiple times (Multiple Assignment)
a,b,c = 1,2,3
print(a)
print(b)
print(c)

Output:

Output :
Abhiram
1
2
3

Thanks for reading! 😃

See you next week 😉