Overview of Python:
Python is a high-level, interpreted programming language that was first released in 1991. It is a versatile language that can be used for various purposes such as web development, scientific computing, data analysis, artificial intelligence, and more. Python is known for its readability, simplicity, and ease of use.
Installing and setting up Python:
To install Python, you can download the installation file from the official Python website (https://www.python.org/downloads/). After downloading, run the installer and follow the instructions to complete the installation.
Running Python code using Jupyter notebook:
Jupyter notebook is a web-based interactive development environment that allows you to write and run code in a web browser. To run Python code using Jupyter notebook, you first need to install Jupyter notebook. This can be done using the following command in the command prompt:
Copy code
pip install jupyter
After installing, you can start a Jupyter notebook by running the following command:
Copy code
jupyter notebook
This will open a web browser with the Jupyter notebook interface, where you can create and run Python notebooks.
Basic data types and variables:
Python has several built-in data types, including:
Numbers (integers, floats, complex numbers)
Strings
Booleans
Lists
Tuples
Dictionaries
Sets
Variables are used to store data in Python. To create a variable, you can simply assign a value to a name using the '=' operator. For example:
makefile
Copy code
x = 5
This assigns the value 5 to the variable x. You can also assign values to multiple variables at once using the following syntax:
Copy code
x, y, z = 1, 2, 3
This assigns the values 1, 2, and 3 to the variables x, y, and z, respectively.
Operators and expressions:
Python supports several types of operators, including:
Arithmetic operators (+, -, *, /, %, **)
Comparison operators (==, !=, <, >, <=, >=)
Logical operators (and, or, not)
Bitwise operators (&, |, ^, ~, <<, >>)
Assignment operators (=, +=, -=, *=, /=, %=, **=, //=)
Expressions are combinations of operators and operands that produce a value. For example:
makefile
Copy code
x = 5
y = 3
z = x + y
This assigns the value 8 to the variable z, which is the result of adding the values of x and y.
Arvind Enterprises Group
Conditional statements (if-else)
Looping structures (for, while)
Using lists, tuples, and dictionaries
Conditional statements (if-else):
Conditional statements are used to execute code based on certain conditions. In Python, the if-else statement is used for this purpose. The general syntax of an if-else statement is as follows:
sql
Copy code
if condition:
# code to execute if condition is true
else:
# code to execute if condition is false
For example:
bash
Copy code
x = 5
if x > 0:
print("x is positive")
else:
print("x is non-positive")
This code will print "x is positive" since the condition x > 0 is true.
Looping structures (for, while):
Looping structures are used to execute a block of code multiple times. In Python, there are two types of loops: the for loop and the while loop.
The for loop is used to iterate over a sequence (such as a list or tuple) and execute a block of code for each item in the sequence. The general syntax of a for loop is as follows:
yaml
Copy code
for item in sequence:
# code to execute for each item
For example:
css
Copy code
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
This code will print each item in the list of fruits.
The while loop is used to execute a block of code as long as a certain condition is true. The general syntax of a while loop is as follows:
vbnet
Copy code
while condition:
# code to execute while condition is true
For example:
bash
Copy code
x = 1
while x < 5:
print(x)
x += 1
This code will print the numbers 1, 2, 3, and 4 since the condition x < 5 is true for these values of x.
Using lists, tuples, and dictionaries:
Lists, tuples, and dictionaries are used to store collections of data in Python.
A list is a collection of items that can be of different data types. Lists are mutable, meaning that their values can be changed after they are created. To create a list, use square brackets and separate the items with commas. For example:
css
Copy code
fruits = ["apple", "banana", "cherry"]
A tuple is similar to a list, but its values cannot be changed after it is created. To create a tuple, use parentheses and separate the items with commas. For example:
makefile
Copy code
fruits = ("apple", "banana", "cherry")
A dictionary is a collection of key-value pairs. Each value is associated with a unique key that can be used to access the value. To create a dictionary, use curly braces and separate the key-value pairs with commas. For example:
makefile
Copy code
ages = {"John": 25, "Mary": 30, "Bob": 35}
To access the values in a list, tuple, or dictionary, use the square bracket notation. For example:
bash
Copy code
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # prints "apple"
ages = {"John": 25, "Mary": 30, "Bob": 35}
print(ages["Mary"]) # prints 30
0 Comments