Python Diaries…… Day 1

Nishitha Kalathil
6 min readSep 15, 2023

Welcome to the inaugural entry of Python Diaries! In this series, we embark on a journey to explore the fascinating world of Python programming. Whether you’re a complete beginner or an experienced developer looking to brush up on your skills, you’re in the right place.

In today’s entry, we’ll start by laying the foundation of what Python is, its history, and why it’s such a popular language across various domains. We’ll also set up our development environment and write our first lines of Python code. So, grab your favorite text editor or IDE, and let’s dive into the world of Python!

Introduction

Installation

Data types

Comment

Type casting

User input

Introduction to Python

Python is a versatile and powerful programming language known for its simplicity and readability. Guido van Rossum released the first version of Python in 1991, and since then, it has become one of the most widely used languages in the world. It’s employed in a multitude of fields, from web development to scientific computing, data analysis, artificial intelligence, and more.

Setting Up Your Environment

Before we start writing Python code, we need to set up our development environment. There are several ways to do this, but for beginners, I recommend using Anaconda, a distribution that includes Python and commonly used libraries. Follow these steps to get started:

  1. Download Anaconda:
  • Go to the Anaconda website.
  • Select your operating system (Windows, macOS, or Linux) and download the installer.

2. Install Anaconda:

Follow the installation instructions provided on the Anaconda website for your specific operating system.

3. Verify Installation:

Open a terminal or command prompt and type python. You should see the Python version you installed.

4. Open Jupyter Notebook:

In the terminal or command prompt, type jupyter notebook and press Enter. This will open a new tab in your web browser where you can create Python notebooks.

Basic Syntax

Python uses a clean and simple syntax, which makes it easy to read and write code. Here are some basic syntax rules:

  • Python statements end with a newline character. You don’t need to use semicolons (;) like in some other programming languages.
  • Indentation is crucial and is used to define blocks of code (e.g., loops, functions, conditional statements).
  • Comments start with the # symbol and are ignored by the Python interpreter.

Variables

Variables are used to store data that can be referenced and manipulated in a program. In Python, you can create a variable by assigning a value to it using the = operator. For example:

name = “Alice”
age = 30
is_student = True

Data types in python

In Python, data types refer to the classification or categorization of values that a variable can hold. It determines the operations that can be performed on the variable and the way it is stored in memory. Python has several built-in data types, and they can be broadly categorized into the following groups:

Your First Python Program

Let’s start by writing a simple program that prints “Hello, Python!” to the console.

print("Hello, Python!")

To run this program, open a text editor, paste the code, and save it with a .py extension (for example, hello.py). Then, in your terminal or command prompt, navigate to the directory where you saved the file and type python hello.py.

Congratulations! You’ve just written and executed your first Python program.

Understanding the Code

  • print(): This is a built-in Python function used to display output to the console.
  • "Hello, Python!": This is a string, which is a sequence of characters enclosed in either single (' ') or double (" ") quotes.

Comments in Python

In Python, comments are used to provide explanations or documentation within your code. Comments are not executed as part of the program; they are purely for human readability. Python supports two types of comments:

Single-Line Comments:

  • Single-line comments are created using the # character. Anything following the # on the same line is considered a comment and is ignored by the Python interpreter.
# This is a single-line comment
print("Hello, World!") # This is also a comment

Multi-Line Comments or Docstrings:

  • Python does not have a built-in syntax for multi-line comments like some other programming languages. However, you can use triple-quotes (''' or """) to create multi-line strings, and these are often used as docstrings for functions, classes, and modules. While not true comments, they serve a similar purpose for documentation.
"""
This is a multi-line comment
or a docstring.
It provides documentation for a function, class, or module.
"""

The type() function

This is a built-in function that allows you to determine the data type of a given object. It takes one argument (the object whose type you want to know) and returns the type of that object.

x = 5
y = "Hello"
z = [1, 2, 3]
print(type(x))  # Output: <class 'int'>
print(type(y)) # Output: <class 'str'>
print(type(z)) # Output: <class 'list'>

Type conversion

Type conversion, also known as type casting, is the process of changing the data type of a variable or value from one type to another. In Python, you can perform type conversion using various built-in functions. Here are some of the commonly used type conversion functions:

num_str = "42"
num_int = int(num_str) # num_int will be an integer 42

num_str = "3.14"
num_float = float(num_str) # num_float will be a float 3.14

num = 42
num_str = str(num) # num_str will be a string '42'

my_tuple = (1, 2, 3)
my_list = list(my_tuple) # my_list will be [1, 2, 3]

my_list = [1, 2, 3]
my_tuple = tuple(my_list) # my_tuple will be (1, 2, 3)

num = 0
bool_value = bool(num) # bool_value will be False

my_list = [1, 2, 2, 3, 3, 4]
my_set = set(my_list) # my_set will be {1, 2, 3, 4}

Python user-input

In Python, input() is a built-in function that allows a program to accept user input during runtime. This function prompts the user with a message or question, waits for the user to provide input, and then returns that input as a string.

name = input("Enter your name: ")
print(f"Hello, {name}!")

Keep in mind that the input() function always returns a string, even if the user enters a number. If you want to use the input as a number, you'll need to convert it using functions like int() or float().

x = float(input("Enter the first number : "))
y = int(input("Enter the second number : "))

print("The addition result is : ", x+y)

Today, we covered the basic syntax of Python, explored different data types, and learned about variables and commends. We also wrote a simple program to print a message to the console.

You can access the other topics in this tutorial series right here:

In the next entry, we’ll dive deeper into different different data types. Until then, keep coding!

--

--