Full Stack Data Science Journey: Python Basics – 3

Hello Learners…

Welcome to the blog…

Table Of Contents

  • Introduction
  • Full Stack Data Science Journey: Python Basics – 3
  • Python Programming
    • 1.2 Python Basic
  • Summary
  • References

Introduction

In this post, we are going to start with the basics of python programming. this is the 3rd post of our series Full Stack Data Science Journey: Python Basics – 3.

We hope that you are learning and implementing with us.

Full Stack Data Science Journey: Python Basics – 3

Let’s Start the journey of learning and implementation of python programming.

1. Python Programming

1.1 Python Installation And Setup

1.2 Python Basics

Here we are covering the basics of python programming. To implement the code we are using VSCode and Jupyter Notebook.

If you are new to Linux and don’t have Jupyter installed already, first, you should install Jupyter, and then open VSCode to create a Jupyter notebook file.

So let’s start,

Data Types In Python

Python supports several built-in data types that store and manipulate different types of data and values.

Some of the most commonly used data types in Python are:

  1. Numeric Types:
    • int:
      • The Integer data type represents whole numbers.
    • float:
      • Floating-point data type, used to represent decimal numbers.
    • complex:
      • Complex data type, used to represent complex numbers (e.g., 3 + 4j).
  2. Text Type:
    • str:
      • The string data type represents text and characters.
  3. Sequence Types:
    • list:
      • List data type, used to represent ordered collections of items.
      • Lists are mutable (can be modified).
    • tuple:
      • The Tuple data type represents ordered collections of items and is immutable, meaning it cannot be modified.
    • range:
      • The range data type represents sequences of numbers.
  4. Mapping Type:
    • dict:
      • The dictionary data type represents key-value pairs, and it denotes unordered collections of items.
  5. Set Types:
    • set:
      • The set data type represents unordered collections of unique items.
    • frozenset:
      • Frozenset data type, similar to sets, but immutable.
  6. Boolean Type:
    • bool: Boolean data type is used to represent True or False values, often used for conditional expressions.
  7. Binary Types:
    • bytes:
      • The bytes data type represents a sequence of bytes.
    • bytearray:
      • Bytearray data type, similar to bytes, but mutable.
    • memoryview:
      • The memory view data type is used for accessing the memory buffer of an object.
  8. None Type:
    • NoneType:
      • Represents the absence of a value or a null value. Often used to indicate the absence of a return value in functions.
  9. Custom Types:
    • You can also define your own custom data types using classes

These data types provide the foundation for working with different kinds of data in Python. Depending on the type of data and the operations we want to perform, we will choose the appropriate data type.

Python’s dynamic typing allows variables to change their data type as needed during runtime.

Variable Declaration In Python

In python, we can declare any variables without defining their data type. we can directly assign any value to the variable. Python’s dynamic typing allows variables to change their data type as needed during runtime.

Variables are those that store the different types of values.

Let’s See some examples,

a="galaxyofai"

In the above example ‘a’ is a variable and “galaxyofai” is the value of that variable.

When we try to access that variable we get the value of that variable.

print(a)

#OUTPUT
galaxyofai

We can check the data type of a variable using the “type” function.

type(a)

#OUTPUT
str

Also, we can define multiple variables in a single line

# variable declaration

a,b="45",56

Try to access the value and data type of “a”.

print(a)
type(a)

#OUTPUT
45
str

Try to access the value and data type of “b”.

print(b)
type(b)

#OUTPUT
56
int

Let’s declare boolean data types.

d= True
e= False

type(d)

#OUTPUT
bool

In Python, boolean values (True and False) can be treated as integers, where True is equivalent to 1, and False is equivalent to 0 when used in arithmetic operations.

True + True + True - False

#OUTPUT
3

False-True+True

#OUTPUT
0

In Python, we cannot directly add a number (5) to a string (“galaxyofai“) using the + operator because they are of different types (int and str).

5 + "galaxyofai"

result = str(5) + "galaxyofai"
print(result)

This will produce the output:

5galaxyofai

By converting the number 5 to a string using str(5), we can concatenate it with the string “galaxyofai” to create a new string.

In Python, when we use the * operator with a number and a string, it repeats the string that number of times

5 * "galaxyofai"

#OUTPUT
'galaxyofaigalaxyofaigalaxyofaigalaxyofaigalaxyofai'

Take User Input In Python


To take user input in Python, we can use the input() function. Here’s an example of how to do it:

input() function returns a string by default

user_input = input("Please enter something: ")
print("You entered:", user_input)

#OUTPUT
Take user input in python

But If we want to take user input of a particular data type then we have to convert this into our required data type.

a = int(input("Enter an integer: "))

we entered 5.

print(a)

#OUTPUT
5
type(a)

#OUTPUT
int
Take user input in python

But if we pass a string in this then we get an error.

b = int(input("Enter an integer: "))

Take user input in python

Please try with different examples which running in your mind and try to understand how python programming works with values and variables.

Summary

This flexibility, combined with a rich set of data types, makes Python a powerful and popular programming language for a wide range of applications, from web development and data analysis to scientific computing and artificial intelligence.

Understanding Python’s variable declaration and data types is fundamental for effectively harnessing the language’s capabilities in any project or application.

Happy learning And Keep Learning…

Thank you…

References

Leave a Comment