Python for Data Science #2: Numbers, Strings and Functions

Analytics Vidhya · Beginner ·📐 ML Fundamentals ·9mo ago

Key Takeaways

The video covers essential Python building blocks for data science, including numbers, strings, and functions, using tools like Anaconda, Jupyter, and Python, and demonstrates mathematical operations, string manipulation, and file input/output.

Full Transcript

Welcome to week two of Python classes. So you all have learned amazing stuffs in the week one content where we talked about how important is Python from a data science and data analytics point of view and we talked about things like how people can start working with Python with Anaconda installation, Google collab or even ID like you know Visual Studio Pycharm. So in that series we talked about how to work with basic Python data structures, simple commands, simple functions, for loops, if else conditions, other elementary stuff. Today we're going to be taking the step one more forward direction and try to cover some more amazing concepts of Python. These fundamentals will help you in the long run of your uh data uh data world. Right? So with that I am Jant. I will be taking on this session with you. So let's dive in. So what I'm using? I'm using Anaconda today. So when you use Anaconda, you know allows Jupiter environment to run on your local system. As you can see, it's hosted on my local system. And from there, I am accessing my IPYNB file for week 2 that is interactive Python notebook. Right. And this is the file that I'm working on. Jupiter if I quickly revise provides you to work with Python IP Y and the interactive Python notebooks where you have cells where you can either write a code or a commit. So today the things that we're going to be covering is about how to play with numbers, basic mathematics, strings, uh collections, functions, iterations, error handlings and even if there is another Python code you have written in a different file. How can you call that function in this file and access its derivatives? You might have seen in data science, you know, people say import numpy, import pandas, plan chain and whatnot, right? So how a code written in a different file can be accessed here. We'll be looking at on that frontier also from a elementary site. So with that let's dive in. So the first thing we have is a very basic elementary stuff that is your mathematics right. So this is a cell where I have written some quotes. Anything after a hash is a comment. So this is a command. My compiler whenever I run this code I'll click on run. The compiler understand these are these are just comments. Then a is a variable that has a value of 10. B is a variable that has a value of three. So if I add a new cell by clicking here on plus and show you how a + b and if I run that cell gets 13. Why? 10 + 3 30. So similarly I have written some multiple mathematical operations here. What are those? Print division. A divided by b. The answer would be 3.333. Then a uh you can say that double division or you can say that the quotient. So if I divide 10 by 3 what would be the question? 3 3's are 9 right? So that 3 * 3 that is called the question. How do you get that? By using the double backwards uh forward slash right. What if you are looking for remainder? If I divide 10 by 3, what is the remainder? One. Then we use the modus. And what if you're looking at a power 10 to power three that gives me 1,000. Sometimes the number that you are getting I have a lot of decimals right and you don't want that. So how can we do that? We have a function called let's say this is there right? A type b. If I run that, it's a lot of decimals, right? So if I type round comm, 2. See what it has done is this function has calculated a by b and rounded it by to two decimal. This is something which I'm going here. Then sometimes what happens is you have a negative number. So the absolute sign changes that number to positive. But these are like basic math that we all know, right? What about some statistical maths? What about mean, median, standard deviation? So we have a package called import math from statistics as stats. What this function does it it allows us to calculate mean. If I run this code here, we show you the output. It will calculate the mean of this list. You remember in our last class we talked about what is a list. A list is a a bunch of elements placed inside a square bracket that allows us to work with them in a C. So if I'm looking at mean, I can run that function median and standard deviation. If you don't like the output, I can run a function of round on top of it. What will happen? The inner function will calculate the mean and the outer function will round it off to two decimal. Similarly, we can do it using the precise decimal function also. That's about numbers. Now, what about our operations dealing with uh characters? So we have lot of amazing functions in characters also. So let's say there is a string called deep learning. If you are interested in the length of the string, you see the function length. It tells me that there are 17 character. What does strip does? Strip splits the word into the sentence into multiple words. The function upper converts your lowerase wordings to uppercase. You see this thing has been converted into an uppercase. Sometimes you might have to replace a word instead of learning I need to replace it with network. So deep network. What if you want to slice the data? You don't need the entire string. You just need the word deep. So what I'm doing this let me run this code for you separately. S S is a string. So if I do slicing what will happen from second position 0 1 second this is the second position till the sixth position 6th - 1 till the fifth position the element will be printed. Now what will happen is sometimes when you are using print statement you can use something called fring. You see this f strings are a way of writing comments in programming language. It's there in Java also it's there in Python also. The advantage that you get using string is I can write characters and use the variable also using curly braces. As you see this thing is in uh red orange color. But this is black pie. This is a character like a normal string. Hello. And this is a variable user and dot one float means I need one decimal after the number. So you see this thing one decimal after the number. Similarly, we have a function called split that splits the elements using comma separate or on the contrary if you want to join element you can use the join commands. So no you know what happens I know when we are learning these things for the first time I'm coming from the same you know road map which you all are following we get to know confused like do we able to remember all of this it comes with practice right this is maybe you are looking at all of these things for the first time but eventually you will get accustomed to it next increasing the complexity a little bit more remember list are mutable means I can change their value so there is a list called num which has three elements 3 1 4. If I do append, what will happen? One will get added after four. Then if I do extend, five and nine will get added after the n. Right? So let's say I want to show you this thing again. Let me run this code separately. If I run this code right now, what will happen? Oh, I have printed that all see originally 314 you added one, it became 31 41. So what we are doing is let's say now let's do extend also. So 3 1 4 then 1 and then 5 9 got added and then there is a function called sort. What this function does it sorts the array 1 1 3 4 5. However in tpple which are your round brackets you can pack and unpack these things. Right? If I run this thing separately, if it is getting confused, let me run this. See the original value was 10 20. After unpacking, first element is 10. Another element is 20. Then what about dictionaries? You remember dictionaries? Key value bear key value. Key value. So we can fetch the element by using the get command. See what is it? Model is a dictionary. I want to get the value of LR. The value of LR is 0.01. Right? Then just like your dictionaries we have set. Set set also statistics set. Remember union uh intersection regarding those operations you can do right? So union means adding these two things together. Intersection means common elements. The common element is only three. Difference is things which are there in the first one and should not be there in the second. Right? And similarly sim difference is also provides you the uh removes the column element and gives me 1 to 4 pi. It just removes the common element. So we talked about numbers, we talked about strings and we talked about some amazing data structures and these are the key things which will help you make your portfolio better in future. Next we have comprehensions. Comprehensions are for loops, if conditions, functions running in a on top of your list, dictionaries or sets. Let's say you want to calculate the square of numbers, right? And if I print this thing, so see I'm declaring a range six. Range six means count the values from zero till n minus one. So 0 1 2 3 4 5. So what I'm doing saying is 0 into 0. Then I will become 1. 1 into 1 1. Then I will become 2 2 into 2 4 3 into 3 9 4 into 4 16 and 5 into 5. Similarly I can calculate the even numbers. I can map key value pair. I can calculate the length of each element. So now you're getting the pattern all the basics that we studied we are able to run them in terms of functions and if you want to make this functions more complex let's do that why not right what is this function doing it is the function def called scale it will take a value as a input which has a name value and it will be of float type then it can take any number of values val and then a value called factors which will be a float type of a format 1 comma default value with 1 comma 0 right and the output would be value into factor so if I call this function scale 3.5 comma factor 2.0 And what will it do? It will multiply 3.5 into. You can make your functions more complex. How? Summarize. It will take n number of inputs. Star num. It will take n number of inputs. Followed by with what value you want to round it off the output. And then a meta means key value pair. Double star means key value pair. So experiment is a key and its value is a. So I will pass a series 1 2 3 4 round it to one decimal and the experiment value is a what will what will it return? Length of the data 1 2 3 4 5 length is five. mean of the data rounded off to one decimal mean is three and meta value is that summarizes your basic to medium level of functions. Then we have lambda functions. What these do? They try to do the same thing which you did using functions but in inline fashion. Inline fashion means they save a lot of space and time complexity run faster. So if you have simple functions like multiply why do you need so much of code all you need to return is multiply right I mean here we need to do a lot of things I understand but in this code I just need to return multiplication so what this lambda does it you see this thing lambda x is the input what do you want to do with the x multiply x with two and where is x coming from nums nums is a list so what will be the output 2 into 2 2 into 2 4 2 2 into 3 6 one line of function. So lambda is running that function. Then it is mapping those values and converting it into list and saving into doubles. Similarly I can get the even values also. Now once you have learned to build such amazing functions you need to learn how loops also work right because in future you might be having a lot of data and you have to be very thoughtful of how are you calling that data. So range is something which we already studied when you want to print data from a starting value till the end n minus one. So range becomes 1 2 3 n minus one. What is enomerate? Enumerate takes this letters A B C and split them into two parts. What are the two parts? idx. What is idx? Is your index and ch is the actual value. Right? So what enumerate does it will split this into location and the value and zip will do the reverse. It will merge these two things. You see this zip zip is associating one with 10, two with 20 and three with. Now after learning about numbers, strings, operations, functions, iteration, there could be some errors also while you are writing the quotes. Why not? We are all humans in the end, right? So there is a concept of try catch where by chance let's say you have written a code and that code is a part of a thousands of lines of code and it throws an error. If you are not handling the errors properly you will end up with a code breaking. So these error handlings allows us to resolve those edge cases. So the there are some popular errors like division divide by 0 you all know that 1 divided by 0 3IID by 0 will give an error. So how do we define try catch you write try and you write the functions that you want to do whatever then if this function gives an error except what is the error zero division error and you caught that error and return none. So the code will not break and it will keep on running. else division is good return the result right let's call this function so when I call this function 10A 2 what is the output division is okay but when I'm calling the function next 10 0 what is the output c division by zero right similarly you can raise an exception also if you want users to enter non- negative value only and you see sometimes you get an error when you are filling a form you cannot enter this character why something very similar like this is written in the background I have a function diff which takes only non- negative values so if a user has given a value which is negative let's say indentially I'll check that and tell the user you have entered a non- negative number so please rectify that fun isn't it right now not only you are learning how to write amazing code you are learning how to rectify your code still and after you have done such an amazing job you need to save your code also somewhere don't you so there is a thing called path using this path you can save some data into txt dot csv excel file so let's say you have written some text name of the person their score and blah blah blah information and you want to save that into a txt file so I have initialized a txt file and I'm saying Open that file tmp open and open it in a write mode means I want to write something in that file using the encoding 8 and what do I want to write f.t write is name and all of these things. So when I run this line you see there is a file got created temp data txt and see the content in that data then I can read that file. What if I want to read and print the data here? I can open that file using read mode. Right? And let's say there are scores that you have saved. I want to get an average of that. I mean you guys are expert. Now, now imagine all of this code has been written in a different file. Then how will you do that? Will you write that code again? Let's say there is a code which a function has been written here in a different file demo. py. So do I need to write this function again in this notebook? No, I don't. How will I work with that? I will use something called system and operating system packages. They allow me to work with files which are there in a different folder maybe or in the same folder. In my case, we have kept that in a different folder in the source folder. You see this thing, it's in the source folder. So I'm telling my compiler that go to source folder from that source folder pick up this file demo this file name demo and from that demo file imports a function greet there is a function greet what that function does it takes a name and prints hello andam for example for me hello jant so now I'm calling the function So it found that there is a file folder source which has a python file demo there is a function inside it greet print greet equal to week 2 so hello week 2 but if this would have been error let's say this function is not there this file is not there this folder is not there then I will raise a function ensure project match match the week one scale food or in our case week 2 skill. So isn't it fun? I mean all the small small things that we did today constitute to a very big and large picture. Imagine if you are able to do all of these things on the fly. You are there you are a developer now. So we you know I hope you all had fun. Please you will be getting all the content and resources. Go through them run them in your Jupyter notebook. Google Collab ID. Have fun on that and let me know how it goes. Other than that, I'm Jen. Thank you very much and I'll see you all in the week three session. Thank you.

