Basics of Python

code, coding, computer

Identifiers in Python

  • An identifier is a name used to identify a variable, function, class, module, or other objects. There are some rules and conventions to follow when naming an identifier in Python:
  • Name can contain letters (upper-case or lower-case), numbers, and underscores, but cannot start with a number.
  • Python is case-sensitive, so names like “myVariable” and “myvariable” are different.
  • Python reserves some words as keywords, such as if, else, for, while, etc., which cannot be used as identifier names.
  • Avoid using common words and symbols as identifier names, to prevent conflicts with built-in or standard library functions and modules.
  • Use descriptive and meaningful names for your identifiers, so that your code is easy to read and understand.

For example,
The following are valid Python identifier names:
myVariable
_private
MY_CONSTANT
sum_of_squares


And the following are invalid Python identifier names:
123abc
if
else
for
while

Python Keywords

In Python, keywords are reserved words that have a special meaning and cannot be used as identifier names for variables, functions, classes, or any other objects.
Here is a list of Python keywords:
and, as, assert, async, await, break, class, continue, def, del, elif, else, except, False, finally, for, from, global, if, import, in, is, lambda, None, nonlocal, not, or, pass, raise, return, True, try, while, with, yield.
Note that the keywords may change with new versions of Python.

To get a list of Python keywords, you can use the keyword module:

import keyword

keywords = keyword.kwlist

print(keywords)

Statements

A statement is a line of code that performs a specific action.
A block of code is made up of one or more statements.
Here are some common types of statements in Python:
Assignment statement: assigns a value to a variable.
x = 10

Expression statement: evaluates an expression and discards the result.
x + 10

Conditional statement: executes a block of code based on a condition.
if x > 0:
print(“x is positive”)


Looping statement: repeatedly executes a block of code.
for i in range(5):
print(i)


Return statement: returns a value from a function.
return x * x

Raise statement: raises an exception.
raise ValueError(“Invalid argument”)

Note that each statement must end with a newline or a semicolon. However, it is recommended to use a newline instead of a semicolon to improve readability.

Expressions in Python Programming

An expression is a piece of code that evaluates to a value.
Expressions can be used as part of larger statements or as standalone statements.

Here are some common types of expressions in Python:
Arithmetic expressions: perform arithmetic operations like addition, subtraction, multiplication, division, etc.
2 + 3
4 * 5


Comparison expressions: compare two values and return a Boolean value indicating if the comparison is true or false.
2 < 3
4 == 5


Logical expressions: perform logical operations like and, or, not and return a Boolean value.
(2 < 3) and (4 < 5)
not (2 < 3)


Function call expressions: call a function and return its value.
len([1, 2, 3])
max(2, 3)


String expressions: create and manipulate strings.
“Hello” + “ ” + “World”
“Hello”.upper()


List expressions: create and manipulate lists.
[1, 2, 3] + [4, 5, 6]
[1, 2, 3][0]


Tuple expressions: create and manipulate tuples.
(1, 2, 3) + (4, 5, 6)
(1, 2, 3)[0]


Dictionary expressions: create and manipulate dictionaries.
{“a”: 1, “b”: 2}[“a”]
{“a”: 1, “b”: 2}.keys()


The value of an expression can be used as input to other expressions or as part of larger statements, like an assignment statement or a function call.

Use of Variables in Python

A variable is a named location in memory that stores a value. Variables allow you to store values that can change during the execution of a program, and to refer to those values by using the variable name.
Here are some key points to keep in mind when using variables in Python:
Variable assignment: You can assign a value to a variable using the = operator.
For example:
x = 10

Variable names: A variable name must start with a letter or an underscore and can contain letters, digits, and underscores. It cannot contain spaces, punctuation, or start with a number.

Data types: Python is dynamically typed, which means that the type of a variable is determined by the value that is assigned to it.
For example, x = 10 creates an integer variable, while x = “Hello” creates a string variable.

Multiple assignments: You can assign multiple values to multiple variables in a single statement, like this:
x, y, z = 10, 20, 30

Variable scope: Variables defined inside a function are local to that function, and are not accessible from outside the function. Variables defined outside of any function are called global variables and can be accessed from anywhere in the program.

Type casting: You can convert a variable from one type to another using type casting functions like int, float, str, list, tuple, etc.
For example:
x = 10
y = float(x)


Garbage collection: Python automatically manages the memory used by variables. When a variable is no longer referenced, its memory is automatically released, making it available for reuse.
By using variables, you can make your programs more flexible and easier to maintain. They are an essential tool for any Python programmer.

To buy Python books click the below links
Python Programming
Python Programming 2E
Handbook of Python Programming

Leave a Comment

Your email address will not be published. Required fields are marked *