Program Structure of Python
A basic program in Python is made up of a sequence of statements that are executed consecutively. Some basic Building blocks of Python program follows:
Importing Modules
Python has large collection of standard libraries and modules that enhance its capability. Using the 'import' keyword, you may import modules at the start of your program.
Functions and Methods
Functions are reusable modules of code that execute specified tasks. You may define your own functions and invoke them later in the program by using the def keyword.
Conditional Statements
These statements allow to run different part of codes based on particular criteria. Python has conditional statements which are executed using keywords 'if ' , 'elif ', and 'else'.
**NOTE**
Python is a 'case sensitive' language which means that uppercase and lowercase are treated distinctively.
Loops
Loops are used for the repeating execution of a block of code. Python has two kinds:- 'for' and 'while'.
See more about Loops in Later Posts.
Indentation
In Python, a block represents a group of statements that show the logic of a particular statement. Indentation plays a major role in Python to execute a set of statements. It refers to the space or tabs used between statements. A colon(:) is used to include all the statements belonging to that particular block.
Importance of Indentation:
Above you can see statement next to 'while' is indented with a space , also inside the loop, the statements inside it are indented properly. Else they would generate an error in program.
Debugging
Debugging is the process of locating and fixing errors in the program.
Types of Errors
Syntax Errors
The errors which occur due to violation of the laws of syntax of a programming language are known as Syntax errors. They occur mainly due to incorrectly typed statements or grammatical mistakes in the programming language.
The first statement generates an error due to wrong spelling of 'print'.
The second one is executed properly.
Logical Syntax
Logical errors are executed but derive an incorrect logic.
The above code is executed but as seen we didn't get the desired output. We have input two values and want the sum but instead we have subtracted, which is a logic error.
Linking Errors
These errors occur during the compilation process. For example , absence of math module in the program and math.sqrt is used in program, then an error message will be displayed.
Run Time Errors
Errors that occur during the execution of a program.
EXAMPLES-
- Dividing a number by 0
- Finding square root of a negative number
- Finding logarithmic value of a negative number
Variables in Python
Variables are used to store some values for performing various operations. Just like in real life, you need to store some food for future use, you require containers, here we require variables.
Rules for Declaring Variable Names
- Variable name must start with a letter(upper or lower -cases) or an underscore( _ )
- A variable name can consist of letters, digits, and underscore. Not any other character allowed.
- Variable names are case- sensitive.
- Python keywords should not be used.
Variable | Valid / Invalid | Reason |
---|---|---|
name |
✅ Valid | Starts with a letter, contains only letters. |
Name123 |
✅ Valid | Starts with a letter, numbers allowed after first character. |
_value |
✅ Valid | Underscore can be the first character. |
value_2 |
✅ Valid | Contains letters, numbers, underscore; starts with letter. |
VALUE |
✅ Valid | All caps is fine; Python is case-sensitive. |
n |
✅ Valid | Single letter variables allowed. |
__hidden |
✅ Valid | Double underscore allowed; often used for special purposes. |
2name |
❌ Invalid | Cannot start with a number. |
first-name |
❌ Invalid | Hyphen - not allowed (interpreted as minus). |
my var |
❌ Invalid | Spaces not allowed in variable names. |
if |
❌ Invalid | Reserved keyword in Python. |
while |
❌ Invalid | Reserved keyword in Python. |
123 |
❌ Invalid | Cannot start with a digit. |
class |
❌ Invalid | Reserved keyword in Python. |
@value |
❌ Invalid | Special characters like @ not allowed. |
value$ |
❌ Invalid | $ is not allowed in Python identifiers. |
Literal
A literal is a value given to a variable. Literals can be numbers, fractions, words or letters. For Example:- 5 is a numerical constant, 3.14 is a fractional/decimal constant, "HelloWorld" is a word(string) constant. These are the examples of values assigned to variables in Python.
0 Comments