Original Description

NB Link - https://github.com/abhirajsuresh/Python-for-Data-Science Welcome back to our "Python for AI" series! In this second episode, we dive deeper into the essential Python building blocks that are crucial for any aspiring data scientist or AI practitioner. Building on the fundamentals from Week 1, this video will guide you through more intermediate concepts to strengthen your programming foundation. What you'll learn in this video: - Numbers & Math: Go beyond basic arithmetic and explore useful math and statistics libraries in Python. - String Manipulation: Master advanced string operations including slicing, common methods, and powerful f-strings for formatting. - Python Collections: A deep dive into lists, tuples, dictionaries, and sets, including their methods and when to use each. - Functions & Lambdas: Learn how to write your own functions, understand variable arguments (*args & **kwargs), and use anonymous (lambda) functions. - Iteration Helpers: Use powerful built-in functions like range, enumerate, and zip to write cleaner and more efficient loops. - Error & File Handling: Learn how to gracefully handle errors using try/except blocks and how to read from and write to files on your system. - Imports & Modules: Understand how to import and use code from other Python files and modules, a key skill for building larger projects. By the end of this tutorial, you will have a robust understanding of the Python concepts that form the backbone of data analysis, machine learning, and AI development. Don't forget to like, subscribe, and hit the notification bell to stay updated with our latest content! Chapters: 0:00 - Introduction & Recap of Week 1 0:59 - Setting Up the Environment: Anaconda & Jupyter Notebook 1:34 - Agenda Overview for Week 2 2:12 - Numbers & Math in Python 2:53 - Using Math & Statistics Libraries 5:39 - String Manipulation: Slicing, Methods & F-Strings 8:21 - Python Collections: Lists (Mutable) 9:39 - Python Collections: Tuples (Immutable) 10
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Analytics Vidhya · Analytics Vidhya · 0 of 60

