Subscribe Us

Python Variables

Variables are used to store values.To create a variable, use the equal sign (=).

In this example, we have a variable named fruit and we assign the "mangos" value to it.

Python Variables

color = "green"

Now, we can use our variable:


Here is another example:

name = "Rahul"

print("Hello" + name)

Changing a Variable Value

To change the value of a variable, simply assign a new value to it.

name = "Rahul"

name = "Amit"

print("Hello + name)

You can also assign a variable to a value of another type.

In this example, the variable is originally a string (text) then we'll change its value to a number.

name = "Rahul"


Rules for Naming Variables

• A variable name should start with a letter or underscore (_).

• A variable name cannot start with a number.

• A variable name is case-sensitive.

• A variable name should only contain underscores ( ) and alphanumeric characters.

Here are some examples of legal variable names:


my_name = "Rahul"

_myname = "Rahul"

myname1 = "Rahul"

myName = "Rahul"

Here are some examples of illegal variable names:

$myname = "Rahul"

my name = "Rahul"

1myname = "Rahul"

my-name = "Rahul"

Long Variable Names

When naming a variable with a long name, use the underscores to separate words.

Here is an example:

my_favorite_fruit = "Banana" 


You can also use the camelCase format where every first letter of the words are capitalized.

myFavorite Fruit = "Banana"


Python Data Types

In this tutorial, you will learn the most basic data types in Python.


In Python, there are two basic types of numbers and they are called integer and floating point numbers.
An integer does not have decimals.

#x is an integer 
x = 33

A floating point number has decimals.

# x is a floating point number 
x = 33.33

To add numbers, use the plus sign (

x = 5
y = 8
print(x + y)


Strings are simply text. A string must be surrounded by single quotes or double quotes.

# using single quotes

fruit = 'mangos' print(fruit)

Try It Yourself

# using double quotes

fruit = "mangos" print(fruit)

Single or double quotes?

Use single quotes when your string contains double quotes.

x = 'I Love HTML' ("I Love Python")

Use double quotes when your string contains single quotes.

x = "I love Python!" 


The Boolean data type can only have one of these two values: True or False.

bool = True
bool = False

When comparing two values, Python returns a Boolean. Let's compare some numbers and see whether the expression evaluates to True or False.

print(9 > 4) # True
print(9 > 4) # True
print(3< 2) # False 
print(6 == 6) # True
print(8 == 5) # False


  • A list is an ordered collection of data.
  • It can contain strings, numbers etc.
  • Lists are written with square brackets ([]).
  • The values (also called elements) in a list are separated with commas ( ,).

list = ["Kivy", "Bananas", "Mangos",]
list = [2, 4, 6, 8, 10] 

Python Numbers

In Python, there are three types of numbers:

• Integer int
• Floating Point float
• Complex - Complex


An integer is a number without decimals.

a = 3
b = 5
c = 6

Floating Point

A floating point number or a float is a number with decimals.

a = 5.0
b = 8.21
c = 76.25


A complex number is an imaginary number. To yield a complex number, simply append a 'j' or 'J' to a numeric value.

a = 7j
b = 7.81j
c = 3.93j

Getting the Type of Numerical Values

We can get the specific data type of numbers using the type() function.

a = 6
b = 86
c = 7.24


Adding Numbers

To add numbers, use the plus sign (,)

x = 8+ 4

Subtracting Numbers

To subtract numbers, use the minus sign (-).

x = 57

Multiplying Numbers

To multiply numbers, use the asterisk sign (*).

x = 2 * 6

Dividing Numbers

To divide numbers, use forward slash (/).

x = 4/2 print(x)

Python Number Methods

In this lesson, we'll study some useful methods or functions that can be used to work with numbers.

Rounding a Number

The round() function rounds a floating point number to a specified decimal point.


round (number, ndigits)

number the number to be rounded off ndigits - the number of decimal places.

pi= 3.1415926535897 # this is 1
two_decimals = round(pi, 2)
three_decimals = round(pi, 3)

Raising a Number to a Power

The pow() function is used to raise a number to a specified power.


pow(base, exp)

base the base number exp the exponent.

x = pow(4, 2)
y = pow(4, 3)


Getting Absolute Value

The abs() function returns the absolute value of a number.What is the absolute value? In mathematics, the absolute value is the non-negative value of a number.

For example, the absolute value of 21 is 21 and the absolute value of -8 is 8.

a = abs(-2)
b = abs(-3.21)

Getting Quotient and Remainder

The divmod() function takes two numbers and return a pair of numbers consisting of their quotient and remainder.In this example, 5 is divided by 2

The divmod() function takes two numbers and return a pair of numbers consisting of their quotient and remainder.

In this example, 5 is divided by 2 therefore the quotient is 2 and the remainder is 1.


x = divmod(5, 2)

Python Strings

Strings in Python, are sequences of characters or simply text. A string can either be created using single quotes or double quotes.

Using single quotes:

str = 'Hello World!' 


Using double quotes:

str = "Hello World!" 


Single or Double Quotes

It does not really matter which one you use because Python treat them the same way.

However, if your string contains double quotes use single quotes.

text = 'He said "I love Python!' 


And, if your string contains single quotes use double quotes.

str= "I Love Python"


Multi-Line Strings

In Python, it is possible to have a string that spans multiple lines.

To create a multiline string, surround a string with triple single quotes or triple double quotes.

Using triple single quotes:

str = '''Python is fun,I love Python learning Python.'''


Using triple double quotes:

str= """Python is fun,I love Python learning Python"""


Concatenating Strings

Concatenating strings simply means combining or adding strings together.

To combine strings, use the plus sign (+).

a = "Hello"

b = "World!"

str = a + b


We can combine as many strings as we want.

a = "I Love Python"

b = "I love HTML"

c = "I love JavaScript"

d = " I love CSS"

str = a + b + c + d


Escaping Characters in a String

Escaping characters is important in handling strings. It helps us make sure that our strings are recognized as pieces of text, and not as part of the code.

In this example, there is a single quote inside a string created with single quotes because of that it produces an error.

# This will produce a syntax

str = 'I love Python' 


We can prevent this error by escaping the single quote.

To escape a character, put a backslash (\) right before the character, which in this case is the single quote.

# this will NOT produce a syntax

 str = 'Let\'s learn Python'


Accessing Parts of a String

In Python, we can access a single character or a range of characters from a string.


To access a single character, use indexing. Indexing uses square brackets ([]) to access characters.

O represents the first character, 1 represents the second character, and so on.

str = "Hi everyone"

While -1 represents the last character, and -2 represents the second to the last character.

str = "Hi everyone"


To access a range of characters, use slicing.

Slicing also uses square brackets ( []).

The square brackets can contain two integers separated by a colon (: ). The first integer is the start index, the second integer is the end index (exclusive).

str = "Love Python"


Even though index 5 represents the letter n in our example above, it was still not printed because when slicing the end index is not included.

To slice from a specific position until the end of the string don't specify the second integer.

str = "I love Python"


Post a Comment

* Please Don't Spam Here. All the Comments are Reviewed by Admin.