DEV Community

Benta Okoth
Benta Okoth

Posted on • Edited on

Python Basics

Introduction To Python

-Python is the most popular programming language used globally for web development, Data Science, Machine Learning and Automation.
-Python codes are normally run using the print() keyword and the string should be in quotation(single or double quote) marks for example
print("Hello World !")
print('Hello World !')

  • Comments in python are denoted by # (harsh symbol).

It is one of the easiest to learning programming language and beginner friendly.

** Use Cases of Python in Data

  1. Automated Data Cleaning with Pandas library Exploratory Data Analysis(EDA) interactive Data Visualization Dashboard matplotlib.pyplot and seaborn. Web Scraping for Data Collection. Automating Model Training with Scikit-learn Pipelines. For Feature Engineering with Feature-engine. Automated hyperparameter Tuning with Optuna. Model Evaluation Report. Automating Dataset Versioning with DVC. Schedulinng and Monitoring Scripts with APScheduler.
  • Python can be accessed in different Programming Tools such as Google Colab VS Code (Virtual Studio Code) Juypter Notebook(On brower) Anaconda Command Line/Prompt.

Python Variables

  • A variable is a name that is assigned to a value and it is used to store data/ hold the data.
  • Some common variable naming are :
  1. Must start with a letter or underscore.
  2. Cannot start with a number.
  3. Cannot contain spaces.
  4. Cannot use python key words eg if, for, class.
  • Common python variable naming convections are like:
    (a). PascalCase eg StudentName commonly used for classes
    (b). camelCase eg studentName rarely used
    (c). UPPER_CASE eg MAX_SIZE used for constants.

  • Constants are variables that do not change/ they have predefined value for example GRAVITY = 9.8.

  • We have two types of variables
    ie user defined

Data Types

  • Data Types - this is a way of classifying data items in python.
  • There are various data types in python namely: (i). Integer - Whole number data type, for example
    num = 20
    print(num)
    type(num) # checking the data type
Enter fullscreen mode Exit fullscreen mode

the word num is the variable used to store the value which is 20. print(num) returns the value of the variable.

(ii) Float - These are numeric data type that contain decimal places.
Example

   num = 20.5
   print(num)
   type(num)
Enter fullscreen mode Exit fullscreen mode

(iii) Boolean - These are True or False. The 1st letter is case sensitive.
For Example

   pen = True
   print(pen)
   type(pen)
Enter fullscreen mode Exit fullscreen mode

(iv) String - This is a word/text data type. Denoted by the quote during print
For example

    print("Hello world")
Enter fullscreen mode Exit fullscreen mode
  • Type Casting is the conversion of a variable from one data type to another. For example
     y = int(num)
Enter fullscreen mode Exit fullscreen mode

Data Structures

  • These are organized formats for efficiently storing, accessing and manipulating data in computer memory.
  • The types of data structures are :
  • Tuple - ordered collections of elements, similar to python list. X-tics
  • It is immutable(you cannot change the elements in a tuple)
  • Both stores sequences you can access by index position. eg mytuple[0] = 3
  • You create them using parentheses syntax.
  • When to Tuples
    • Function returns: Python functions often return multiple values as tuples: return (min_value, max_value, average) Example
    mytuple = (3, 4, 5, 6)
    mytuple
Enter fullscreen mode Exit fullscreen mode
  1. Lists - A built-in data structures used to store a collection of items in form of an array. x-tics
  2. Mutable: you can be able to change, update, add or remove elements. eg .pop(), .add(), .union() mylist.append([10, 11])
  3. Ordered : elements maintain the order in which they are inserted.
  4. Index- based. eg mylist[-1] or mylist[0]
  5. Example
    mylist = [3, 4, 5, 6]
    mylist
Enter fullscreen mode Exit fullscreen mode

  1. Sets - Used to store a collection of items. x-tics
    • It does not allow duplicates
    • It's a unordered collection, you can access your items without any specific order.(not index-based)
    • We can have multiple data types within a single set
    • We can join sets using unions(), intersection()
  2. Example python myset = {10, 20.5, True, "trial"} myset

4.Dictionary - Stores information in key-value pairs separated with a comma.

  • x-tics
    • Keys must unique and immutable
    • values can be of any data types example python mydict = { "name" : ("Nyambura", "Benta"), "age" : (20, 23), "course" : ("Data Engineer", "Data Scientist") } mydict
  • mydict.keys() - give us the keys.
  • mydict.values() - gives us the values.
  • type(mydict) - tells us the data structure.

Python Operators and Expression

  • They allow for mathematical manipulation

Conditional statements

  • They are used to make decisions in a program.
  • They allow your code to run different blocks depending on whether a condition is True or False. Exmples
  • if, elif, else x = 20 y = 30 if x > y: print(x) elif y > x : print(y) else : print("They are equal")

LOOPs

  • Loops are used to execute a block of code repeatedly until a condition is met or all items in a sequence are processed.
  • The main types are:

(i). For loops (iterating over sequences)

  • They are used to iterate over a sequence such as a list, tuple, dictionary, set string or range
  • Example:
fruits = ['banana', 'orange', 'apple']
for fruit in fruits:
  print(fruit)
Enter fullscreen mode Exit fullscreen mode

(ii). While loops (executing code based on a condition).

  • Example
i = 1
while i < 6:
   print(i)
   i += 1
Enter fullscreen mode Exit fullscreen mode

FUNCTIONS

  • A function is a named, reusable block of code that performs and specific task and it only runs when it is called.
    We have two types of functions namely:

  • Built in e.g print(), input(), list(), str() functions

  • User defined functions.

Top comments (0)