← Previous Next →
1 The DataHour: Data Science in Retail
The DataHour: Data Science in Retail
Analytics Vidhya
2 The DataHour: Anomaly detection using NLP and Predictive Modeling
The DataHour: Anomaly detection using NLP and Predictive Modeling
Analytics Vidhya
3 The DataHour: Energy Data Science Project from Scratch
The DataHour: Energy Data Science Project from Scratch
Analytics Vidhya
4 The DataHour: Explainable AI Need and Implementation
The DataHour: Explainable AI Need and Implementation
Analytics Vidhya
5 The DataHour: Google Cloud AI/ML
The DataHour: Google Cloud AI/ML
Analytics Vidhya
6 Prediction to Production in Machine Learning #machinelearning #prediction
Prediction to Production in Machine Learning #machinelearning #prediction
Analytics Vidhya
7 Practical Applications of Data science in Ecommerce
Practical Applications of Data science in Ecommerce
Analytics Vidhya
8 How to tackle Overfitting?#machinelearning #overfitting
How to tackle Overfitting?#machinelearning #overfitting
Analytics Vidhya
9 Building Data Pipelines on GCP #googlecloud #datapipelines #data
Building Data Pipelines on GCP #googlecloud #datapipelines #data
Analytics Vidhya
10 Hands-on with A/B Testing #abtesting #datascience
Hands-on with A/B Testing #abtesting #datascience
Analytics Vidhya
11 Efficient Implementations of Transformers #transformers #cnn  #machinelearning
Efficient Implementations of Transformers #transformers #cnn #machinelearning
Analytics Vidhya
12 Modern Deep Learning Architecture #deeplearning  #architecture #deeplearningtutorial
Modern Deep Learning Architecture #deeplearning #architecture #deeplearningtutorial
Analytics Vidhya
13 Key steps for Designing Artificial Neural Network (ANN) for Image classification #machinelearning
Key steps for Designing Artificial Neural Network (ANN) for Image classification #machinelearning
Analytics Vidhya
14 5 things you should know about Azure SQL #azure #sql #datahour #datascience
5 things you should know about Azure SQL #azure #sql #datahour #datascience
Analytics Vidhya
15 AI & ML in the Automotive Industry #machinelearning #ai
AI & ML in the Automotive Industry #machinelearning #ai
Analytics Vidhya
16 Building Machine Learning Models in BigQuery
Building Machine Learning Models in BigQuery
Analytics Vidhya
17 NLP aspects in Telecommunication Industry
NLP aspects in Telecommunication Industry
Analytics Vidhya
18 Practical Time Series Analysis
Practical Time Series Analysis
Analytics Vidhya
19 Fundamentals of Quantum Computing
Fundamentals of Quantum Computing
Analytics Vidhya
20 A DAY IN THE LIFE of a Data Scientist (From waking up to working on algorithms)
A DAY IN THE LIFE of a Data Scientist (From waking up to working on algorithms)
Analytics Vidhya
21 Classification Machine Learning Model from Scratch
Classification Machine Learning Model from Scratch
Analytics Vidhya
22 Knowledge Graph Solutions using Neo4j
Knowledge Graph Solutions using Neo4j
Analytics Vidhya
23 Model Guesstimation (MLOps)
Model Guesstimation (MLOps)
Analytics Vidhya
24 ETL Pipelines in Google Cloud Platform
ETL Pipelines in Google Cloud Platform
Analytics Vidhya
25 Key steps for Designing Convolutional Neural Network(CNN) for Image Classification
Key steps for Designing Convolutional Neural Network(CNN) for Image Classification
Analytics Vidhya
26 Getting Started with AWS EC2 #amazon #aws
Getting Started with AWS EC2 #amazon #aws
Analytics Vidhya
27 How to Use Azure NLP and Graph Databases for Intelligent Knowledge Mining
How to Use Azure NLP and Graph Databases for Intelligent Knowledge Mining
Analytics Vidhya
28 Certified AI & ML BlackBelt Plus Program #shorts
Certified AI & ML BlackBelt Plus Program #shorts
Analytics Vidhya
29 Visualizing Data using Python #machinelearning #visualization #python
Visualizing Data using Python #machinelearning #visualization #python
Analytics Vidhya
30 DCNN for Machine RUL Prediction using Time-series Data #timeseries #machinelearning #datascience
DCNN for Machine RUL Prediction using Time-series Data #timeseries #machinelearning #datascience
Analytics Vidhya
31 M in ML stands for Math & Magic
M in ML stands for Math & Magic
Analytics Vidhya
32 An Unsupervised ML approach using Clustering
An Unsupervised ML approach using Clustering
Analytics Vidhya
33 Customizing Large Language Models GPT3 for Real-life Use Cases #gpt3 #datascience
Customizing Large Language Models GPT3 for Real-life Use Cases #gpt3 #datascience
Analytics Vidhya
34 Model Parameters vs Hyperparameters - Techniques in ML Engineering #machinelearning
Model Parameters vs Hyperparameters - Techniques in ML Engineering #machinelearning
Analytics Vidhya
35 Practical MLOps #mlops #datascience
Practical MLOps #mlops #datascience
Analytics Vidhya
36 Data Engineering with Databricks #dataengineering #databricks
Data Engineering with Databricks #dataengineering #databricks
Analytics Vidhya
37 Multi-Objective Optimisation
Multi-Objective Optimisation
Analytics Vidhya
38 When Airflow Meets Kubernetes
When Airflow Meets Kubernetes
Analytics Vidhya
39 AI in Banking
AI in Banking
Analytics Vidhya
40 Learn Convolutional Neural Network for Image Recognition
Learn Convolutional Neural Network for Image Recognition
Analytics Vidhya
41 Extracting Value from Data
Extracting Value from Data
Analytics Vidhya
42 How to measure Marketing Channel Effectiveness
How to measure Marketing Channel Effectiveness
Analytics Vidhya
43 Transforming Lives | Data Science Immersive Bootcamp
Transforming Lives | Data Science Immersive Bootcamp
Analytics Vidhya
44 Stock Market Analysis - AI driven approach
Stock Market Analysis - AI driven approach
Analytics Vidhya
45 Become a Data Engineering Professional in 2022 | Future Trends + Skills Required
Become a Data Engineering Professional in 2022 | Future Trends + Skills Required
Analytics Vidhya
46 Ensemble Techniques in Machine Learning #machinelearning #ensemble #datascience
Ensemble Techniques in Machine Learning #machinelearning #ensemble #datascience
Analytics Vidhya
47 The Power of Visualization | Tableau Full Course | Analytics Vidhya
The Power of Visualization | Tableau Full Course | Analytics Vidhya
Analytics Vidhya
48 Demand for Data Engineers is on the Rise | Data Engineer | Analytics Vidhya
Demand for Data Engineers is on the Rise | Data Engineer | Analytics Vidhya
Analytics Vidhya
49 Data Visualization in Data Science | DataHour | Analytics Vidhya
Data Visualization in Data Science | DataHour | Analytics Vidhya
Analytics Vidhya
50 Role of Optimization in Machine Learning & Deep Learning | DataHour | Analytics Vidhya
Role of Optimization in Machine Learning & Deep Learning | DataHour | Analytics Vidhya
Analytics Vidhya
51 Solving any Machine Learning Problem | Approach and Steps Involved
Solving any Machine Learning Problem | Approach and Steps Involved
Analytics Vidhya
52 Topic Modeling Explained with Implementation | Using LDA in Python | DataHour by Arpendu Ganguly
Topic Modeling Explained with Implementation | Using LDA in Python | DataHour by Arpendu Ganguly
Analytics Vidhya
53 Data Engineering in E-Commerce | The Best Case Study
Data Engineering in E-Commerce | The Best Case Study
Analytics Vidhya
54 Introduction to Classification using Azure Machine Learning | DataHour | Analytics Vidhya
Introduction to Classification using Azure Machine Learning | DataHour | Analytics Vidhya
Analytics Vidhya
55 Introduction to Federated Learning | DataHour | Analytics Vidhya
Introduction to Federated Learning | DataHour | Analytics Vidhya
Analytics Vidhya
56 Diffusion Models for Generative Arts | DataHour | Analytics Vidhya
Diffusion Models for Generative Arts | DataHour | Analytics Vidhya
Analytics Vidhya
57 Master Google Analytics in 1 Hour | DataHour | Analytics Vidhya
Master Google Analytics in 1 Hour | DataHour | Analytics Vidhya
Analytics Vidhya
58 Learn Hypothesis Testing | DataHour | Analytics Vidhya
Learn Hypothesis Testing | DataHour | Analytics Vidhya
Analytics Vidhya
59 A Practical Approach to Kaggle Competition | DataHour | Analytics Vidhya
A Practical Approach to Kaggle Competition | DataHour | Analytics Vidhya
Analytics Vidhya
60 Making AI work for Business | DataHour | Analytics Vidhya
Making AI work for Business | DataHour | Analytics Vidhya
Analytics Vidhya

