Table of contents
What is Python?
Python is an interpreted, object-oriented, high-level, open-source, general-purpose programming language.
Python is Interpreted: Python is processed at runtime by the interpreter You do not need to compile your program before executing it. This is similar to PERL and PHP.
Python is Interactive: You can actually sit at a Python prompt and interact with the interpreter, directly to write your programs.
Python is Object Oriented: Python supports an Object-Oriented style or technique of programming that encapsulates code within objects
It was first released in 1991 by Guido van Rossum
Python consists of vast libraries and various frameworks like Django, Tensorflow, Flask, Pandas, Keras, etc.
Python Setup
To setup Python in Windows, follow these steps:
Download the latest version of Python from the official Python website (python.org/downloads/windows) based on your Windows version (32-bit or 64-bit).
Run the downloaded installer and follow the on-screen instructions to install Python on your system.
During the installation, make sure to select the option to add Python to the PATH environment variable. This will make it easier to run Python from the command prompt.
Once the installation is complete, open the command prompt (Windows key + R, type cmd, and hit enter) and type Python. This should start the Python interpreter and display the version information.
To start writing Python code, you can use any text editor of your choice, such as Notepad or Visual Studio Code. Save the file with a .py extension.
To run a Python script, open the command prompt, navigate to the directory where the script is located using the cd command, and then type python followed by the name of the script (e.g., python script.py).
To setup Python in Linux, follow these steps:sudo apt-get install python3
sudo apt-get install python3
Once Python is installed, you can start writing Python code in any text editor of your choice, such as Nano, Vim, or Emacs. Save the file with a .py extension.
To run a Python script, open the terminal, navigate to the directory where the script is located using the cd command, and then type:python3 script.py
python3 script.py
This will run the script using the Python interpreter.
You can also use the interactive Python shell by typing the command:python3
python3
This will open the Python interpreter, where you can enter Python commands and see their output immediately.
Checking the version of pythonpython3
python3 --version
Variables
Variables are containers or memory locations for storing data values.
To create a variable in Python, you need to follow these rules:
Choose a name for your variable that is meaningful and descriptive. The name can contain letters, numbers, and underscores, but it cannot start with a number.
Assign a value to the variable using the equals sign (=). You do not need to declare the type of the variable explicitly, as Python is a dynamically typed language.
Here's an example of how to create a variable in Python
name="Dwarika"
age = 26
height = 5.10
is_student = True
In this example, we have created four variables:
name
is a string variable that stores the value "Dwarika".age
is an integer variable that stores the value 26.height
is a float variable that stores the value 5.10.is_student
is a Boolean variable that stores the value True.
"Dwarika", 26, 5.10, True these all are different data types.
What is data type?
Data types are used to classify and categorize different types of data values that can be used in a program.
In Python, data types can be classified as either mutable or immutable.
An immutable data type is one whose value cannot be changed once it is created It includes Numbers, strings, and tuples
Numbers: Python has three types of numbers:
integers (int) eg= 1,10,200 etc
floating-point numbers (float) eg= 5.100000
complex(imaginary) eg=6j
strings: A string is a sequence of characters enclosed in single quotes, double quotes, or triple quotes.
- eg='d', "Dwarika", """he is learning devops"""
Tuples: A tuple is a collection of ordered, immutable elements enclosed in parentheses.
- eg=(10, 1.5, "dwarika", "etc")
Booleans: A Boolean value is either True or False.
A mutable data type is one whose value can be changed after it is created. It includes lists, sets, and dictionaries.
Lists: A list is a collection of ordered, mutable elements enclosed in square brackets.
- eg: list=[10, 1.5, "dwarika", "etc"]
Sets: A set is an unordered collection of unique elements enclosed in curly braces.
- eg: sets={10, 1.5, "dwarika", "etc"}
Dictionaries: A dictionary is an unordered collection of key-value pairs enclosed in curly braces.
- eg: dict={"name": "Dwarika", "age":26, "height":5.10, "is_student":"True" }
datatypes can be checked by using the type() method
print(type())