Python Programming
Python Programming
02 _ APSCHE
This interactive session helps us in learning about Python Interpreter, Compiler, Platforms,
Versions, Fundamentals, Comments, Variables, Keywords, and Datatypes.
Key Points:
Machines understand 0 and 1 but writing 0 & 1 not at all human friendly hence we need
programming languages.
Python usually uses interpreter, goes through code line by line and converts it to machine
code.
Compiler takes your code all at once and translates it to machine code.
Python code --> Interpreter --> line by line --> byte code --> runs on cpython vm --> runs on
our PC
We need a terminal, Code editor, IDE, and Execution Platform (Jupyter).
Comments in Python are annotations within the code that are ignored by the Python
interpreter. They are used to explain the code or provide additional information for anyone
reading it.
There are two ways to write comments in python:
1. Single-line comments: Single-line comments start with the # symbol and continue
until the end of the line.
2. Multi-line comments or Docstrings: Multi-line comments can be enclosed within
triple quotes (''' or """) and are often used for documentation strings (docstrings) to
provide information about functions, modules, or classes.
Variables can be declared in many ways, they are:
1. Snake Case
This is the most common convention in Python. Variable names are written
in lowercase, with words separated by underscores.
Ex: your_age
2. Camel Case
Words are capitalized except for the first word, and there are no spaces
between words. While not as common in Python, it's occasionally used,
especially in situations where consistency with other languages is required.
Ex: yourAge
3. Pascal Case
Similar to Camel Case, but with the first letter of every word capitalized. This
convention is often used for naming classes in Python.
Ex: YourAge
4. UPPERCASE
Variables written entirely in uppercase are typically used for constants.
Ex: MAX_AGE or MAX
5. Mixed Case
Sometimes, a combination of lowercase, uppercase, and underscores is used
for variable names. This is less common in Python and should be used
judiciously to maintain readability.
Ex: yourAge or YourAge
"Dunder" is shorthand for "double underscore," and these methods are special methods in
Python that are surrounded by double underscores on both sides of their names.
Ex: __str__
Keywords in Python are reserved words that have special meanings and purposes within the
language. These keywords cannot be used as identifiers (such as variable names or function
names) because they are already predefined and serve specific roles in Python syntax and
semantics.
Ex: and, if
Datatypes in python are:
1. Integers (int)
2. Floating-point numbers (float)
3. Strings (str)
4. Booleans (bool)
5. Lists
6. Tuples
7. Dictionaries (dict)
8. sets
An arithmetic expression is something in which mathematical operations are involved.
In arithmetic expressions, the order of operations, often remembered by the acronym
PEMDAS, specifies the sequence in which different mathematical operations should be
performed:
1. Parentheses
2. Exponents
3. Multiplication and Division
4. Addition and Subtraction
Strings are sequences of characters enclosed within either single (' ') or double (" ")
quotation marks in Python. They are immutable, meaning once created, their contents
cannot be changed.
The process of joining two or more strings is known as string concatenation.
Ex: message = "Hello" + " " + "world" # message is now "Hello world"
Individual characters in a string can be accessed using indexing. Python uses zero-based
indexing, meaning the index of the first character is 0.
print(string[0]) # Output: H
The process of extracting a substring from a string using slicing notation, which specifies a
range of indices is known as slicing.