Python Track · Part 1 of 9

Python Basics: Variables and Data Types

Python
Python Basics
An introduction to Python, its interpreters, and core data types: numbers, strings, lists, tuples, dictionaries and basic operators.
Author

Gujuri Alekha

Published

Oct 20, 2024

1.1 What is Python?

  • Python is a high-level, interpreted, and general-purpose programming language.
  • It was created by Guido van Rossum and first released in 1991.
  • Python is known for its readability and simplicity, which makes it an excellent choice for beginners and experienced developers alike.
  • It has a large and active community, providing extensive libraries and frameworks for various applications.

1.1.1 Python Interpreters

  • Python provides different ways to interact with the language, including interactive shells and integrated development environments (IDEs).
  • IDLE: IDLE is a simple integrated development environment that comes bundled with Python. It provides a Python shell where you can execute code interactively and write scripts.
  • Jupyter Notebook: Jupyter Notebook is a web-based interactive computing environment that allows you to create and share documents containing live code, equations, visualizations, and narrative text. It’s widely used for data analysis and data science tasks.

1.2 Variables and Data Types

1.2.1 Numeric Types

  • Integers (int): Integers are whole numbers without a decimal point. For example, 5 and -10 are integers.
  • Floats (float): Floats represent real numbers with a decimal point. Examples include 3.14 and -0.5.
  • Complex Numbers (complex): Complex numbers consist of a real part and an imaginary part. For instance, 3 + 2j is a complex number.

1.2.2 Strings and String Manipulation

  • Strings (str): Strings are sequences of characters enclosed in single (’ ‘), double (” “), or triple (’’’ ’’’ or”“” “““) quotes. Examples:”Hello, Python!” or ’12345’.
  • String Manipulation: Python provides various methods and operators for manipulating strings, such as concatenation, slicing, indexing, and formatting.

1.2.3 Lists, Tuples, and Dictionaries

  • Lists (list): Lists are ordered collections of items enclosed in square brackets [ ]. They can contain elements of different data types and are mutable (can be modified).
  • Tuples (tuple): Tuples are similar to lists but enclosed in parentheses ( ). They are immutable (cannot be changed after creation).
  • Dictionaries (dict): Dictionaries are unordered collections of key-value pairs enclosed in curly braces { }. Keys are unique, and they are used to access values. Dictionaries are mutable.

1.2.4 Variables, Assignments, and Basic Operators

  • Variables: Variables are used to store data in Python. You can assign values to variables using the assignment operator (=). For example: x = 10 assigns the value 10 to the variable x.
  • Basic Operators: Python supports various operators for performing operations on variables and values. Common operators include:
    • Arithmetic operators (+, -, *, /, // for floor division, % for modulo)
    • Comparison operators (==, !=, <, >, <=, >=)
    • Logical operators (and, or, not)
    • Assignment operators (+=, -=, *=, /=, etc.)
    • Membership operators (in, not in) for checking if an element is in a container
    • Identity operators (is, is not) for comparing object identity
Show code
# Numeric types
integer_num = 5
float_num = 3.14
complex_num = 3 + 2j

# Strings and String Manipulation
str_variable = "Hello, Python!"
substring = str_variable[0:5]                    # Extracts "Hello"
concatenated_str = str_variable + " Welcome!"  # Concatenation

print(f"str_variable : {str_variable}")
print(f"substring : {substring}")
print(f"concatenated_str : {concatenated_str}")
str_variable : Hello, Python!
substring : Hello
concatenated_str : Hello, Python! Welcome!
Show code
# Lists, Tuples, and Dictionaries
my_list = [1, 2, 3]
my_tuple = (4, 5, 6)
my_dict = {'name': 'Gujuri Alekha', 'age': 30}
my_set = {2,3,4,'Gujuri Alekha'}

print(f"my_list : {my_list}")
print(f"my_tuple : {my_tuple}")
print(f"my_dict : {my_dict}")
print(f"my_set : {my_set}")
my_list : [1, 2, 3]
my_tuple : (4, 5, 6)
my_dict : {'name': 'Gujuri Alekha', 'age': 30}
my_set : {2, 3, 4, 'Gujuri Alekha'}
Show code
# Variables, Assignments, and Basic Operators
x = 10
y = 5
result = x + y                           # Arithmetic operation
is_equal = x == y                        # Comparison
logical_result = (x > 5) and (y < 10)   # Logical operation


print(f"result : {result}")
print(f"is_equal : {is_equal}")
print(f"logical_result : {logical_result}")
result : 15
is_equal : False
logical_result : True

Citation

BibTeX citation:
@online{alekha2024,
  author = {Alekha, Gujuri},
  title = {Python {Basics:} {Variables} and {Data} {Types}},
  date = {2024-10-20},
  url = {https://gujurialekha.github.io/blog/python/01-python-basics-variables-and-data-types.html},
  langid = {en}
}