What is Python
Python is a object-oriented, interpreted and high-level programming language with dynamic semantics known for its simplicity, readability, and versatility.
What Exactly Is High-Level Language?
A high-level language is a programming language that is designed to be easily understandable and readable by humans. It provides a level of abstraction from the underlying hardware and low-level details, making it easier to write, understand, and maintain code compared to low-level languages.Some examples of popular high-level programming languages include Python, Java, C#, JavaScript, Ruby, and PHP.
What does "interpreted language" mean?
An interpreted language is a programming language in which the source code is executed directly by an interpreter without the need for explicit compilation. In an interpreted language, the interpreter reads and executes the code line by line, converting each line into machine code or bytecode on-the-fly and immediately executing it.
What is object-oriented programming?
Object-oriented programming (OOP) is a programming paradigm that organizes code around objects, which are instances of classes. It provides a way to structure and design software by grouping related data and behavior together into objects.
Dynamic Semantics?
In Python, dynamic semantics refers to the behavior of the language where the type of a variable is determined at runtime, and objects can change their type or behavior during program execution. Python is dynamically typed, meaning you do not need to declare the type of a variable explicitly. Instead, the type of a variable is inferred based on the value assigned to it.
Here are some key aspects of dynamic semantics in Python:
-
Dynamic Typing: Python allows variables to hold values of different types. You can assign different types of values to the same variable without explicitly declaring its type. For example, a variable can hold an integer value at one point and later be assigned a string value.
-
Type Inference: Python automatically infers the type of a variable based on the value assigned to it. For example, if you assign an integer value to a variable, Python will consider it as an integer type and perform integer operations on it. If you then assign a string value to the same variable, Python will treat it as a string type and allow string operations on it.
-
Late Binding: In Python, variable names are bound to objects dynamically at runtime. This means that you can reassign a variable to refer to a different object of a different type during program execution.
-
Duck Typing: Python follows the principle of "duck typing," which means that the suitability of an object for a particular operation is determined by its behavior rather than its specific type. If an object supports the required methods or attributes, it can be used in that context, regardless of its explicit type.
Keywords and Identifiers
Keywords in Python
In Python, keywords are reserved words that have predefined meanings and cannot be used as identifiers (such as variable names or function names). These keywords are an integral part of the language syntax and serve specific purposes in the code. Here is a list of Python keywords as of the Python 3.9 version:
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
Keyword | Description |
---|---|
and | Logical operator |
as | Alias |
assert | for debugging |
break | Break out of Python loops |
class | Used for defining Classes in Python |
continue | Keyword used to continue with the Python loop by skipping the existing |
def | Keyword used for defining a function |
del | Used for deleting objects in Python |
elif | Part of the if-elif-else conditional statement in Python |
else | Same as above |
except | A Python keyword used to catch exceptions |
False | Boolean Value |
Finally | This keyword is used to run a code snippet when no exceptions occur |
for | Define a Python for loop |
from | Used when you need to import only a specific section of a module |
globle | Specify a verable scope as global |
if | Used for defining an "if" condition |
import | Python keyword used to import |
in | Checks if specified values are present in an iterable object |
is | Used to test for equality |
lambda | Create anonymous functions |
None | The None keyword represents a Null value in Python |
nonlocal | Declare a variable with non-local scope |
not | Logical operator to negate a condition |
or | A logical operator used when either one of the conditions needs to be true |
pass | This Python keyword passes and lets the function continue further |
raise | Raises an exception when called with the specified value |
return | Exits a running function and returns the value specified |
TRUE | Boolean value |
try | Part of the try…except statement |
while | Used for defining a Python while loop |
with | Creates a block to make exception handling and file operations easy |
yield | Ends a function and returns a generator object |
It is important to note that the keywords are case-sensitive, meaning that using uppercase or lowercase variations of these keywords will not change their meaning.
Identifiers in Python
In Python, identifiers are names used to identify variables, functions, classes, modules, or other objects.
Rules for Writing Identifiers
There are some rules for writing Identifiers.
- Naming Rules:
- Identifiers must start with a letter (a-z, A-Z) or an underscore (_).
- The rest of the identifier can contain letters, digits (0-9), or underscores (_).
- Identifiers are case-sensitive, so "myVar" and "myvar" are considered different.
- Python allows the use of Unicode characters in identifiers.
- Convention:
- Use descriptive names that convey the purpose or meaning of the identifier.
- For multi-word identifiers, follow the convention of using underscores between words (e.g., my_variable).
- Avoid using reserved keywords as identifiers.
Example of Valid Identifiers
my_variable
counter
MAX_SIZE
_private_variable
CalculateArea
Invalid Identifiers | why? |
---|---|
123abc | starts with a digit |
my-variable | contains a hyphen |
class | reserved keyword |