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.
color = "green"
Now, we can use our variable:
print(color)
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"
print(name)
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:
myname="Rahul"
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"
print(my_favorite_fruit)
You can also use the camelCase format where every first letter of the words are capitalized.
myFavorite Fruit = "Banana"
print(myFavoriteFruit)
Python Data Types
Numbers
#x is an integerx = 33print(x)
# x is a floating point numberx = 33.33print(x)
x = 5y = 8print(x + y)
Strings
# using single quotesfruit = 'mangos' print(fruit)Try It Yourself# using double quotesfruit = "mangos" print(fruit)
x = 'I Love HTML' ("I Love Python")print(x)
x = "I love Python!"print(x)
Booleans
bool = Trueprint(bool)bool = Falseprint(bool)
print(9 > 4) # Trueprint(9 > 4) # Trueprint(3< 2) # Falseprint(6 == 6) # Trueprint(8 == 5) # False
Lists
- 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",]print(list)list = [2, 4, 6, 8, 10]print(list)
Python Numbers
Integer
a = 3b = 5c = 6
Floating Point
a = 5.0b = 8.21c = 76.25
Complex
a = 7jb = 7.81jc = 3.93j
Getting the Type of Numerical Values
a = 6b = 86c = 7.24print(type(a))print(type(b))print(type(c))
Adding Numbers
x = 8+ 4print(x)
Subtracting Numbers
x = 57print(x)
Multiplying Numbers
x = 2 * 6print(x)
Dividing Numbers
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.
Syntax:
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)
print(two_decimals)
print(three_decimals)
Raising a Number to a Power
The pow() function is used to raise a number to a specified power.
Syntax:
pow(base, exp)
base the base number exp the exponent.
x = pow(4, 2)
y = pow(4, 3)print(x)
print(y)
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)
print(a)
print(b)
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)
print(x)
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!'
print(str)
Using double quotes:
str = "Hello World!"
print(str)
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!'
print(text)
And, if your string contains single quotes use double quotes.
str= "I Love Python"
print(str)
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.'''
print(str)
Using triple double quotes:
str= """Python is fun,I love Python learning Python"""
print(text)
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
print(str)
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
print(str)
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'
print(str)
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'
print(str)
Accessing Parts of a String
In Python, we can access a single character or a range of characters from a string.
Indexing
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"
print(str[0])
print(str[1])
While -1 represents the last character, and -2 represents the second to the last character.
str = "Hi everyone"
print(str[-1])
print(str[-2])
Slicing
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"
print(str[2:5])
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"
print(str[2:])