WEEK-4
Basic Programming Concepts: Syntax ; Tokens and types; Variable -Rules for
creating variables; constants; data types; errors ; comments ; Best programming
practices;
BASIC PROGRAMMING CONCEPTS:
Syntax:
Syntax refers to the set of rules that define how you write a program in a
programming language.
Syntax is like the grammar of a programming language—it tells you how to write
correct code so the computer can understand it.
If code violates these rules, the interpreter or compiler will raise a syntax error and
fail to execute the program.
Syntax is the set of rules that specify the correct structure and format of statements in a
programming language.
Python is known for its simple and readable syntax, which often uses indentation
to define code blocks
TOKENS AND TYPES:
The smallest individual unit in a program is called token.
All statements are built using tokens.
Tokens include keywords (reserved words like if, for, print), identifiers (variable /
function names), literals (raw values like 10 or "hello"), operators (+, =, *), and
delimiters (parentheses, commas)
🔹 Types of Tokens
1. Keywords / Reserved Words
o Special words reserved by the language for a specific purpose
o Cannot be used as identifiers
o Examples: int, float, if, while, return
2. Identifiers
o Names given to variables, functions, arrays, classes, or objects
o Must follow naming rules of the language
o Examples: age, sum, studentName
3. Constants / Literals
o Fixed values in the program
o Examples: 10, 3.14, 'A', "Hello"
4. Operators
o Symbols used to perform operations on variables or values
o Examples: +, -, *, /, %, =, ==
5. Punctuators / Separators / Special Symbols
o Symbols that structure the program
o Examples: ; (statement terminator), , (comma), () (parentheses), {} (braces)
VARIABLES:
A variable is a symbolic name given to a memory location used to store data that can
be modified during the execution of a program.
The quantity which changes during the execution of the program is known as
variable.
In Python, there is no need to declare a variable explicitly as in C, C++, Java by
using int, float, etc.
To define a variable in Python, simply assign a value to a name as
given below. Ex:
A= 20
B = “hello”
RULES FOR CREATING VARIABLES:
Variable name consists of any number of letters, underscores and digits.
Variable name should not start with a digit. Variable names must start with a letter or
underscore.
Keywords cannot be used as variable names.
Any other special character or white spaces are strictly prohibited in identifiers.
Variable names are case sensitive. So total, TOTAL and Total are 3 different
variables in Python.
Variable names should be meaningful
CONSTANTS:
Constants are data containers whose values do not change throughout the program's
execution.
Python doesn't have built-in strict constant types like some other languages; the
convention is to define them using all uppercase
letters with underscores to separate words
(e.g., PI , MAX_SIZE ).
DATA TYPES :
A data type is a classification that specifies the type of data a variable can hold and the
operations that can be performed on it.
In simple words:
👉 Data types tell the computer what kind of value a variable can store, like numbers, text, or
true/false values.
Python has several built-in datatypes:
Numbers: Stores numeric values.
o int (integers, e.g., 10)
o float (floating-point numbers, e.g., 20.8)
String (str): Stores a sequence of characters, enclosed in single
or double quotes (e.g., "hello", 'world').
List (list): Ordered, changeable collection of items (e.g., [1, 2, 'apple']).
Tuple (tuple): Ordered, unchangeable collection of items (e.g., (1, 2, 'apple')).
Dictionary (dict): Unordered collection of key-value pairs (e.g., {'name': 'John', 'age':
30}).
Boolean (bool): Represents truth values, either True or False.
ERRORS:
In basic programming, an error is a problem in the code that prevents the program
from running correctly.
Errors can occur during program execution:
1. Syntax Errors: Occur when the code violates the language's rules (e.g., a missing
quote or incorrect indentation). These are usually detected before the program runs.
2. Runtime Errors: Occur while the program is running and the program crashes or stops
unexpectedly
3. Logical Errors: Program runs without crashing, but produces wrong results and it is hard to
detect
4. Latent Errors: Hidden errors that only appear when specific, often unusual input
data is used.
COMMENTS:
Comments are non executable statements.
Comments can be used to explain the code.
Comments can be used to make the code more readable.
In Python, comments start with # and Python Interpreter will ignore them.
Comments can be placed at the end of the line and python will ignore the
rest of the line. Ex:
Multiline comments:
To add a multi line comment insert #
for each line. Ex:
OR
You can enclose text in triple single quotes or triple double quotes (''' or """)
BEST PRACTICES OF PYTHON PROGRAMMING:
Python best practices that can be really useful for Python programmers to improve
their coding experience.
1. Writing Well-Structured Code
2. Having Proper Comments and Documentation
3. Keep Code Simple and Readable.
4. Proper Naming of Variables, Classes, Functions and Modules.
5. Use Functions: Break down complex tasks into smaller, reusable functions to make
code modular and easier to test.
6. Handle Errors Gracefully: Use try and except blocks to anticipate and manage
potential runtime errors.
7. Use Version Control: Tools like Git/GitHub are essential for tracking changes
and collaborating with others.
8. Write Tests: Implement unit tests to ensure individual parts of your code work as
intended.
9. Use an IDE: Integrated Development Environments (like PyCharm or VS Code)
provide tools for code completion, debugging, and following best practices.