This video teaches the basics of Python programming for data science, covering numbers, strings, and functions, and demonstrates how to perform mathematical operations, manipulate strings, and work with files.

Key Takeaways
  1. Run code in Jupyter cell
  2. Import math and statistics packages
  3. Use variables and functions for calculations
  4. Calculate mean, median, and standard deviation
  5. Round numbers to specific decimal places
  6. Use lambda function with map to create a list of squares of numbers
  7. Use enumerate to split a list into index and value pairs
  8. Use zip to merge two lists
  9. Use try-except block to handle division by zero error
  10. Use path to save data into a file
💡 Python is a versatile language that can be used for a wide range of data science tasks, from mathematical operations to file input/output.

Related AI Lessons

Stop Overfitting With Basically One Line of Code
Learn to prevent overfitting with a simple code tweak and understand the difference between Ridge and Lasso regression
Medium · AI
Stop Overfitting With Basically One Line of Code
Learn to prevent overfitting in machine learning models with a simple code tweak and understand the difference between Ridge and Lasso regression
Medium · Machine Learning
Stop Overfitting With Basically One Line of Code
Prevent overfitting in models with a simple code tweak, understanding the difference between Ridge and Lasso regression
Medium · Data Science
Stop Overfitting With Basically One Line of Code
Learn to prevent overfitting in machine learning models with a simple code tweak, comparing Ridge and Lasso regression techniques
Medium · Python

Chapters (8)

Introduction & Recap of Week 1
0:59 Setting Up the Environment: Anaconda & Jupyter Notebook
1:34 Agenda Overview for Week 2
2:12 Numbers & Math in Python
2:53 Using Math & Statistics Libraries
5:39 String Manipulation: Slicing, Methods & F-Strings
8:21 Python Collections: Lists (Mutable)
9:39 Python Collections: Tuples (Immutable)
Up next
Learn Deep Learning by Hand (Beginner's Guide - Part 1)
Thu Vu
Watch →