Basics of Python

Basics of Python

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:

  1. 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).

  2. Run the downloaded installer and follow the on-screen instructions to install Python on your system.

  3. 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.

  4. 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.

  5. 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.

  6. 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

  1.  sudo apt-get install python3
    

  2. 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.

  3. 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.

  4. 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.

  5. 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:

  1. 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.

  2. 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

    1. 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

    2. strings: A string is a sequence of characters enclosed in single quotes, double quotes, or triple quotes.

      • eg='d', "Dwarika", """he is learning devops"""
    3. Tuples: A tuple is a collection of ordered, immutable elements enclosed in parentheses.

      • eg=(10, 1.5, "dwarika", "etc")
    4. 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.

  1. Lists: A list is a collection of ordered, mutable elements enclosed in square brackets.

    • eg: list=[10, 1.5, "dwarika", "etc"]
  2. Sets: A set is an unordered collection of unique elements enclosed in curly braces.

    • eg: sets={10, 1.5, "dwarika", "etc"}
  3. 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())