DEV Community

Cover image for Python Basics: Variables and Operators
iamdestinos
iamdestinos

Posted on

Python Basics: Variables and Operators

Introduction

Python is one of the most commonly used programming languages. This post will cover information regarding the basics of Python, specifically variable types and operators.

Variables

In order to store values, Python has a series of variable types that store values differently. These variables can be split into two kinds, simple and complex data types. Simple data types hold a single value of a given type while complex data types hold multiple values of various types.

Basic Variables (Simple Data Type)

Numbers store numerical values and can be an integer or a float value.

a = 1 #integer
b = 3.14 #float
Enter fullscreen mode Exit fullscreen mode

Strings are capable of storing words, phrases, and symbols as a series of characters

name = "John" #strings are stored between two quotation marks
surname = 'Doe' #they can be marked by double or single quotes
Enter fullscreen mode Exit fullscreen mode

Booleans are a binary value, either being true or false.

bool1 = True #booleans are noted by capitalizing the first character
bool2 = False
Enter fullscreen mode Exit fullscreen mode

Complex Variables (Complex Data Types)

Python has four types of complex variables; lists, tuples, sets, and dictionaries. Each of these variable types store values in a collection but how they store values differ.

Lists are a collection of values which store values in a 0-indexed order. Lists are also changeable, which allow values to be added, removed, or changed.

# Lists are similar to javascript arrays as ordered storage of variables
list_example = ["val1", "val2", "val3"] #lists are noted by square brackets
# Lists can be accessed by index values
print(list_example[0])
# print will print "val1" 
Enter fullscreen mode Exit fullscreen mode

Tuples are similar to lists in that they are an ordered collection of values but have one key difference: once created they cannot be changed in any way.

tup = ("val1", "val2", "val3") #similar to lists, tuples are noted with parentheses
print(tup[1]) #will print out "val2"
Enter fullscreen mode Exit fullscreen mode

Sets are different from lists and tuples, unlike them sets store values in an unordered collection, meaning that whenever a set is accessed the order of values is different. Sets also differ from lists in tuples in that they do not allow duplicate values inside them. Similarly to tuples the values in a set cannot be changed but do allow values to be added and removed

setex = {"val1", "val2", "val3"} #sets are noted by curled braces
print(setex) #will print out "val1", "val2", and "val3" in a random order
Enter fullscreen mode Exit fullscreen mode

Dictionaries, unlike all other variables store values as key value pairs in an ordered list. In a similar vein to sets, dictionaries cannot have duplicates, but duplicates only apply to the key names. Like lists, dictionaries can be changed by adding, removing, or changing values.

dictex = {
  "key": "val",
  "key2": 2
}
# Dictionary values are accessed through its keys
print(dixtex["key"]) #prints "val" to console
Enter fullscreen mode Exit fullscreen mode

Operators

Operators are symbols that indicate operations to be performed on a value/variable. Python has several different types of operators, including: mathematical, assignment, comparison, logical, identity, and membership.

Mathematical Operators

Arithmetic operators are operators that carry out mathematical operations on values. Python has the basic addition and subtraction as shown below as well as some more complex ones.

#Addition and Subtraction
a = 10 + 2 #a will equal 12
b = 10 - 2 #b will equal 8
Enter fullscreen mode Exit fullscreen mode

While multiplication and division aren't really complicated, Python has a notable interaction with division. When the division operator is used, Python will have the result be a float no matter what. If an integer is desired, a different operator must be used.
Additionally, Python includes the modulo operator and the power operator.

#Multiplication and Division
c = 2 * 3 #c will equal 6
#The normal division operator will result in a float value
d = 1 / 2 #d will equal 0.5
#To get an integer or rounded value, use two division operators
e = 1 // 2 #e will equal 0
#Modulo will return the remainder of a division operation
f = 10 % 3 #f will equal 1
#The power operator multiples a number to the given power
g = 2 ** 3 #g will equal 8 or 2 to the third power 
Enter fullscreen mode Exit fullscreen mode

Assignment Operators

Assignment operators allow variables to know to take in a value and in some cases add cases regarding how to take in the value. The basic assignment operator is the = sign, which all other assignment operators will include.

a = 1 #the = sign assigns the value to the variable
a += 2 #equivalent of a = a + 2, which results in 3
b = 3
b -= 2 #equivalent of b = b - 2, which results in 1
c = 2
c *= 3 #equivalent of c = c * 2, which results in 6
d = 3
d /= 4 #equivalent of d = d / 4, which results in 0.75
e = 3
e //= 4 #equivalent of e = e // 4, which results in 0
f = 2
f **= 3 #equivalent of f = f ** 3, which results in 8
Enter fullscreen mode Exit fullscreen mode

Comparison Operators (Boolean checks)

Comparison Operators, as the name implies, carry out a comparison between two values or variables and return a boolean.

a= 2
b = 4
#equals operator checks if two values are equal
comp1 = a == b #results in false
#not equal operator checks if two values are not equal
comp2 = a != b #results in true
#greater than operator checks if first value is greater than the second
c = 3
d = 3
comp3 = c > d #results in false
comp4 = c >= d #results in true, checks if greater or equal to value
#less than operator checks if first value is less than the second
comp5 = a < b #results in true
comp6 = c <= a #results in false, checks if less or equal to value
Enter fullscreen mode Exit fullscreen mode

Logical Operators

Logical Operators also return a boolean but they take in booleans or operations that result in booleans, allowing a check on multiple comparisons.

#The and operator checks if both conditions are true
cond1 = 1 < 2 and 1 < 5 #results in true
cond2 = 2 < 2 and 2 < 5 #results in false as one condition is false
#The or operator checks if at least one condition is true
cond3 = 1 < 2 or 1 < 5 #results in true since both conditions are true
cond4 = 2 < 2 or 2 < 5 #results in true since one condition is true
cond 5 = 5 < 2 or 5 < 5 #results in false as both conditions are false
#The not operator inverts the result, making true false and vice versa
cond6 = not(1 < 2) #results in false, as 1 < 2 is true and not inverts it
Enter fullscreen mode Exit fullscreen mode

Identity Operators

Identity Operators are used in the comparison of complex variables, specifically if two variables share the same reference.

var1 = [1, 2, 3]
var2 = [1, 2, 3]
var3 = var1
#The is operator checks if two variables possess the same reference
comp1 = var1 is var2 #results in false, as both refer to different lists even though they have the same values
comp2 = var1 is var3 #results in true, as both refer to the same list reference
#The is not operator  checks if two variables do not possess the same list reference
comp3 = var1 is not var3 #results in false, as both refer to the same list reference
comp4 = var1 is not var2 #results in true, as both refer to different list references
Enter fullscreen mode Exit fullscreen mode

Membership Operators

Membership Operators are used to check if a value is contained in a collection.

setex = {1, 2, 3}
#The in operator checks if a value is contained inside a collection
comp1 = 2 in setex #results in true as 2 is contained in setex
#The not in operator checks if a value is not contained inside a collection
comp 2 = 3 not in setex #results in false as 3 is contained in setex
Enter fullscreen mode Exit fullscreen mode

Closing

In conclusion Python contains many different ways to store values and several operators that can assign and compare them.

Helpful Sources

W3schools

Top comments (0)