SomeQuiz

Course

Introduction to Python Programming

Co-Founder

Top Contributor

Nuwan Perera

@nuwanperera.me

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
KeywordDescription
andLogical operator
asAlias
assertfor debugging
breakBreak out of Python loops
classUsed for defining Classes in Python
continueKeyword used to continue with the Python loop by skipping the existing
defKeyword used for defining a function
delUsed for deleting objects in Python
elifPart of the if-elif-else conditional statement in Python
elseSame as above
exceptA Python keyword used to catch exceptions
FalseBoolean Value
FinallyThis keyword is used to run a code snippet when no exceptions occur
forDefine a Python for loop
fromUsed when you need to import only a specific section of a module
globleSpecify a verable scope as global
ifUsed for defining an "if" condition
importPython keyword used to import
inChecks if specified values are present in an iterable object
isUsed to test for equality
lambdaCreate anonymous functions
NoneThe None keyword represents a Null value in Python
nonlocalDeclare a variable with non-local scope
notLogical operator to negate a condition
orA logical operator used when either one of the conditions needs to be true
passThis Python keyword passes and lets the function continue further
raiseRaises an exception when called with the specified value
returnExits a running function and returns the value specified
TRUEBoolean value
tryPart of the try…except statement
whileUsed for defining a Python while loop
withCreates a block to make exception handling and file operations easy
yieldEnds 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.

  1. 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.
  1. 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 Identifierswhy?
123abcstarts with a digit
my-variablecontains a hyphen
classreserved keyword