Code Crafter IT Innovation
Loading...
  • Bansal Computer Center
  • punjabedu.bansalcenter.com

Python Key Concepts

Identifiers in Python

An Identifier is a name used to identify a variable, function, class, or other object. It serves as the basic building block of a Python program.

Rules for Identifiers:

# Valid
student_name = "John"
val_1 = 50

# Invalid
1st_rank = "A" 
my-variable = 10 

Python Indentation

Unlike many other programming languages, Python uses indentation to define the structure of code blocks instead of curly braces {}.

Key Principles:

if 5 > 2:
    print("Five is greater than two!")
    # These lines are in the same block

Variables

Variables are reserved memory locations to store values. This means that when you create a variable, you reserve some space in the memory.

Declaration and Assignment:

Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable with the equal sign (=).

counter = 100          # An integer assignment
miles   = 1000.0       # A floating point
name    = "John"       # A string

print(counter)
print(miles)
print(name)

Multiple Assignment:

Python allows you to assign a single value to several variables simultaneously or assign multiple objects to multiple variables.

a = b = c = 1
x, y, z = 1, 2, "abc"

Variable Naming Conventions:

Constants in Python

A constant is a value that does not change during the execution of a program. Once defined, its value should remain the same.

Why use Constants?

Declaration Convention:

Python does not have "true" constants that are protected from change. Instead, programmers use a naming convention: constants are written in UPPERCASE letters.

PI = 3.14159
GRAVITY = 9.8
MAX_MARKS = 100

# Usage example
GST_RATE = 0.18
price = 1000
total = price + (price * GST_RATE)

Constant vs Variable

FeatureConstantVariable
Value Change❌ No✅ Yes
NamingUPPERCASElowercase
ExamplePI = 3.14age = 15

Dynamic Typing

Python is a dynamically-typed language. This is one of its most powerful features, allowing for high flexibility during development.

What does "Dynamic" mean?

In statically-typed languages (like Java or C++), you must declare the type of a variable before using it. In Python, the type is determined at runtime based on the value assigned to the variable.

# The variable 'data' changes type automatically
data = 10
print(type(data))  # Output: <class 'int'>

data = "Now I am a string"
print(type(data))  # Output: <class 'str'>

data = [1, 2, 3]
print(type(data))  # Output: <class 'list'>

Advantages:

Python Keywords

Keywords are reserved words in Python that have a special meaning to the interpreter. You cannot use a keyword as a variable name, function name, or any other identifier.

Key Characteristics:

Comprehensive List of Common Keywords:

FalseNoneTrueandas
assertasyncawaitbreakclass
continuedefdelelifelse
exceptfinallyforfromglobal
ifimportinislambda
nonlocalnotorpassraise
returntrywhilewithyield

How to Check Keywords in Python:

You can see the list of keywords directly in your Python environment by using the following code:

import keyword
print(keyword.kwlist)

What students Say?

Here are the stdudents...

#

Contact Us

Empower your tech dreams with us. Get in touch!

  • #
  • #
  • #
  • #
  • #