Friday, 20 March 2020

Python variable and some exercises


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 (age, Age and AGE are three different variables) 

Python variable is a reserved memory location to store values. In other words, a variable in a python program gives data to the computer for processing. Every value in Python has a datatype. Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc

x = y = z = 99

print(x)
print(y)
print(z)
Output:
99
99
99
Another example of multiple assignment
a, b, c = 5, 6, 7
print(a)
print(b)
print(c)
Output:

Plus and concatenation operation on the variables

x = 10
y = 20
print(x + y)

p = "Hello"
q = "World"
print(p + " " + q)
Output:
However if you try to use the + operator with variable x and p then you will get the following error.
unsupported operand type(s) for +: 'int' and 'str'

Data Types

A data type defines the type of data, for example 123 is an integer data while “hello” is a String type of data. The data types in Python are divided in two categories:
1. Immutable data types – Values cannot be changed.
2. Mutable data types – Values can be changed
Immutable data types in Python are:
1. Numbers
2. String
3. Tuple
Mutable data types in Python are:
1. List
2. Dictionaries
3. Sets

No comments:

Post a Comment