Variable & Data Types (Session 3)
Variable:
A variable is a name given to a memory location in a program. Variables do not need to be declared with any particular type, and can even change type after they have been set.
For example:
a=28
b=21.26
c="Mustajab"
In short, we can say that here variable is a container to store a value.
Example:
x = 28
y = "Mustajab"
print(x)
print(y)
Here Mustajab is str type and 28 is int type.
Many Values to Multiple Variables:
Python allows us to assign values to multiple variables in one line:
x, y, z = "Student1", "Student2", "Student3"
print(x)
print(y)
print(z)
print(x)
print(y)
print(z)
Rules for defining a variable name:
- A variable name can contain alphabets, digits, and underscore.
- A variable name can only start with an alphabet and underscore.
- A variable can’t start with a digit.
- No white space is allowed to be used inside a variable name.
Task: Read about Whitespace Characters in Python.
Data Type:
Python is a great language that automatically identifies the type of data for us. Primarily there are the following data types in Python:
- Integers
- Floating point numbers
- Strings
- Booleans
- None
Kindly read and practice all type of datatypes by clicking on the provided link below:
Casting:
We use casting when we need to specify the date type of variable.
Example:
x = str("Mustajab") # x will be 'Mustajab'
y = int(2) # y will be 2
z = float(2) # z will be 2.0
y = int(2) # y will be 2
z = float(2) # z will be 2.0
print (x)
print (y)
print (z)
Keywords:
Reserved words are known as keywords in Python. Python has a set of keywords that cannot be used as variable names, function names, or any other identifiers.
Some of them are:
- And
- elif
- as
- else
- del
- false
- global
- finally
- import
- in.. in etc.
Operators in Python:
Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.
The following are some common operators in Python:
- Arithmetic Operators (+, -, *, /, etc.)
- Assignment Operators (=, +=, -=, etc.)
- Comparison Operators (==, >=, <=, >, <, !=, etc.)
- Logical Operators (and, or, not)
Remember, REPL task in which we use some arthimetic operators to print table of 5.
>>> 2*3
6
Type()Function:
type function is used to find the data type of a given variable in Python.
a = ('Red', 'Blue', 'Pink')
b = "Hello World"
c = 33
x = type(a)
y = type(b)
z = type(c)
print(x)
print(y)
print(z)
Input()function:
This function allows the user to take input from the keyboard as a string.
For Example:
a = input(“Enter name”)
print("Enter your name:")
x = input()
print("Hello, " + x)
Practice Set 2:
- Write a Python program to add three numbers.
- Write a Python program to find the remainder when a number is divided by Z(Integer).
- Check the type of the variable assigned using the input() function.
- Use a comparison operator to find out whether a given variable a is greater than b or not.
- Write a Python program to find the average of two numbers entered by the user.
- Write a Python program to calculate the square of a number entered by the user.
- Write about pascal case, camel case and snake case with examples.
- Read and practice Self study 1 & 2.
This comment has been removed by the author.
ReplyDelete(Hint: Practice Set Question 3)
ReplyDeleteWhen you use input funtion to assign some value to a variable, it consider it as a string even if you enter an integer, float or boolean value. So use int() function by passing string as argument in it to convert that string to int.
Example:
num = input('Enter a number: ')
num = int(num)
or
num = int(input('Enter a number: '))