DEV Community

Cover image for Variables, Operators, and Strings
Tejas Shinkar
Tejas Shinkar

Posted on

Variables, Operators, and Strings

Python Fundamentals — Operators & Strings

Variables

Definition

Variables are named memory locations used to store data.

Syntax

name = "Tejas"
age = 24
salary = 50000
Enter fullscreen mode Exit fullscreen mode

Key Points

  • No need to declare data type.
  • Python determines type automatically.
  • Variables are case-sensitive.

Interview Point

Python is a dynamically typed language.


Integer (int)

Definition

Stores whole numbers without decimal values.

Example

num = 100
print(type(num))
Enter fullscreen mode Exit fullscreen mode

Output:

<class 'int'>
Enter fullscreen mode Exit fullscreen mode

Interview Point

Integers in Python can store very large values without overflow issues seen in some languages.


Arithmetic Operators

Definition

Used to perform mathematical calculations.

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
// Floor Division
% Modulo
** Power

Example

a = 20
b = 3

print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)
Enter fullscreen mode Exit fullscreen mode

Key Observations

20 / 3   = 6.666...
20 // 3  = 6
20 % 3   = 2
20 ** 3  = 8000
Enter fullscreen mode Exit fullscreen mode

Interview Point

/ always returns float.


Comparison Operators

Definition

Used to compare two values.

Operators

<
>
<=
>=
==
!=
Enter fullscreen mode Exit fullscreen mode

Example

a = 10
b = 20

print(a < b)
print(a >= b)
print(a == b)
print(a != b)
Enter fullscreen mode Exit fullscreen mode

Output

True
False
False
True
Enter fullscreen mode Exit fullscreen mode

Interview Point

Comparison operators always return Boolean values.


Logical Operators

Definition

Used to combine multiple conditions.

Operators

and
or
not
Enter fullscreen mode Exit fullscreen mode

Example

age = 25
salary = 50000

print(age > 18 and salary > 30000)
print(age > 30 or salary > 30000)
print(not(age > 30))
Enter fullscreen mode Exit fullscreen mode

Interview Point

Python uses short-circuit evaluation.


Membership Operators

Definition

Checks whether a value exists inside a sequence.

Operators

in
not in
Enter fullscreen mode Exit fullscreen mode

Example

text = "DevOps"

print("Ops" in text)
print("AWS" not in text)
Enter fullscreen mode Exit fullscreen mode

Interview Point

Frequently used while searching logs and validating inputs.


Identity Operators

Definition

Compare memory locations of objects.

Operators

is
is not
Enter fullscreen mode Exit fullscreen mode

Example

a = [1, 2]
b = [1, 2]

print(a == b)
print(a is b)
Enter fullscreen mode Exit fullscreen mode

Output

True
False
Enter fullscreen mode Exit fullscreen mode

Interview Point

== compares values.

is compares object identity.


Bitwise Operators

Definition

Perform operations on binary values.

Operators

&
|
^
~
<<
>>
Enter fullscreen mode Exit fullscreen mode

Example

a = 5
b = 3

print(a & b)
print(a | b)
print(a ^ b)
print(~a)
Enter fullscreen mode Exit fullscreen mode

Interview Point

Important in networking, permissions, and low-level system operations.


Strings

Definition

A sequence of characters enclosed within quotes.

Example

name = "DevOps"

print(name)
print(type(name))
Enter fullscreen mode Exit fullscreen mode

Key Points

  • Strings are immutable.
  • Can use single, double, or triple quotes.
  • Supports indexing and slicing.

Interview Point

Strings cannot be modified in place because they are immutable.


Quick Revision

# Arithmetic
+  -  *  /  //  %  **

# Comparison
<  >  <=  >=  ==  !=

# Logical
and  or  not

# Membership
in  not in

# Identity
is  is not

# Bitwise
&  |  ^  ~  <<  >>
Enter fullscreen mode Exit fullscreen mode

Class Summary

This class covered Python data representation, arithmetic operations, comparisons, logical evaluation, object identity, bitwise operations, and strings. These concepts form the foundation for automation scripts, infrastructure tooling, API integrations, and cloud engineering workflows.

Top comments (0)