Python Variables: How to Define/Declare String
Variable Types
What is a Variable in Python?
A 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. The names
we select for variables and functions are commonly referred to as Identifiers. In
Python, Identifiers must adhere to the following rules:
1- All identifiers must begin with a letter or underscore (_), they can't start with a
digit.
2- Identifiers can include letters, digits and underscores (_).
3- Identifiers can't be a keyword. Keywords are reserved words that Python uses for
special purposes). The following are keywords in Python 3.
* Identifiers can be of any length.
Illegal variable names
2myvar = "Ahmad" # variable name cannot start with a number
my-var = "Sara" # variable name cannot contain special characters like -
my var = "Ghazi Khaldun" # variable name cannot contain space
Assigning Values to Variables
In Python, you can assign values to variables using the assignment operator, which
is the equals sign =. Here's how you assign values to variables:
1- Variable Declaration: You can create a variable by giving it a name and then
assign a value to it.
variable_name = value
For example:
age = 38
name = "Khaldun"
2- Multiple Assignments: You can assign values to multiple variables in a single
line.
variable1, variable2, variable3 = value1, value2, value3
For example:
x, y, z = 10, 20, 30
3- Updating Variables: You can change the value of a variable by reassigning it
with a new value.
variable = new_value
For example:
count = 5
count = count + 1 # Updating the 'count' variable
4- Variable Types: In Python, you don't need to specify the data type of a variable.
Python dynamically determines the type based on the value assigned.
variable = value # Python determines the data type
For example, age = 40 could be an integer, and name = "Ali" could be a string.
5- Multiple Variables with the Same Value: You can assign the same value to
multiple variables.
variable1 = variable2 = value
For example:
x = y = 10
- Remember that variable names in Python are case-sensitive and should follow
certain naming conventions. Variable names can include letters, numbers, and
underscores, but they cannot start with a number and cannot be a Python
keyword. It's also a good practice to use descriptive variable names to make your
code more readable.
- In Python, you don't need to declare variable types ahead of time. The interpreter
automatically determines the variable's type based on the data it contains.
Example:
x = 200 # x is integer output
pi = 3.14 # pi is float
Text = "Python is fantastic" # Text is string
200
a = b = c = 44 # This statement assigns 44 to c, b and a.
3.14
print(x) # print the value of variable x Python is fantastic
print(pi) # print the value of variable pi 44 44 44
print(Text) # print the value of variable Text
print(a, b, c) # print the value of variables a, b, and c
Example:
A = 1 # initial value of A is 1 output
B = 2 # initial value of B is 2
A= 2
B, A = A, B # assign B value to A and A value to B
B= 1
print("A=",A) # final value of A is 2
print("B=",B) # final value of B is 1
Example:
# Variable names are case-sensitive
name1="Sadeen"
output
name2="Diala"
age=6
Age=14
Sadeen 6
AGe=13
Diala 13
print(name1,age)
print(name2,AGe)
Example:
#Python variable
#Python has no command for declaring a variable.
d=45
c="Ahmad"
print ("d=",d,"\n","c=",c) output
z=4 # type of z is int
z="Sally" # type of z now is a string d= 45
print (z) c= Ahmad
Sally
z="Sami" # The final value of z now is Sami
print (z) Sami
Python Data Types
Python supports a variety of data types to work with different kinds of data. Here
are some of the fundamental data types in Python:
Numeric Types:
int: Integer data type, for whole numbers.
float: Floating-point data type, for decimal numbers.
complex: Complex data type, for complex numbers (e.g., 2 + 3j).
Sequence Types:
str: String data type, for text.
list: List data type, an ordered collection of values.
tuple: Tuple data type, an ordered collection of immutable values.
Mapping Type:
dict: Dictionary data type, a collection of key-value pairs.
Set Types:
set: Set data type, an unordered collection of unique values.
frozenset: Frozenset data type, an immutable set.
Boolean Type:
bool: Boolean data type, representing True or False.
None Type:
NoneType: Represents the absence of a value, often denoted as None.
These are the most common built-in data types in Python. Additionally, Python
allows for custom data types through classes and object-oriented programming,
enabling you to create your own data structures as needed.
Re-declare a Variable
You can re-declare Python variables even after you have declared once.
Here we have Python declare variable initialized to f = 0.
Later, we re-assign the variable f to value “guru99”
Source: https://www.guru99.com/python-tutorials.html output
Python Example
# Declare a variable and initialize it 0
f = 0 guru99
print(f) # Output: 0
# re-declaring the variable works
f = 'guru99'
print(f) # Output: guru99
Python Example
x = 5 # Assign the value 5 to variable x
print(x) # Output: 5
x = 10 # Reassign a new value 10 to the variable x
print(x) # Output: 10
Example:
# VARIABLE DATATYPE
x = 10 # int
x = 2.3 # float
print(type(x)) # <class 'float'>
d = 3j
print(type(d)) # <class 'complex'>
m = range(5) # range(0, 5)
a = range(100) # range(0, 100)
z = [1,2,3,4,5] # list []
tup = (2,4,6,8) #tuple ()
st = {3,6,9,12,15} # set {}
fr = ("apple", "banana", "cherry") # Tuple ()
print(z) # [1, 2, 3, 4, 5]
a2 = {"name":"Ali","Age":30,"Email":"ali.ghnmein@gmail.com"} # dict {}
xst = set((10,20,30,40))
print(xst) # {40, 10, 20, 30}
bl = True
bbl = False
print(bl,bbl) # True False
Python String Concatenation and Variable
Let’s see whether you can concatenate different data types like string and number
together. For example, we will concatenate “Guru” with the number “99”.
Unlike Java, which concatenates number with string without declaring number as
string, while declaring variables in Python requires declaring the number as string
otherwise it will show a TypeError
Source: https://www.guru99.com/python-tutorials.html
Example:
# Concatenating strings
first_name = "Khaldun"
last_name = "Ali"
full_name = first_name + " " + last_name
print(full_name) # Output: Khaldun Ali
# Concatenating a string and a variable
age = 39
message = "I am " + str(age) + " years old."
print(message) # Output: I am 39 years old.
* Note that you need to convert the variable age to a string using str() before
concatenating it with the string.
Example:
# Python - concatenation Strings
a = "Maan"
b = "Jordan"
c = a + b
print(c) # output : MaanJordan without whitespace
print("Amman"+",Jordan"+" "+"AHU") #output: Amman,Jordan AHU
Example:
z= 10 output
z="ALI"
6.7
z=False
z=True False
z=6.7
print (z) # The final value of z now is 6.7
z=False # Reassign a new value False to the variable z
print (z) # The final value of z now is False
Example:
# legal and illegal variable name
X1= 10
X2= "ALI"
X3=False
X4=True
#5x=9
e_o="Hello"
#w t =0
#First%Name
#First_name
Example:
# Find the type of the variable
a=15
print(type(a)) output
b= 73.9
print(type(b))
print(type(73.9)) <class 'int'>
c='ALI' <class 'float'>
print(type(c))
<class 'float'>
t=True
print(type(t)) <class 'str'>
print(type(False)) <class 'bool'>
<class 'bool'>
# Assign values to multiple variables
x1, x2, x3 = 10, 20, 30
10 20 30
print(x1,x2,x3) Ali OMAR AHMED
Ali OMAR 1234567 96
fname, mname, lname = 'Ali', 'OMAR', 'AHMED'
print(fname,mname,lname)
fname, lname, id, test1 = "Ali", 'OMAR', 1234567, 96
print(fname,lname,id,test1)
Arithmetic operations in Python
Arithmetic operators are used with numeric values to perform common
mathematical operations:
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
Output
print(10+7)# output: 17
17
print(7-1) # output: 6
print (6*10) # output: 60 6
print(19/3) # output: 6.333333333333333 60
print (19//3) # output: 6 6.333333333333333
print(2**10) # output: 1024
6
print (19/3) # output: 6.333333333333333
1024
#or
a= 19/3 6.333333333333333
print (a) # output: 6.333333333333333
6.333333333333333
-------------------------------------------------
Floor Division / Integer Division
In Python, the double-backslash operator (//) is the floor division operator. floor
division means dividing and rounding down to the nearest integer.
print(16//3) # output: 6
Example:
# Arithmetic operators
x=9
y=3
print (x,"+",y,"=", x+y) # returns 12
print (x,"-",y,"=", x-y) # returns 6
print (x,"*",y,"=", x*y) # returns 27
print (x,"/",y,"=", x/y) # returns 3.0
print (x,"%",y,"=", x%y) # returns 0 (Modulus)
print (x,"//",y,"=", x//y) # returns 3
print (x,"//",2,"=", x//2) # returns 4 (Floor division)
print (x,"/",2,"=", x/2) # returns 4.5 ( Division )
print (x,"**",2,"=", x**2) # returns 81 (Exponentiation)
Example:
print (10/2) # output : 5.0
print(10//2) # output : 5
# output floating point value
x1 = 19/3
print (x1) # output : 6.333333333333333
X2 = 19//3
print (X2) # output : 6
a = 10/2
print (a) # output : 5.0
a=10%2
print (a) # output : 0
b = 11/2
print(b) # output : 5.5
b = 11%2
print(b) # output : 1
Example:
#Exponentiation
s = 2**10
print(s) # output : 1024
print (2**10) # output : 1024
print(pow(2,10)) # output : 1024
m = pow(2,10)
print(m) # output :1024
-----------------------------------------------------------------
number1 = 5
number2 = 20
result = number1 + number2 # result = 5 + 20
print(result) # output: 25
-----------------------------------------------------------------
# + operator for strings is Concatenation
firstName = "ALI"
lastName = 'OMAR'
studentFullName = firstName + ' ' + lastName
print(studentFullName)
-----------------------------------------------------------------
Example:
# Addition (+)
result = 5 + 3
print(result) # Output: 8
# Subtraction (-)
result = 10 - 4
print(result) # Output: 6
# Multiplication (*)
result = 6 * 4
print(result) # Output: 24
# Division (/)
result = 15 / 5
print(result) # Output: 3.0 (in Python 3)
# Floor Division (//)
result = 15 // 5
print(result) # Output: 3
# Modulus (%)
result = 15 % 7
print(result) # Output: 1
# Exponentiation (**)
result = 2 ** 3
print(result) # Output: 8
order of operators (from highest to lowest precedence)
# 1- Parenthesis ()
# 2- Exponentiation ** pow(x,y)
# 3- Multiplication *,Division /,Floor Division //,and Modulus %
# 4- Addition + and Subtraction - (left to right)
Exercise: Show the output
print (10-7//2*3+1)
print (7+6*2**4+3//5)
print (((7+6*)2**4+3)//5)