Understanding Variables, Data Types, and Basic Syntax in Python

Thu May 23, 2024

Variables and Identifiers:

In Python, a variable is a symbolic name for a value or data.Variables are created by simply assigning a value to a name.

Example:

name = "John" age = 30

Identifiers:
Identifiers are the names of variables, must start with a letter (a-z, A-Z) or an underscore (_). They can be followed by letters, digits (0-9), or underscores.

Data Types:
Python supports a variety of data types:

int: Integers (e.g., 42, -10)
float: Floating-point numbers (e.g., 3.14, -0.5)
str: Strings (e.g., "Hello, World!")
bool: Booleans (True or False)
list: Ordered, mutable sequences (e.g., [1, 2, 3])
tuple: Ordered, immutable sequences (e.g., (1, 2, 3))
dict: Key-value mappings (e.g., {"name": "John", "age": 30})
set: Unordered collection of unique values (e.g., {1, 2, 3})
NoneType: Represents the absence of a value (None)

Basic Syntax:

Indentation:
Python uses indentation (whitespace) to define code blocks. Proper indentation is critical for code readability and functionality.

For example, in a conditional statement:

if condition:
# Code block
statement1
statement2

Comments:
Comments are used to add explanations within your code. In Python, comments start with a '#' symbol and continue until the end of the line. # This is a single-line comment """ This is a multi-line comment. It can span multiple lines. """

Print Statements:
To display output, you can use the print() function.
print("Hello, World!")

Input:
You can use input() to obtain user input as a string.
user_input = input("Enter your name: ") user_input = input("Enter your name: ")

Basic Arithmetic Operations:
addition = 5 + 3
subtraction = 8 - 2
multiplication = 4 * 6
division = 12 / 4


String Concatenation:
first_name = "John" last_name = "Doe" full_name = first_name + " " + last_name

Boolean Logic:
x = True y = False and_result = x and y or_result = x or y not_result = not x

Comparison Operators:
a = 5 b = 10 equal = a == b not_equal = a != b greater_than = a > b

Conditional Statements:
if condition: # Code to execute if condition is True
else: # Code to execute if condition is False


NOTE:
These are the fundamental concepts and syntax elements that you need to understand when working with Python. In the next steps of your Python journey, you'll build upon this foundation to create more complex programs and solve a wide range of problems.

Vijay Kashyap
Python Basics