Variables are nothing but containers to store data values .To declare a variable in Python you just have to type the variable name and assign a value to it. You can declare a variable as below:
Variable_name= value
Python uses = to assign values to variables . In Python assigning a value to variable itself declares the variable you need to declare a variable first and then assign it an initial value.
Example:
x= 5
name= "John"
price= 56.98778
Start= True
string variable can be declared either by using single quotes or double quotes.
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (price, first_name, last_name, total_count). Rules for Python variables:
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive (name, Name and NAME are three different variables)
- We cannot use reserved keywords as a variable name.
Example :
Valid variable names:
x=5
_myvalue=7
total_price=1245.676
MYBAG="empty"
Invalid variable names:
5x=6
my-val=49
$y=56.6
My name= 'john'
You can assign multiples values to multiple variables in one single line but make sure that there are same number of arguments on both left and right side of the = operator.
Example:
a,b,c = 1,2,3
name,age,weight= 'john',54,79
Data types
Boolean: A Boolean value will be either True or False. Logical operations such as and,or and not can be performed on booleans.
example:
state= True
Numbers: numbers are of two types- integer and floating point numbers.
Example of integer numbers:
x=10
age=45
Example of floating point numbers:
y=56.677
price=5679.34
Strings: Strings are arrays of bytes representing unicode characters. Square brackets can be used to access the elements of strings. Strings are created using single quotes or double quotes.Creating strings with triple quotes will allow multiple lines.
Example:
string1='hello'
string2="world"
string3='''hello
World'''
Accessing characters in Python
In Python we can access the individual characters of a string by using the method of indexing. The first character is accessed by 0, next by 1 and so on. In a similar way we can access by using negative indexing. The last element can be accessed by using -1, second part using -2 and so on. If we try to access a character which is out of rammed we get an error as IndexError and if we pass float value as index we get an error as typeError.
#Python Program to Access
# characters of String
String1 = "MyLearningPath"
print("Initial String: ")
print(String1)
# Printing First character
print("\nFirst character of String is: ")
print(String1[0])
# Printing Last character
print("\nLast character of String is: ")
print(String1[-1])
Output:
Initial String:
MyLearningPath
First character of String is:
M
Last character of String is:
h
[Program finished]
String slicing
To access a range of characters in the String, method of slicing is used. Slicing in a String is done by using a Slicing operator (colon).
# Creating a String
String1 = "Learning Python is fun and challenging"
print("Initial String: ")
print(String1)
# Printing 5th to 17th character
print("\nSlicing characters from 4-16: ")
print(String1[4:16])
# Printing characters between
# 3rd and 2nd last character
print("\nSlicing characters between " +
"3rd and 2nd last character: ")
print(String1[3:-2])
Output:
Initial String:
Learning Python is fun and challenging
Slicing characters from 4-16:
ning Python
Slicing characters between 3rd and 2nd last character:
rning Python is fun and challengi
[Program finished]
Challenge Yourself
Write a program by creating variables if each type we discussed above and print them using the print() function. Go through my solution if you are confused or stuck and this is the basic one which will be very easy and it will make it fit in your memory.
Solution
x= 5
y=567.78
value= True
name='john smith'
father_name="Mark"
occupation='''i am a software engineer and i work in a reputed software company in California'''
print('integer=')
print(x)
print('\n')
print('floating point number=')
print(y)
print('\n')
print('Boolean')
print(value)
print('\n')
print('string=')
print(name)
print('\n')
print('double quote string=')
print(father_name)
print('\n')
print('triple quote string=')
print(occupation)
Output
integer=
5
floating point number=
567.78
Boolean
True
string=
john smith
double quote string=
Mark
triple quote string=
i am a software engineer and i work in a reputed software company in California
[Program finished]
Formatted strings( f strings)
You might have observed that we wrote so many lines for printing.As of Python 3.6, f-strings are a great new way to format strings. Not only are they more readable, more concise, and less prone to error than other ways of formatting, but they are also faster!
F–strings provide a concise and convenient way to embed python expressions inside string literals for formatting.
Syntax:
name='john'
Age=45
print(f"Hello!my nameis is {name}. I am {Age} old")
Output:
Hello!my nameis is john. I am 45 old
[Program finished]
Well this is interesting! now I want you to rewrite the above program for variables and print everything using f strings which we have learnt.