Python Programming

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

PYTHON PROGRAMMING

INTRODUCTION

Python is a general-purpose, open-source programming language that


promotes rapid program development and readable code.

KEYWORDS

A keyword is a word having a special meaning reserved by a programming


language.

Python programming language contains the following keywords:

COMMENTS

• Comments can be used to explain Python code.


• Comments can be used to make the code more readable.
• Comments can be used to prevent execution when testing code.
• Single-line comments start with the '#' symbol, and multi-line
comments are enclosed within triple quotes (""").

CONSTANTS

A constant is a variable whose value cannot be changed throughout the


program.

VARIABLES

• Variables in Python are used to store and manipulate data.


• They have a name and a value associated with them.
• Variables can be assigned different values throughout the program.
• Examples of variable declarations:
• age = 12
• name = "John"
• temperature = 25.5

VARIABLE NAMES

Identifiers are the names given to different parts of the program which are
variables, objects, classes, functions, lists, dictionaries, and so forth.

The naming rules for Python identifiers can be summarized as follows:

➢ A variable name must start with a letter or the underscore character


➢ A variable name cannot start with a number
➢ A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _)
➢ Variable names are case-sensitive (age, Age, and AGE are three
different variables)
➢ A variable name cannot be any of the Python keywords.

Python is case-sensitive as it treats upper and lower-case letters differently.

Examples of valid variable names:


• name = “John”
• my_name = “John”
• _name = “John”
• NAME = “John”
• Name92 = “John”

Examples of invalid variable names:


• 92name = “John”
• my name = “John”
• my-name = “John”
MULTIPLE ASSIGNMENTS

ASSIGNING THE SAME VALUE TO MULTIPLE VARIABLES

You can assign the same value to multiple variables in a single statement, e.g.,

a = b = c = 10

It will assign a value of 10 to all three variables a, b, and c.

ASSIGNING MULTIPLE VALUES TO MULTIPLE VARIABLES

You can even assign multiple values to multiple variables in a single statement,
e.g.,

x, y, z = 10, 20, 30

It will assign the values order-wise. That means the above statement will assign
values 10 to x, 20 to y, and 30 to z.

DATA TYPES

Every value in Python has a data type that determines the type of
operations that can be performed on it.

Common data types in Python include:

• Integer (int): Whole numbers without decimal points, like 10 or -5.


• Float (float): Numbers with decimal points, like 3.14 or -2.5.
• String (str): Textual data enclosed in quotes, like "Hello" or 'Python'.
• Boolean (bool): Represents either True or False.
OPERATORS

Operators are used to perform operations on variables and values.

ARITHMETIC OPERATORS
Arithmetic operators are used to perform mathematical operations in Python.

Common arithmetic operators:

• Addition (+): Adds two values together.


• Subtraction (-): Subtracts one value from another.
• Multiplication (*): Multiplies two values.
• Division (/): Divides one value by another.
• Modulus (%): Returns the remainder of a division.
• Floor division (//): Rounds the result down to the nearest whole number
• Exponentiation (**): Raises a value to the power of another value.

LOGICAL OPERATORS
Logical operators are used to perform logical operations in Python.

Common logical operators:

• AND: Returns True if both conditions are True.


• OR: Returns True if at least one condition is True.
• NOT: Returns the opposite of the condition.

You might also like