Python Basic
Python Basic
Introduction
Python is described as a high-level, interpreted, dynamically typed programming
language.
● It was created by Guido van Rossum and first released in 1991.
Python's syntax is designed to be readable and concise, emphasizing code readability
and simplicity.
● Standard Library: Python comes with an extensive standard library that includes
a wide range of modules and packages. These modules provide pre-built
functionalities for tasks like file I/O, networking, data manipulation, and more,
enhancing code reusability and simplifying development.
Variables
A variable in Python is the name of a place in memory where data is kept. It serves as a
container to store manipulable and useful values for the programme. Because Python is
dynamically typed, unlike some other programming languages, you are not required to
explicitly specify the data type of a variable. Using the variable's value as a starting
point, the data type is deduced.
Declaring a variable
Using the create variable command, you may provide a variable a name and a value.
Variable Naming Rules:
● Variable names can only contain letters (a-z, A-Z), digits (0-9), and underscores
(_).
● The first character of a variable name cannot be a numeric character
● Variable names are case-sensitive (e.g., age, Age, and AGE are three different
variables).
● Avoid using reserved keywords as variable names (e.g., if, while, for, and, etc.).
Keywords
Keywords in Python are reserved words that have specific meanings and functionalities
within the Python language. These words cannot be used as identifiers (variable names,
function names, etc.) because they are already predefined and serve specific purposes
in the language's syntax and semantics.
Datatype in python
Datatypes in Python refer to the categorization of data based on its nature and the
operations that can be performed on it. Python is a dynamically typed language,
meaning the data type of a variable is determined at runtime based on the value
assigned to it. Here are some common data types.
Type of datatype :
1. Integer
2. Float
3. Boolean
4. String
5. List
6. Tuple
7. Set
8. Dictionary
Type Casting
Type casting, also known as type conversion, refers to the process of converting a
variable from one data type to another. Python provides built-in functions that allow you
to perform typecasting between compatible data types.
Operators
Operators in Python are special symbols or characters used to perform various
operations on variables and values. Python supports a wide range of operators, which
can be categorized into several groups:
Types of operators
1. Arithmetic
2. Comparison
3. Logical
4. Identity
5. Membership
6. Assignment
Decision-Making:
● Conditional statements provide the ability to make decisions in code based on
certain condition.
● When the condition is True, the code block under the if or elif (if used) is
executed.
● When the condition is False, the code block under the else (if used) is executed,
or the program continues to the next part of the code.
Loops in Python
Loops are significant programming control structures that let us run a block of code
repeatedly until a certain condition is satisfied. They assist in handling vast amounts of
data effectively and minimizing repetitive operations.
Types of loops
● for loop
● While loop
For loop-
The for loop is used when you know how many iterations there will be or when you want
to loop through a sequence of elements (such as a list, tuple, or string).
It performs a code block execution for each iteration while iterating over a sequence,
adding each element to a variable.
example-
s = [1,2,3]
for i in s:
print(fruit)
While loop:
The while loop is used when the number of iterations is unknown in advance, and the
loop continues as long as a defined condition remains True.
It executes the code block periodically as long as the condition is true.
Example-
count = 0
while count < 5:
print("Count:", count)
count += 1