Introduction
This comprehensive Python course begins with core programming concepts such as variables, data types, control flow (if-else, loops), and progresses through object-oriented programming, file input/output, and ends with advanced topics including modern Python features and AI-powered projects.
Core Programming Fundamentals
- Variables & Data Types: Understanding integers, floats, strings, booleans, and None.
- Control Flow: Use of if, elif, else clauses; logical and relational operators.
- Loops: While and for loops with examples like printing multiplication tables.
- Functions: Defining, calling, passing arguments, returning values, default parameters, and recursion (factorial and sums).
Data Structures & Their Manipulation
- Lists & Tuples: Creation, indexing, slicing, mutability differences, methods like append, insert, count.
- Dictionaries: Key-value pairs, methods like update, get, keys, and values, mutable nature.
- Sets: Unordered collections with unique elements, set operations such as union, intersection, subset.
- String Methods and Manipulations: Slicing, concatenation, methods like .startswith(), .find(), .replace(), escape sequences.
File Handling
- Reading, writing, and appending text files using the open function and the context manager (
withstatement). - Working with file methods like read(), readline(), readlines(), and managing file pointers.
- Handling exceptions and errors during file operations.
Object-Oriented Programming
- Classes and Objects: Structure, attributes (class vs instance), methods, and constructors (
__init__). - Encapsulation & Abstraction: Accessing instance variables via methods and properties.
- Inheritance: Single, multiple, multilevel inheritance, use of
super()method. - Static and Class Methods: Using decorators to define methods not bound to object or class instance.
- Advanced OOP Features: Property decorators for getters and setters, operator overloading,
__str__and__len__methods.
Modern Python Features
- Walrus Operator: Assign expressions within larger expressions to shorten code.
- Type Hints: Adding type annotations for variables, functions for clarity and better code checking.
- Match-Case Statements: Python's switch-case-like syntax introduced in 3.10 for cleaner multi-conditional logic.
- Exception Handling Enhancements: Use of
try-except-else-finallyblocks and raising custom exceptions. - Virtual Environments: Creating isolated Python environments and managing package dependencies with
pip freeze/requirements.txt.
Functional Programming Tools
- Map, Filter, and Reduce: Applying functions to lists for transforming or filtering data sequences.
- List Comprehensions: Elegant syntax for creating modified lists from iterables.
Practical Projects and Demos
- Jarvis Virtual Assistant: Voice-controlled assistant with speech recognition, text-to-speech, web interaction, and news fetching via API.
- Guess the Number: Interactive game implementing Python basics and control flow.
- WhatsApp Auto-Responder Bot: Automating message responses by scraping chat with PyAutoGUI and integrating AI replies via OpenAI API.
Best Practices for Learning and Development
- Emphasis on hands-on coding with practice problem sets after each chapter.
- Using AI tools like ChatGPT effectively for coding assistance.
- Encouragement to document learning with notebooks, source codes, and cheat sheets.
- Job search advice including LinkedIn optimization and strategic application sending.
Conclusion
This course bridges beginner to advanced levels through a blend of theoretical concepts, practical exercises, and real-world projects focusing on modern Python capabilities and market-relevant skills. It fosters a strong foundation and inspires learners to explore specialized fields such as AI, web development, and data science using Python. For a deeper dive into AI concepts integrated in this course, see the Comprehensive Artificial Intelligence Course: AI, ML, Deep Learning & NLP. To strengthen foundational programming skills relevant here, consider the Comprehensive Overview of Algorithms and Data Structures Course. For learners interested in expanding beyond Python to full-stack development, the Comprehensive CS50 Course Review: From Basics to Web Development offers valuable perspectives. Additionally, mastering data manipulation techniques detailed in the Comprehensive Guide to Python Pandas: Data Inspection, Cleaning, and Transformation can significantly complement your Python data handling skills.
This is Sallu Bhai's son, Ballu. Ballu didn't get a break in Bollywood. That's why he learned Python from the internet in 2 days.
He thought that by learning Python in 2 days, he'll get a 90 LPA job. He didn't get a 90 LPA job.
All he got was frustration. Why do I get an error every time? I don't want to do the recording.
Harry, you? What happened? Why is there so much noise? I've been trying to learn Python since then.
Nothing is happening. One error, then another error. How will I learn Python?
I'm following the proper road map. It'll be done. It's not that difficult. These things look good in theory.
Road map, project. I'm going to Bigg Boss' house. I don't want to do the recording.
I think I'll have to bring a new Python course. In which, latest features, how to use AI,
and how to get jobs. I'll learn Python from this aspect. Complete Python course with handwritten notes.
And how to use AI to learn Python. All this is ready with knowledge. Not only this,
I made this course keeping the job aspect in mind. If you learn Python through this course, you'll definitely get a job.
And I'll also tell you how to find a job and where. I've divided this course into many chapters. And for your practice,
I've made many practice sets for you. Not only this, we're going to make amazing projects.
Not light projects. Amazing, amazing AI projects. There's only one prerequisite of this course.
And that is your time. If you bring your time, I'll teach you how to do machine learning with complete Python.
Artificial intelligence programs. Data science. Web development.
General scripting. And this list goes on and on. I'll teach you how to do all this.
So, let's not waste time. Let's go to the computer screen. And let's get started.
According to Stack Overflow, Python is the most loved and easiest language. To do this course,
you don't need to know any language. Even if your Python is the first language, you can do this course easily and well.
In fact, the purpose of this course is to teach you your first programming language. So, I'd like to open my Python handbook here
and talk about what is programming. I'll give you this ultimate Python handbook along with cheat sheet,
along with notes, which I've written by hand. There are many more things for you,
like source code. There are many more things which I'll tell you in the coming time.
But let's focus on what is programming. Just like we use Hindi and English to communicate with each other,
we use programming language like Python to communicate with the computer. If we want to say something to the computer,
like, sum two numbers, we can't say, like Amitabh Bachchan,
like, Mr. Computer, sum two numbers. No, we can't say that. We have to write a proper program.
And we'll write that program through a programming language. Just like you talk to a Chinese person
in Chinese, you talk to a French person in French, whatever language you understand,
you talk to them in that. Similarly, you talk to the computer in programming languages like Python.
C, C++, Java, Rust, Ruby, Golang, JavaScript, these are all programming languages. But in this video,
we're going to learn Python. Why did I choose Python as your first language
if you're a beginner? Because Python is simple, it's easy to understand,
and it feels like you're reading simple English. Yes, it feels like you're reading plain and simple English.
And I'm not saying this. These are the people who have learned Python.
And this is the general opinion in the industry that Python is so easy that it feels like you're reading
plain and simple English. Now, after watching this whole course, you'll think this course is too big.
In fact, this course is too small. This course is not big because I have learned all the Python
along with projects, along with my experience, which I have gained
after struggling so much. I have learned so many things that I am going to teach you in a video.
So yes, this video is small, boss. This is the pseudocode nature of Python, that is, the easy-to-understand nature
that it feels like you're reading English. This nature makes it understandable among beginners.
For example, look at this program. This is a program to sum up five numbers, which I have written in just ten lines of code.
Then look at this program. This is a program to equally distribute the expense of a trip among three friends.
Suppose three friends are going on a trip to equally bear everyone's expenses, then this program will equally split
the total expense among the three. Then this third program is amazing, which is Jarvis.
Whatever you say, he will do. Jarvis opens YouTube. You will give voice commands
and he will obey the voice commands. I will tell you once again that this course is absolutely free
and I am not going to sell you anything in this course. So you guys watch it completely and you will learn Python.
You don't have to buy anything. I am not going to sell you anything. Let's talk about the features of Python.
It is easy to understand, that is, the development time is very short. If you want to write a script
which is an assistant and you talk to people, basically your computer,
it is very easy to write such scripts in Python. Artificial intelligence, machine learning, it is very easy to write all these things in Python
because it has dedicated libraries. It is free, it is open source. By the way, we will talk about libraries
in the coming time, but it is free. You don't have to pay for it.
It is open source. It is high-level language. High-level language means
that you talk to it from above and it translates your code in low-level instruction and executes it in the machine.
The hardware is our computer, like our CPU, RAM, GPU. All this comes in our hardware
and the software is the instructions which are executed on that hardware. Our Python script is also a kind of software
which is going to be run on our hardware and will instruct our hardware what to do with it.
For example, if you want to sum two numbers, then through software, that is, through a Python program,
you will tell that I want to sum two numbers and then you can easily sum two numbers. Basically, we give code to the Python interpreter
which is written in the Python script and it converts that Python script into the machine language.
The Python interpreter takes the Python script from us and gives the machine code to the computer which the computer easily understands.
You cannot talk directly to the hardware. You will have to talk through software. Python programming language
is a way to make this kind of software. I hope that makes sense. Even if it doesn't,
if you are a beginner, it will make sense in the future. It's fun to work with
plus it's portable. That is, if you have written a code in Linux, Windows,
even if it is written on Mac, then your code is going to run. However, I will tell you more about this in the future.
But Python is portable and it's fun to work with. Let's not waste time
and install Python. To install Python, I will simply come to Google
and write Python install. And as soon as I write Python install, I will get a page with the download Python title.
Which will be the first page. And you will get a button which says download Python 3.12.3.
I will click on it. And the Python setup has been downloaded. This setup of 25 MB has been downloaded very quickly.
And after this, I will open it. And as soon as I open it, I will see a window like this.
Here it is very important for you. And I want to emphasize this that you guys check this
at python.exe to path. It's really very important that you guys follow this step.
After this, click on install now. And it will start installing. It will take some time
depending upon how fast your system is. My computer is very fast. That's why it's getting very fast.
But you got the point. It will also happen in your computer. Even if it is slow, it will happen.
It will take time. It will take time. It will be correct.
It will happen. And setup was successful. I am very excited.
I will close this. And before we start coding, we will install one more piece of software.
And this time we will write Download VS Code. As soon as we write Download VS Code,
the first link we will get will be of Download VS Code. We will open this.
As soon as we open this, here you will be given options for Windows, Linux, and Mac.
I am in Windows. That's why I will download Windows. If you are in Mac, Linux,
or in any other machine, download for that. If you are in Apple Silicon,
download for Silicon. If you are in Apple's Intel laptop, download for Intel.
But for now, I will download for Windows. It has started downloading.
The internet connection is very fast. I pay a lot of money for my internet. I have collected that.
I have downloaded it as soon as possible. We will open this. And we will read the License Agreement.
I will read this License Agreement fully. This License applies to Visual Studio and other damages.
And yes, I agree to this agreement. I will click on Next. Make sure that you have checked
all these check boxes. We will click on Next. We will click on Install.
And it's going to install now. And you can see here that it has been installed.
Now I will click on Finish. And my Visual Studio code will be launched. I will change a quick setting for you.
You have to search for mouse wheel here. And you have to check this setting. This is very important.
Then you have to click on Extensions and download the Python extension. You have to search for Python here.
Click on Install. And as soon as you click on Install, it will be installed.
Basically, it becomes easy to code in Python programming language if we use it.
So yeah, it was that easy. Just install it like a game. I am very excited
to write my first Python program. And we are going to do the same in the next chapter.
So yeah, see you in the next chapter. Before moving ahead, I want to tell you
a very interesting story. Python was created by Guido van Rossum in 1991.
He had created a new programming language for which he was looking for a name. And he couldn't think of a name.
He was watching a British comedy series named Monty's Flying Circus. He wanted a short, unique,
and a little mysterious name. And he named it Monty Python's Flying Circus.
Guido van Rossum is a genius who first worked in Google and then he joined Dropbox.
And now he works in Microsoft. If you are looking for a job, Python programming language
is perfect for you. I am not saying this Python course is the latest.
I will include all the things that have come in Python in the recent years.
And some features have been added in the latest version. So we are going to cover all those things.
Now I am very excited because we are going to write our first Python program.
And our first chapter is Module, Comments, and Pip. What are these three things?
First of all, I will show you by writing the first program. We will make a folder from here
so that things are organized. See, I also have to give you the source code.
So from that perspective, I will make a new folder for you whose name I am going to keep
Chapter 1. I will go inside Chapter 1. I would like to tell you
that if you have installed exactly like I have installed VS Code,
then you can right click and select the option Open with Code
so that VS Code will open in this folder. That is, whatever files will be in this folder,
like if I made a file here, I will make a file here. Let's say I make a main.py
or I make a first.py. I think first.py is a better name. I made first.py.
.py is an extension. You can click here and make a new file in VS Code.
What is VS Code? VS Code is an editor for us to write code.
Can we write in Notepad? Yes, we can write. But it won't be that efficient.
Okay, you should understand this thing. The second thing is I wrote first.py here.
.py means it is a Python script. Now you see, as soon as I wrote first.py here,
first.py came here too. So opening this folder in VS Code means the file I am making here
I will see that file here too. That means this folder is open. Now whatever I type here,
it will be saved in this file. If you have enabled autosave in the file, then you don't need to press ctrl s.
Whatever you write, it will be saved. So make sure
that you have turned on autosave. Just like me. Now what I will do here,
I will write print. And I will put a double quote here. And you see,
as soon as you put a double quote, automatically this double quote will also close.
And if you write hello world, then here
hello world will be printed when you run this program. Now what do you have to do
to run this program? You click on the terminal here. What did I do here?
As you can see, I am bringing my mouse arrow here. So it is showing me like this.
I can take it up like this. And by clicking on the terminal here, I will write python.
And after that I will write first.py. That means I am telling python
to run the first.py script. I will hit enter and you see
hello world is written here. What does it mean? It means
that this script is running and I got to see its output. What does output mean?
When we run a program, it has an output. In this case,
that output is hello world. As you can see. So congratulations,
you have written your first python program. You guys in the comment section below,
do write congratulations by liking this video. Like the video
and in the comment section below, congratulations to all of us learners.
Now what I will do here, I will tell you what is a module.
What is a module? And what is a pip? We wrote our first python program.
We understood what .py is. .py is an extension. When we make your python program,
then we write the name of that file as .py. Like you must have seen
movies.mp4. Game setup is .exe. In the same way,
we use python file .py extension. Now let's talk about what is a module
and what is a pip? Module is a file containing code
written by somebody else. What does it mean? It means that
usually this code is written by somebody else. And we can use it
in our program in the same way. Now what does it mean?
Suppose I tell you guys that you have to find the harmonic mean
of five numbers. You have to find the arithmetic mean.
If you have to write its program, then you will write it
from scratch. You will code the whole logic one by one.
But if you automatically get a module which you can install
with the help of pip, then you can use somebody else's code
in your program. How does it work? I will show you guys.
With the help of pip, you can install any module.
Now there are many modules. For example, there is a module called flask
which helps you in making web apps. There is a framework called Django
which also comes as a package, as a module
which you install by typing pip install django. There are many more
modules in python which we can install by typing pip install
and then using its name we can install it. So I will show you guys
a few modules which I will install. You have to come to the terminal
and simply type pip install flask. And as soon as I type
pip install flask, my flask will be installed. You can see
successfully installed flask. And after this I can import it
by typing import flask. And to give you guys an example of a module,
I will show you a very interesting package. Let me show you, whose name is pyjokes. You can install it by typing pip install pyjokes.
So I will come here in my terminal and I will type pip install pyjokes. I have created a new file by clicking here with the name module.py. Now you can see here that successfully installed pyjokes.
That is, pyjokes has been successfully installed. Now what I will do here, I will import pyjokes. And you see, as soon as I write pyj, it automatically suggested me pyjokes.
And this is the reason why we use VS Code. Because we get all the features that we may not get by typing in notepad or any other editor. Now I will write here joke is equal to pyjokes.
And I will write here get joke. And then I will write print joke. I will tell you all this step by step.
But now I just want to show you what is the package. So look, it has given me a joke. What's the object-oriented way to become wealthy?
It's inheritance. Let's see if it gives a new joke by running again. Yes, it's giving a new joke.
How many programmers does it take to kill a cockroach? Two, one holds, the other installs windows on it. So this is a windows joke.
But I am a windows fan by the way. So this is the module. Look, I didn't write this code to write jokes.
I imported pyjokes. And automatically I can print a random joke on my computer. So I didn't write the code.
I used someone else's code. But I used someone else's code and printed the joke. That means I achieved the ultimate task.
Which ultimately matters. The documentation of pyjokes is here. You can do everything here.
But that was just an example. We will see many more things later. So pyjoke was a module.
We installed it with the help of pip. Pip is the package manager for python. You can use pip to install a module on your system.
Like pip install flask, pip install pyjoke. So we have two types of modules. One built-in module and one external module.
Now we will see how python can be used as a calculator. I will tell you that if you open the terminal of your computer. Which you can open by right-clicking on the terminal in windows.
And if you are in mac or linux, you can open the terminal. From spotlight search or by going directly to search. Now what I will do here, because I am on windows.
I opened the terminal from here. Once your terminal is open, you write python. And as soon as you write python.
It will tell you the version and date of python. When this version was built. And after that you can write here.
Let's say I write 5 plus 6. And it is showing me 11. Wow, this is amazing.
Let's try multiplication. 4 multiplied by 2. Wow, 8.
You are doing multiplication very well. You are doing everything right. Let's try something else.
Let's give it a little trick. Not like this. Let's give it a trick.
Will it be able to do it? Maybe it will be able to do it, maybe not. And it is able to do it.
So even if we give it a tricky calculation. Still it is able to do it. Which means that we can use python as a calculator.
So to use python as a calculator. We simply write python. Enter it in the terminal.
And it opens RAPL. RAPL stands for Read Evaluate Print Loop. Now what is this Read Evaluate Print Loop?
Read Evaluate Print Loop means. It will read and evaluate. For example, I entered 6 plus 5.
So it read and evaluated it. It said it is 11. And printed it.
And went back to the loop. It will read, evaluate and print. Then it will go to the loop.
For example, if I write here 3. Multiplied by 6. So it read it.
Evaluated it. Printed it. And then repeat.
Now it is saying again. So Read Evaluate Print Loop. It is called RAPL.
So if someone asks you what is RAPL? So this is RAPL in python. So if you want to write python.
You can fire python like this. In the terminal and write. Will my print hello world work?
It should work. Yes it is working. So whenever we want to execute anything through python.
We can write it in RAPL. We can write print. After that we wrote hello world.
Entered it. If I make a syntax error here. If I write anything.
Which is not valid in python. So will it work? No, it is giving me an error.
So it will give an error. If it is valid, then it will work. If it is not valid, then it won't work.
So you have to keep this in mind. So we saw what is RAPL. Read Evaluate Print Loop.
Now let's talk about comments. If you come here. I wrote import pyjokes.
This was our program. With which we printed the joke. If I run it and show you.
How many programmers does it take to change a light bulb? None. That's a hardware problem.
So this is a joke. A programmer can't fix a light bulb. Because it is a hardware problem.
So go and call an electrician. So if I write print. Like I wrote hello world.
Printing jokes. If I run it again. So you will see.
Printing jokes. If you play a windows cd backwards. You will hear.
Satanic chanting. If you play it forwards. It installs windows.
So this is a windows joke. If you understood. Now I want this line.
To remain in my program. But not execute. I want a way to ignore it.
So what I can do. By clicking on this line. Control forward slash.
So this comment will be out. By pressing ctrl forward slash. VS code put a pound symbol.
Pound symbol is. Symbol for. Comments.
To write a single line comment. You have to start with pound. Whichever line starts with pound.
Python will automatically ignore it. You should know this. Now why do we use comment.
Sometimes we have to put comments. In our program. Like I want to write.
This prints a random joke. Now if I write it like this. I want to tell you.
That the line below. Is printing a random joke. I want to indicate this.
For fellow programmers. I want to write this line. Now if I run it like this.
Invalid syntax. But I want to keep this line. So the way to keep this line is.
Single line comment. By pressing ctrl forward slash. It will be uncomment.
By pressing ctrl forward slash again. It will be comment. VS code is showing it in green color.
Because VS code wants to tell you. Python interpreter will ignore this line. So if you run it.
This line will be considered. As part of this program. It will be ignored.
So simply a joke will be printed here. And the rest of the comment lines. Will be ignored.
So if you want. It to be uncomment again. By pressing ctrl forward slash.
You can toggle it. So this was single line comment. Sometimes we have.
Very long comment. And we need multiline comment. So this is the way to write.
Multiline comment. Multiple lines will be ignored. Single line comment will be ignored.
Multiline comment is the way. To write triple double quotes. And it will be ignored.
For example. If I come back to VS code. If I write triple double quotes.
And I write something like this. So thanks. That was my program.
If I run it. So thanks that was my program. It is not printed.
It was ignored. Because it was a multiline comment. So it was ignored.
So this is multiline comment. This is single line comment. Now I will tell you something amazing.
Since we have. Good IDEs and text editors. Since then we use.
Only single line comment. Most of the programmers. Use single line comment.
Suppose you have multiple lines. There are many lines. One more line.
And suppose I write. Yet another line. And we have many more lines.
What happens is. If I want to comment all of them. I will select.
Control forward slash. And it will comment individually. It is difficult to do it manually.
That's why multiline comments were made. But with the help of IDE. With the help of text editors.
We can select lines. Control forward slash. Comment or uncomment.
If I want to comment only these three lines. Control forward slash. Comment uncomment.
Good. Now we will comment it out. Otherwise there will be an error.
If I don't comment it out. There will be an error. What is this?
That's why I will comment it out. If I comment it out. There will be no error.
Everything is working fine. This is Microsoft attack. Why did Microsoft name their search engine Bing.
Because it's not Google. Now what we will do. We have seen the comment.
We will come back to this handbook. Handbook is very important. I will tell you all the links.
How you can download the handbook. How you will get cheat sheets. Just stay in this course.
And it is my responsibility to teach you Python. We have seen two types of comments. We write single line comments like this.
We put a pound symbol and it is ignored. After this we have seen multi-line comments. Triple double quote or triple single quote.
You can also use triple single quote. You can also use triple double quote. To write multi-line comments.
So this was about our comments. Now we will go to chapter 1 practice set. Let me tell you.
In practice sets. I will make you talk after every chapter. In these practice sets there will be problems.
Which I will make you solve. I will give you the solution. So that you can practice.
And you can understand the concepts. So let's go to chapter 1 practice set. So let's go to chapter 1 practice set.
So brothers and sisters. I have become a big fan of organizing things. So I will make a new folder in the name of chapter 1 PS.
I will close our existing folder. I will close it. I will choose the option of open with code.
If you are not getting the option of open with code. It means that at the time of installation. The checkboxes that I told you to stick.
You didn't do that. And if you want to fix it. Then install VS code again.
By sticking those checkboxes. Without installing, you run the setup again. Stick those checkboxes.
Your open with code option will come. Now I will click on plus here. After clicking on plus, I will make a new file.
Problem1.py I will hit enter. And this is our problem 1.
Now let's see what was our problem 1. Now see here. If your select interpreter is coming here.
Then you select the interpreter. I will select the 3.12 which I have just installed. So this has become my default interpreter.
If you can see below. It's a very good thing. What is the first question of chapter 1?
Write Twinkle Twinkle Little Star Poem in Python. Write a program. Let's google it first.
Twinkle Twinkle Little Star Poem has come here. After that, we will scroll down here.
Where is the poem? I have made the whole page. And you guys copy paste it like this.
The question is very simple. Write a program to print Twinkle Twinkle Little Star Poem in Python.
How will you do it? It's very simple. You will use print.
If you do it in double quotes. Then it will not work. So what you have to do?
You have to put triple single quotes. Put triple single quotes like this. And put triple single quotes again.
What did I do? I didn't tell you this. So I told you through this practice set.
Either you print all the lines one by one. Print Twinkle Twinkle Little Star Print How I Wonder What You Are
Print Up Above The World So High Print Like A Diamond In The Sky Instead of doing all this,
You can print multi lines with triple single quotes or triple double quotes. We talked about comments.
We said that comments can be done with triple single quotes and triple double quotes.
But now I want to tell you that you can use triple single quotes and triple double quotes
to print multi lines. Or if I keep it more professionally. Then I will say that
you can also make multi line strings. So that was the take away. Let's run it and yes
our poem will be printed. Wow! Amazing! Poem is printed. Problem is solved. Question number 2.
Use Ripple and print the table of 5 using it. You have to solve all the problems. Try to solve it yourself.
And if you say that I don't have time. Now JCB will be installed outside. And when JCB will start digging,
everyone will stand up to see. It will look like there is no time. If I ask you to ask questions,
you have to try all the questions yourself. Use Ripple and print the table of 5 using it. So how will you do it using Ripple?
First of all, I will open the terminal here. What is Ripple? What did you say? What is Ripple?
I told you in chapter 1. Write Python like this. And as soon as you write Python,
it will come to you like this. Now what do you have to do here? You have to write 5 into 1.
It's 5. 5 into 2. 5 into 3. 5 into 4.
5 into 5. 5 into 6. Why are you doing it manually?
Yes, I know that it can be done more efficiently. And I will tell you in the coming chapters. But now we have printed the table of 5.
Using Ripple. Using Python. So this way you can print the table of 5.
Question number 2 is solved. Question number 3. Install an external module and use it to perform
an operation of your interest. Which external module do you want to install? We saw that there are two types of modules.
Built-in and external. We have to install the external module. And then after installing the external module,
we have to use it. So to solve question number 3, I am going to use a very interesting module.
whose name is PyTTX. Which is used for Python text-to-speech.
So I have installed pip. I will install PyTTSX3, I will simply bring my terminal here, so I will install PyTTSX3, I will bring my terminal, I will press ctrl V, I will hit enter and you see it has been installed here and it is very easy to use it, simply copy this code and whatever I type, it will speak, okay, it's good, let's run it
I will speak this text, wow, so here we can't speak Hindi, but again you got the point, okay, so we can write in English, we will write anything in English, I will write Hey I am good, so it will play Hey I am good Now you can use any module, this was my choice of module, which I installed an external module, then I used it to perform an action, you can install Pygame You can basically use anything and there are a lot of useful modules in Python, and as we move forward, you will know how to use the modules, now the next question says
Write a Python program to print the contents of a directory using the OS module, now yes, we will do this with chat gpt, now you will say why are we doing it with chat gpt, you are teaching chat gpt here So I give you the justification for this, we are using chat gpt because if we want to survive in this AI era, then we should know how to use AI, and for that I will tell chat gpt to give me the program to print the content of a directory from the OS module So let's go to chat gpt now, so here you can see that chat gpt is open and what I will do here is that I will ask a question from it and why not copy and paste it directly, so I will do ctrl c here and ctrl v here and it will tell me this
In fact, it is searching online, but I don't want it to search, now look here, I will copy this code and I will paste it by doing problem 4, so I will come to this code now, I have made problem 4.py here and I will paste it, now it is saying that give me the path of your directory here, so why not give only slash here and as soon as I run it, you can see that it has listed all the files and folders in my C drive Okay, if I tell it here that tell me what is in the slash new folder and f is small here, then it will tell me what is in the slash new folder, there is audio, there is card 1, there is card 2, amazing, so we can list files and directories in a specific path and you can see that the code that AI has generated for me is how well commented and accurate, you can see the use of comments, AI is also doing it, it is telling us what is the use of which line, so amazing Throughout the course, I am going to use AI because we are going to solve a bigger problem, but I will request you that you don't use AI too much, I just want to tell you that by using AI, you can make a very big program with a little knowledge and I want you to use it, but until we reach chapter 4 or 5, I will request you not to use AI until you get the concepts of the whole python
5th question says, label the program written in problem 4 with comments, AI has already labeled it with comments, so you know what the solution of this problem is, usually the code we get online does not label with comments, but here AI has automatically labeled with comments, so you can delete these comments and write again with comments, you can say to AI that give me the program without comments, just for your learning, you can say give me this program without comments, and you can see that it has given without comments, I will copy and paste it, then I will write comments, so first of all I will write here, select the directory whose content you want to list, I have commented it with ctrl forward slash, I will capitalize it, then after this, use the OS module to list the directory content, then after this, I don't know what is for at this point, I used AI, but if I just do print content here, then it will work, now how is it doing it, leave all this, we have only run the online program, now if it doesn't have it, then I will slash it, there is no folder named directory path on my computer, but you can see here that it has listed all the content,
this is amazing, I love it, and here I will write print the contents of the directory, so this is the program which is commented, plus lists the content of your directory, so that was practice set 1, now we will go to chapter 2, where we will learn about variable and data type, I am very excited, let's go Alright guys, so chapter 2 is our variable and data type, and I will say that your real python starts from here, what is variable, what is data type, I will tell you everything, and I will make your fundamental so strong in python that you will never have any problem, so variable is the name given to a memory location in a program, what does that mean, you may not have understood by reading this statement, but I will also come here, where I have organized for you all, I will give you all this, handbook, handwritten notes, cheat sheet, project source code, I will give you everything, I will click on open with code, so that our folder opens in VS code, click on plus, and here I will make a python program named variables.py, so I made a python program named variables.py, what is variable, if you write here a is equal to 1, b is equal to 2,
and if I print a plus b, then what will happen, a is equal to 1, b is equal to 2, and if I print a plus b, then 3 will be printed, so if I run this program, then its output will be 3, as you can see, so what is a here, a is a variable, what is b, b is also a variable, you can consider a variable as a container, what is a container, like in your kitchen there is lentil, rice, flour, there are many other things in the kitchen, so in your kitchen there is a box, you can store things in the box, so if I bring this analogy here, so here this is a box, in which I put 1, so a is a container, in which I put 1, 1 is a number, so in a comes 1, so whenever I say a, what will it mean, it means give me 1, now let's say there is a red box, there is a blue box, there is sugar in the red box, and there is lentil in the blue box, if I say give me a red box, then what does it mean, it means give me sugar, so whether you call it a red box or sugar, it is the same thing, in the same way, when I said a is equal to 1, then 1 is stored in a, what will happen is that in your RAM, your random access memory, in that, on the memory location named a, 1 is stored, so we will not go into more details, we will just consider the variable as a container and say that 1 has come in that container, in the same way, b is equal to 2 means that 2 has come in b,
let's come back to the handbook, a is equal to 30 means that 30 has come in a, a is equal to Harry means that Harry has come in a, why is there a double quote, double quote means string, and if I write h-a-r-r-y like this, then it will give an error, but I wrote it in a double quote, so I marked it like a string, and it is important to mark it like a string because if there is another variable named Harry, then the Python interpreter will know that I am not talking about it, I am talking about Harry as a string, in the same way, I wrote c is equal to 71.22, so a floating point number has come here, here a is the name of the variable and 1 is a constant value, b is the name of the variable, 2 is a constant value, here we can also call b as an identifier, identifier means that the name used to identify, if your name is Rohan, then your Rohan is an identifier and the value of Rohan is that you are, you understand something like this, identifier means the name by which the name is called, if you say this is a cat, then the cat is an identifier, which identifies, you know that there is a cat image in your mind, so cat is an identifier, in the same way, identifiers are used to denote which variable is being talked about, if I write here c is equal to 7, and if I write here name is equal to double quote, so I am Harry, double quote means that I am talking about a string,
if I don't put a double quote, then this Harry will look for another identifier, which is not there, and that's why he is giving me an error, saying that Harry is not defined, so what does double quote mean, double quote means that a string is being talked about, name is equal to double quote Harry, as soon as I did it, it will understand that I am talking about Harry as a string, then whenever I print name, then Harry will be printed, so this was identifier, now let's talk about data types, if I talk about Harry, it is a floating point number, which has a decimal, and if I talk about Harry, it is a string, which we also write in double quote, so there are three different types of data types, primarily if I talk about integers, floating point numbers, strings, booleans, and none, these are the main data types, what comes in integers, 1,2,3,4 numbers, what comes in floating point numbers, 7.22, 8.74, 7.221, all these are floating point numbers, then in strings, whatever you write in double quote, it becomes a string, so what I will do here, I will make a file named data types.py, data types.py, and let's do one thing, let's number them, so that you know, which program was made first, which was made after that, because I don't just trust you to teach, I want you to get the source code as well, so here if I write a is equal to 1, then I will write a is an integer,
then if I write b is equal to 5.22, then I will write b is a floating point number, then if I write c is equal to Harry, then I will write c is a string, then if I write d is equal to true, which can also be false, and capital F will be false, capital T will be true, true, false, this is the boolean data type in Python, sometimes we have to store only this much, that is it true or false, yes or no, then we use boolean variables, true, false, these are boolean variables, now you see here I write d is a boolean variable, so this is our boolean variable, now if I write here e is equal to none, what is this, e is a none type variable, now why are we storing none, why, what is the need, sometimes we have to mark that there is nothing in this variable, nothing, to mark nothing, we use none, now some of you will say that to mark nothing, we can also use false, no, false yes or no, when we have to identify anything in yes or no, then we use boolean variables,
but if I have to denote that there is nothing in a particular variable, then I will use none type variable, I hope that is clear, now we will talk about rules for defining variable names, now you tell me one thing, your name can be Rohan, Akash, Izan, Sameer, but have you seen a person whose name is 722xy underscore kp at the rate, it doesn't happen, if you have seen it, then it will be a sample, I have never seen it, and it doesn't happen, now don't comment that I have seen it, if you have seen it, then do it, but I tell you that there is a rule to define the name, there is a rule that his name can be this, Srishti can be this girl's name, this girl's name is underscore at the rate bb at the rate, there is no copyright symbol, it doesn't happen, in the same way, there is a rule in Python programming language, you can keep the name of this variable a, you can keep b, you can keep c, you can keep d, you can keep e, but there are some rules, what you can't keep, I will tell you now, so I will make a file here with the name of 03 underscore rules underscore variables dot py, and I will show you that a is equal to 23 will work, aaa is equal to 435 will work, harry is equal to 34 will work, sameer is equal to 45 will work,
underscore sameer is equal to 34 will also work, but at the rate sameer is equal to 56 will not work, you can see that a red color has been given to us by the pylons, that brother, see this will not work, how will we know what will work and what will not work, we read the rules to give the name of the variable, if we understand those rules, then we will find out whether the name of the variable is valid or invalid, come on, let's read the rules, let's come to the handbook, and you see, a variable name can contain alphabets, digits and underscores, see, in the name of the variable, there can be alphabets which are from a to z, there can be digits which are from 0 to 9, there can also be underscores, underscores are symbols by pressing shift and dash, you will see in your keyboard, this one is key, see, you can see this one, this one is key, see, I have to show you underscores too, so there can be alphabets, there can be digits, there can also be underscores, but the variable name can start only from the alphabet or underscore, that is, you can use any character from a to z to start, you can also use underscore to start, you cannot use numbers to start, it will not start from 0, 0 Sameer is not a valid variable name, 1 Sameer is also not there, 1 Srishti is also not there, 1 Divya is also not there, 3 Katrina is also not there, okay, so according to this, you will have to follow this rule, you will have to follow this rule, the third rule says that a variable name can't start with a digit,
it can only start with an alphabet or underscore, it can't start with a digit, it can't be a white space in the name of a variable, this is our fourth rule, our fourth rule says that there can be no white space in the name of a variable, nor can it be a special character, we have talked about underscore, it can be that, it can be alphabets, it can be digits, it can be underscores, so if you understand these four rules to define a variable, then you will tell that the variable is allowed or not, now you see here, if I come back, then underscore Sameer is valid, why was it not valid, because it started with add the rate, okay, what is this, valid variable name, it is not there, it has a symbol of add the rate, a special character has come, if it was underscore, then it would have worked, add the rate will not work, okay, so great, you should understand this thing, okay, so these were the rules for defining variables, this is invalid, this is also invalid, I will add the rate here
So this is invalid due to add the rate symbol This is invalid because it has add the rate There can't be add the rate in the name of variable
Now you will say why such rules were made in python programming language Such rules were made in python programming language because There are some things that python interpreter uses internally
This add the rate, we will see when we will read object oriented programming Where it is used, where is this add the rate used What is decorator, I will tell you everything
And then you will know why such rules were made These rules were made so that you can enjoy python programming So that you don't get confused
That's why these rules were made This name is valid, this name is invalid You can use this, you can't use this, that's it
To talk like this That's why these rules were made for you Now let's come back to operators in python
What are operators? If you have studied a little maths If you have studied maths of 5th and 6th
Then you will know There are plus minus times divide arithmetic operators Everyone knows this
But in programming there are assignment operators Is equal to assignment operator When I will write a is equal to 1
Is equal to an assignment operator Which is saying to put 1 in a It is saying that I will call this memory location with name a
In which 1 store is there a is a container in which 1 store is there So equal to is an assignment operator
Plus equal to is also an assignment operator Minus equal to is also an assignment operator Then we have comparison operators
Which are used to compare two things Like if I say 1 equals to equals to 1 Is 1 equals to equals to 1
If I have written 1 equals to equals to 1 If this is true then it will return true Otherwise it will return false
We will see how it works Then we have logical operators AND, OR, NOT
How do they work? We will also see this So we will see these operators one by one
So first of all we will see arithmetic operators When we write 7 plus 4 is equal to 11 Then 7 and 4 are called operands
And the plus you see is called operator And 11 after equal to is called result So I will come here
04 underscore operators dot py I will make a file And we will start with arithmetic operators
What are arithmetic operators? If I say a is equal to 34 And I say b is equal to 4
And I write print Or I write c is equal to a plus b And then I print c
Print function is there Which is helpful in printing in python programming If I run this program
It is 38 If I go to my 7 plus 4 example You will see 11
As you can see 7 and 4 is 11 So this was arithmetic operator
I will make it concise So that you can understand it easily After this we will talk about assignment operators
When I write a is equal to Suppose I write 4 minus 2 So equal to is assignment operator
So I write assignment operators What is it saying? Assign 4 minus 2 in a
If I write a plus equal to I write 3 Suppose I write b is equal to 6
I want to make it easy And I say b plus equal to 3 So I am saying
Increment the value of b by 3 And then assign it to b Means increase b value by 3
And then make b equal to that value So what was the value of b? It was 6
3 will be added to b Means 3 will be added to 6 6, 3, 9
9 will be printed So let's print both of them Here I do print a
And here I do print b So if I do print b here So what will happen?
Now see once 2 came here Once 9 came What is 4 minus 2?
It is 2 And what is 6 and 3? It is 9
So what was added to the existing value of b? 3 was added to the existing value of b Because I wrote b plus equal to 3
What if I wrote b minus is equal to 3? If I say b minus is equal to 3 And I comment this line
And I say b is equal to 6 And after that I say b minus is equal to 3 So I am saying
Subtract 3 from b So what will be 6 minus 3? It will be 3
So b value will be 3 As you can see So I adjust this
I decrement the increment And decrement the value of b by 3 And then assign it to b
In the same way If I make it star 6 will become 18
If I divide it 6 by 3 will become 2 So you see 2.0 came here
But I will minus it again Because I have written decrement here But you got the point
You got the point What are assignment operators? We will talk about comparison operators
And about comparison operators I will tell you a very interesting fact That their return value is always a boolean
Means if I write a equals to And I write 7 greater than 3 So either it will be true or false
So it will be true or false in a Let me explain You won't understand comparison operators
Until I show it in code So I write here Comparison operators
And here you can see Suppose I write here I use something else
Maybe I use d Here I will write d is equal to And I wrote 5 is less than 4
And I print d So I am saying 5 is less than 4 in d
I used a comparison operator Which is less than Is 5 less than 4 true or false?
It is not true What are you saying? So 5 is not less than 4
So what will happen? False will come instead of d So I will run it and show you
You see here false came But if I flip it If I make 5 greater than 4
Then what will happen? What will come instead of d? True will come
So comparison operators always Either return true or false Comparison operators always
Return boolean So great This is comparison operators
Comparison operators are not just greater than They are greater than equal to Either greater than or equal to
If I will make 5 greater than or equal to 5 It will return true And if I will make 5 greater than 5
Is it greater than 5? No it is not greater than 5 It is equal
And it will be false Greater than equal to means Greater than OR equal to
Either greater than OR equal to You can see 5 greater than equal to
I wrote 5 here So it returned true Even less than equal to
Will return true Greater than OR equal to Either less than OR equal to
It is equal to So it returned true So these are our comparison operators
In comparison operators Not equal to also comes I will show you that too
If I say 5 is not equal to 7 So it is true It is true
So it returned true If I say 5 is not equal to 5 5 is equal to 5 actually
It is false So this thing is false That's why
The value of d will be false I hope you got the point Now if I say
Double equals to Which means Is it equal to 5?
Is it true or false? It is true It is equal to 5
So d What will happen? d will be true
Now many people Who are beginners in programming They say
Double equal to and single equal to They have a lot of confusion Because all their life
Equal to means Is it equal to this? They have always learned this
But suddenly When they are introduced in programming They are told
Double equal to Means Comparison operator
And equal to assignment operator So somewhere Their mind starts to blow
But I tell you When you have to check If two values are equal
Or not Then use double equal to Always
Remember this And when you have to do assignment Means
If you have to put a value in a variable Then you use single equal to I hope you got the point
These things are very simple And straightforward Right?
So let's move ahead I hope I could explain to you You learn python from somewhere
That's why I took the opportunity To teach you python
And It's my luck Right?
So let's move ahead Now see here We have seen comparison operators
Let's see logical operators What are logical operators? If I show you logical operators
I write here Logical operators See if I write here
E is equal to And I write here One
And then I write Or Five
Or I say True or false Right?
Either true Or false Right?
So what will happen? What will be the value of E? E's value will be
True Now you will say What is going on?
I don't understand You will say What is this?
For once you will see You will say What is true or false?
So see Where do logical operators Come from?
If you have read Switching circuits in class 12 Which you are taught
In the chapter of semiconductor If you haven't read Then I will tell you
What is this? So if I tell you Either you are a human
Or an animal Either you are a human Or an animal
Right? So is this true or false? This is true
Either you are a human Or an animal You are a human
So if I say You are a human And you are an animal
Or I say You are a human And you are a fish
Is this true? No, this is not true You are not a fish
You are not a fish So For AND
When both things are true Then it will return true For OR
If one of the two Is true Then it will return true
If I say If I say If I say
If I say If I say If I say
If I say If I say If I say
Either this or this Means one of the two Should be true
So it will return true So these are the logical operators So if I
Make its truth table And show you Actually the truth table
Is called So I made this program For you
See how easy it will become To understand for you So you will see
Its output Telling True or false is true
True or true is true False or true True or true is true
But false or false is False So what happens is
If one of the two is true It will return true Otherwise
If both are false It will return false If I make this truth table
For AND This is called Truth table
By the way This is the fancy name Of this thing
Which two operands What value is going to come This is called
Truth table So if I write here Truth table of
Truth Truth Table
Of OR This is basically
Called truth table Which two values What value is going to come
Simple So don't get confused At all
And here AND And I put AND
In single quote So that you can easily Understand
You will know This is OR Truth table
And here What I will simply do I will replace OR
With AND You see By pressing ALT
How I am bringing Multiple cursors Yes
This is called Multicursor functionality I will tell you
What is this So what happens Once
If I Press ALT In my keyboard
And wherever I click There will be one
Cursor And whatever I type
Will come in that cursor This is called Multicursor functionality
Of VS Code If you are in VS Code You can use
Multicursor functionality I hope you liked This feature
If you are a beginner If you know This feature
For the first time You must have liked it You must have enjoyed
So let's print This truth table So see
We printed OR's truth table TRUE OR FALSE is TRUE
TRUE OR TRUE is this Now here TRUE AND FALSE is FALSE
TRUE AND TRUE is TRUE Means both things Should be true
AND means Both things should be true So see when TRUE AND TRUE
Is TRUE Then everywhere else Is FALSE
As simple as that Now we will talk About NOT
About NOT NOT Operates on
Only one operand Means TRUE is FALSE
FALSE is TRUE Now if you Have seen this course
So far I will request you That you like this video
In the comment section below This python course Is helpful
If you find this course Helpful So if I write here
PRINT NOT OF FALSE Then you will see TRUE
So I will show you You will see TRUE And if I say here
PRINT NOT OF TRUE Then you will see FALSE
So this basically Operates on Boolean operators
Operates on Single operand Make TRUE FALSE
Make FALSE TRUE That is called NOT Operator
Write in the comment Someone Who makes TRUE FALSE
And FALSE TRUE That is called NOT Operator
Someone Who makes TRUE FALSE And FALSE TRUE
That is called NOT Operator If you have time
Write in the comment Someone who wins Even after losing
Is called a winner And someone who Makes TRUE FALSE
And FALSE TRUE That is called NOT Operator
Write below Okay We will all learn
From this And we all will be Thankful
I will come back In python handbook We have understood
All the operators Now we will talk About type function
And type casting For which I Will tell you
What is type casting And what is type I have already told you
Integer String None
Boolean These are all types If I want to know
What is the type of any variable How will I do that We will use
This type of code I want to copy it By pressing
Control C Like this And after copying
I will paste it In my program And basically
I will write it Line by line And I will print
Type of A If I write T is equal to
Type of A And I can Print
T And I will show you As soon as I run it
So you see Class int is written here Because this is
Integer Okay If I make it
31.2 So I This time
I have to write Class int This time
I will write Class float Why
Because 31.2 Has decimal And it is
Floating point number Because it is Floating point number
That's why I am writing 31.2
Then it will become A string I will show you
So you see Class str is being printed Look at the output
Class str is being printed What is the meaning Of this
This means That by using Type function
You can print Any type of Variable
Yes Any type of Variable
This is a very good thing That you can Print any type of
Variable The most interesting Thing is
You can change Any type of Variable
In any type of Variable On the condition
That it is valid What is the meaning Of this
If I say That this Is 31.2
Something like this I will write it like this Okay
I can write it As per my wish So you tell me
What will be its type You tell me What will be its type
In the comment section With timestamp Tell me
What will be its type And if you wrote String
Then you are A genius Because we have come
Too far And you figured it out Which is amazing
So kudos to you Because it is in Double quote
So if I run it Then you see Class str is being
Printed here Class str is being Printed here
If I run it again Class str is being Printed
So class str is being Printed It is a very good thing
But I want it As a floating point Number
So can I Change its type Can I say
Something like B is equal to A
But the type Should be Float
Is there any Programmatic way Or I will say
Float As a function And a
So float is a function int is also a function, str is also a function which takes any kind of variable and converts it to float
provided this conversion is legal now if you try to convert harry to float, you will see an error I will show you that too
and to do experiments like this, do use repl but now we are writing b is equal to float a and then we put pound symbol here, which means all this will be ignored
yes, we can write single line comments after any statement in case you don't know we can write like this, this is valid
if you don't know, that should be fine, you are learning you are learning and you have learned, that's a good thing comments are also written like this
to describe this line, write comments like this it's a good practice okay, great, very good
now see what I am saying here we gave it 31.2 and we said 31.2 in float
what will be the return? return will be 31.2 as an integer so first of all, let me do one thing, let me comment this line
and let me show you, if a is equal to 31.2, then what is the type of a? str, okay, very good and if I say b is equal to float a, then what will be the type of b?
this time the type of b has been floated, did you see? this time the type of b has been floated, which is amazing did you understand what this means?
this means that by using type function we can change the type of any variable in this case, we converted floating point number from string to a
which was a string, there was 31.2 in the string but there was a string, so we use type function to find the type
and float, str, int by using these functions we can do type conversion
so here if we do str 31, then 31 will be converted to string if we do int 32, which was a string, then it will be converted to integer we will call this string to integer conversion
if we write float 32, then integer to float conversion will be done so I hope you understood this thing and this is really very helpful
when we will make programs, then we will know the importance of this so these were the functions which convert one data type to another data type
if the conversion is valid now let's talk about input function, which is an amazing function I will tell you how input function works
for this I will make a file, yes I will make a dedicated file with the name 06 underscore input dot py suppose I want users to input numbers
and both of them become sum I can say a is equal to input and I can say enter number 1
this is a very tricky thing and you will enjoy it, listen carefully I will ask you a question, you have to tell me
what is the problem in this program enter number 2 so if I will write a is equal to input enter number 1
then what will happen? number 1 will come inside a if I will write b is equal to input enter number 2
then number 2 will come in b I will print and I will write number a is
after office what will I do? I am pressing alt shift and down arrow key to replicate a line
in case you don't know, it is coming down the key I am pressing is coming down so I have duplicated this line
and I am saying number b is and I have used alt and click I will do a here and b here
means in VS code you can write any way you want, just write quickly because the more efficient you will be
the more value you will get, in total and that's a fact what is the value of a and b?
we will give that, so enter number 1 say 34, enter number 2 67, entered
number a is 34, number b is 67 if I will put colon here then readability will increase, let's run this number again
34 56 and you see below number a is 34, number b is
56, now I will sum both of them I will write print and I will write sum is
and here I will write a plus b and if I gave 1 to a and 2 to b then 3 will come
right? right or wrong? gave 1 to a gave 2 to b, then what will come?
see what came 12 came what happened?
means how did 12 come? and trust me guys, when I saw such a thing for the first time in my life
my mind was blown, I said how can 12 come, sum of 1 and 2 I will cut short the talks
and I will tell you why sum of 1 and 2 came 12
actually this is what happens if you want to guess, tell me in the comment section let me tell
the input of value a is taken in the form of string value of b is also taken in the form of string
so let me show you repl I use repl a lot when I have to write such small programs
if I write harry plus coder
then what will happen? both the strings will get concatenated when we sum two strings pass it from plus operator
then both the strings get concatenated and here you see I did harry plus coder, so it became harry coder
and if I write something like this 1 plus 2
note that this is a string 1 is also a string, 2 is also a string I will hit enter, see what happened? 12 came
what happened? what happened with harry coder? strings got concatenated, why? because these two are strings
but if I write a number, then what will happen? it will become sum so now you understood why 1 and 2 came
if I take any other number, I just want to show you so that you don't get confused if I take 23 and 67, they will also get concatenated
2, 3, 6, 7 it is not like this, it is happening with 1 and 2 so I want this number
to be sum, add up not concatenate so how will I do it?
I will have to convert these two into integers so what I will do? I will say whatever input it is giving me, return it and make it an int
press end and I closed it, I said a is equal to int of input
whatever input is coming in the form of string whatever user is giving me input make it an integer
now see, if I put two numbers let's take example of 1 and 2 oops, here I have to type 1
and here I have to type 3 if I have to type 3, then sum of 1 and 3 is 4 if I do 1 and 2, then 3 will come
if I do 1 and 2, then 3 will come as you can see did you see that?
if I sum 1 and 2, then 3 came how cool is that? amazing
so this is the thing you have to perform type conversions as and when required
and input function is an amazing function straightforward way to take input from the user
an amazing way to take input from the user and that wraps up chapter 2
we had a lot of fun in chapter 2 and now we will see the practice set of chapter 2 in which we will practice the concepts we have studied
let's move on to chapter 2 practice set so guys, our first question is write a python program to add two numbers
so I will make a folder named chapter 2 ps and I will shift right click on this folder and open vscode
here I will make the first program 01 underscore problem 1 dot py and simply I will say
a is equal to 1, b is equal to 5 print a plus b easy peasy lemon squeezy
whatever the two numbers are, they will be added but it is important to make this program because a lot of people will only watch the video
they will not make the program I want all of you to make programs so that you guys do coding in actual
and learn so we are going to question number 2 problem 2 dot py
and here what is problem 2 telling us write a python program to find remainder when the number is divided by z
now this thing is a little tricky it has an arithmetic operator so let's say we have a number
34, we have a number let us say 5 now I want to know
what is the remainder so what I will do here, I will write print and I will write
when a is divided by b is
what will be the remainder it will be a percent b, this is called modulo
operator in case you don't know and this returns the remainder when a is divided by b
easy peasy lemon squeezy once again guys and you see here 4 will come because 4 will be the remainder
and by default the print adds a space when you put a comma you can print multiple things
using print function in case you don't already know and in case you haven't already noticed
I hope you have noticed when I run it, see when a is divided by b is 4
if I make it 37 then what will be the remainder 5 x 7 is 35 and 2 will be the remainder
so will it show me 2, yes simple easy peasy lemon squeezy you just need to know the operator
and in case you are not able to solve the practice set then don't get disappointed because these practice sets are designed to teach you
ok I have kept some learnings in practice set just so that you know
now you see here use comparison operator to find out but before that check the type of variable assigned using input function
this is also very easy, so what I will do this is also very easy I will do problem3.py
and I will write a is equal to input enter the value of a
and I will write here print type of a let's run it
so I entered the same value class str so I can see the type of the same value
I hope that you guys understood it so if I run it like this I wrote 6 class str
because it is string type, always input type is string whether I write harry
or I run it again or I write a number or I write a point
it is always a string so you should know this and you can convert it by type conversion
that is another thing I hope that you got the point now let's go to the next question
use comparison operator to find out whether a given variable a is greater than b or not
let's make a program there are two numbers a and b and we have to find out whether a is greater than b
or not, so problem let's take the value of a I will put int input
so that input will give me number in string and int will type cast it so I will enter
number 1 after this what will happen b is equal to
int input and enter number 2
I did this, you will enter both numbers and after that we will print and write
a is greater than b is a and after that
we will write whether it is a true statement or false statement so we will type is and here we will write
a is greater than b is it true or false, so here we have to take chances of a and
take chances of b, so first I will take 34 then I will take 80 false, a is greater than b is
false, so if I take another number in which a is greater than b, so I took a as 45
and took b as 2, so see what happens here a is greater than b is true and I will tell you a feature of VS code
in this practice set, I want to teach you as much as I can if you drag it and take it like this
then you can make it a tab and here keep doing your work and keep looking at its output so if you run it like this then automatically
you will come in this tab and here you will get your output, so I hope you liked this trick
so here I will terminate it because already this process was going on, I didn't put the numbers so if I run it again then it will come down again
45, 45 this is also false because I have used greater than we just have to find out
whether a given variable a is greater than b or not, whether a is greater than b or not, that's all we have to find out
we don't have to talk about equal to ok, write a python program to find an average of
two numbers entered by the user, very similar program, very similar program ok, very similar program
so 05 underscore underscore problem 6 or
5.py ok, so what I will do I will leverage this code which I have already written, I have written it, it is my code
so I can steal it, in fact stealing is not even the word, I can use my own code ok, and I can use chat gpt
chat gpt is also running here if I will do complex task then easy task I will do with chat gpt, this is called
using AI to your advantage, ok great, int input enter number 1, enter number 2, now what we have to do
we have to find the average, so here what I will do, I will write the average of these
two numbers is and what will be the average we have I will open bracket, a plus b
and divided by 2, it is important to put bracket so that your calculation will be accurate always put bracket and do calculation
never think that python will do board mask python will automatically do it for you
if you are thinking in your mind first multiply then divide put bracket, simple thing
put bracket, whatever will be in bracket will be first so a plus b will be evaluated first then divided by 2, I will run
and see here, I took 45 and after this I took 45 and 55
ok, so see its average is 50 ok, simple write a python program to find
average of two numbers entered by the user we have done it, now what we have to do square of a number entered by the user
and this is even simpler but I will show you so that you don't think that I didn't show you
problem 6.py and here I will say enter the number
here I will say enter the number let's do one thing let's copy and paste
we are not talking about number 2 I will say enter your number enter your number
and the average square of the number square of the number is
and here do a into a do a into a otherwise I will tell you one more way
you can do a to the power 2 by the way you have seen in books, a tolu 2 is written but it is not valid in python
if you press shift and press 6 then this kind of tolu will come and if you write a tolu 2
then this a square is denoted like this in many books but it is not valid in python
to write a square in python note that if you write a tolu 2 then it will not be a square
so I will do a star star 2 then it will be a square so if I run it and show you
so you see here I wrote 4 and here square of the number is 16
if I would have done a into a then what would have happened then also the same would have happened but what happens with a star star
that a to the power 3, a to the power 30 40, how much ever big power I can raise, this is the benefit of a
so let's run this program and if I write 7 here means both of them will give me 49.49
this is also valid, this is also valid what is not valid, tullu is not valid okay
tullu is not valid in python many people make mistakes I am telling you because many people make mistakes
here I will write not valid incorrect for finding square of a number in python
this is incorrect this is not how you square a number in python okay
I hope your practice set was solved very well you solved it it was fun
if you find it easy in the future we are going to raise the bar like anything I am telling you
in the coming time we are going to take things to a very high level but I want all the beginners to hold their hands and take me up
because of which I am going a little slow but remember what I said we are going to raise the bar like anything
and you are going to get all this source code so we have done chapter 1 we have done chapter 1 practice set
we have done chapter 2 we have done chapter 2 practice set I will give you this handbook, I will also give you the cheat sheet
I will give you the source code along with that source code of the project
you are going to get everything I will meet you in chapter 3 Chapter 3
we are talking about strings I had already told you that whatever you put in the double quote
it becomes a string in the python programming language what is a string? it's a sequence of characters
enclosed in quotes let's understand it closely not like this
closely we will understand it well so I will make a folder here in the name of chapter 3
because I like to keep things organized I will click on shift right I will click on open with code
this folder will open in vs code I will make a file by typing 01 intro to strings.py and I will tell you
what is a string? this is a string I wrote name is equal to harry
this is a string okay, this is a string okay, very good
is there any other way to make a string in python? yes you can put it in single quote
that's the same thing you can put it in double quote that's the same thing
you can put it in triple quote if you want to make a multi-line string remember that twinkle twinkle little star example
yes exactly you can put your characters in triple quote
and your string will be made so what is a string? it is a data type in python
it is a sequence of characters and we can primarily write a string in three ways mainly we can write a string in three ways
a, b and c this way we made three strings all three represent the same type of string
now what is string slicing? a string in python can be sliced for getting a part of the string first of all, I would like to tell you
that a string is immutable what does it mean? once a string is made
you can't change that string it means if I want to change one character of this particular string
I can't now don't get confused because I am going to explain this very well
just a heads up I want to tell you string is immutable
that means you can't change an existing string that means if this is a string then you can't put p instead of r
you will have to make a new string with that in mind let's move ahead
and I will tell you how we can slice a string if I write name2 is equal to
or write name short is equal to that means small name is equal to name and I want to get some characters of name
so what happens is there is a concept called index in python in fact, in any programming language
and the counting in programming languages starts from zero so this is my zeroth character
this is my first character this is second this is third
this is fourth okay we can also count in reverse
this is character minus one this is minus two this is minus three
when we count in reverse we start with one when we count in normal
then we start with zero with that in mind let's come back
a string in python can be sliced for getting part of these strings assume this is your string
this is zero this is one this is Two
this is Three this is Four if we do inverse counting
we start from minus one minus one minus two
minus three minus four minus five
this string's length is five because it has five characters one
two three four
five it has five characters that's why
its length is five to get the length of any string we can do something like this
we can say len of name and if I do len of name
then the length of the string will be printed but now I want to show you that how we can get
a short string of a particular string now see what happens here this concept is very important
the index in a string starts from zero to length minus one in python in order to slice a string
we use the following syntax now what you will do slice string is equal to
existing string then you will write index start then you will write index end
what is the meaning of this your mind will be blown you will say
what is this this is very difficult I can't do it
but it will be done I am there you see
come here in VS code if I write here start from zero
then you write colon zero one
two three if I write here colon three
then it means that start from index zero excluding all the way till three
excluding three okay understand
it will go from zero to two so you will have to remember this thing that whenever I write name zero colon three
then it will go from zero to two zero will be included one will be included
two will be included three will not be included that means h will be included
which is zero a will be included which is one
r will be included which is two but the other r will not be included
so h, a, r will be its output if I print name short
okay so I will print name short so here you can see
that h, a, r came okay so this is how indexing works
in strings this is basic indexing you should know
if I just want a character suppose I say character one is equal to
and I write name one something like this I can move this line by pressing alt
just so that you know I press alt I do one thing
that I toggle screencast mode I can move it by pressing alt if I press the alt key
in my keyboard so I will keep pressing alt and I will use arrow keys
so I can move this particular line so I am moving this particular line and if I write print here
and I write character one so you see that zero, one
here its output will be a so the a will be printed why a was printed
because character one is a what is character one from here we counted from zero
we did zero, one first character counting will be from zero
if you do it from the beginning if you do it from ending then counting will be from minus one
remember this I will come back to the same diagram zero, one, two, three, four
minus one, minus two, minus three minus four, minus five okay
so see here we wrote sl is equal to name index start colon index end
index start is included index end is not included it is written here
first index is included last index is not included sl zero colon three
returns h, a, r characters from zero to three sl one colon three
returns a, r characters from one to three one will be included
three will not be included zero colon three means it will go from zero to two
excluding three zero will be included one will be included
two will be included one colon three means it will go from one to two
three will not be included last one will not be included and counting will start from zero
so it is giving a, r because one has corresponding a
and two has corresponding r three will not be included so you should know this
now I will tell you negative slicing which is very easy
and you will also know you will be surprised that it was so easy
it was negative slicing so to explain negative slicing I will come to my terminal
and I will write python here yes you heard that right we will run repl
ok and here I do one thing I also make a program
zero two underscore negative underscore slicing dot py ok
suppose I have a string name is equal to harry and after this I simply
wrote print name and after this I wrote from zero to five
zero one two three four it will be complete so I will do zero colon three
so here har will come zero one or two three is not included
so far we have seen but if I tell you that man
let me tell you one thing that you will not encounter in your python programming journey
negative slicing usually usually you will not encounter
but in some cases you can get some on the basis of code
or it can come in your interview how does negative slicing work then you have to do something like this
see what does the interviewer ask you from minus four
and if we do it till minus one then what will come ok
now you will run you will say what will come
what is minus four what you do the easiest way to do this
what happens you convert minus four corresponding positive index
means you write on paper from here minus one start marking
like I have done here minus one minus two minus three minus four
minus five what is minus four one is positive
and what is minus one four is positive ok
so what I will do that I will come back here and instead of minus four
I will write one ok I will write name I will write one here
and instead of minus one what will I write what is instead of minus one
instead of minus one there is four so I will write four
so see the output of both will be the same ARR ARR
so you negative indices
in corresponding positive indices convert it
no one will tell you I am telling no one told me
when I was in my python programming journey people used to say
count from here count from there minus four means
do this from here hey brother is it negative anywhere
make it positive first after that you do whatever you want
ok now I will tell you one more thing sometimes what happens
you are asked that if you do something like this that is, nothing is written here
and four is written here so what will happen if nothing is written here
it means zero is written if not written here then it means
length minus one is written I will tell you easily while digesting
while doing ok while digesting
so see I will run it so here colon four is written
means zero colon four is written and if something was written like this if something was written like this
one colon blank blank means length blank means what
blank means length should I write one colon or should I write one colon five
it is the same thing ok it is the same thing
now see arry arry so I will write here
is same as and I will copy it here and write
one colon five ok is same as
one colon five and what is this this is same as
the one above if you leave the first blank it is same as
zero colon four zero colon four ok
four so if you see someone doing this
then first of all correct his actions means you will say
brother you are saying colon four to me I will correct it first
what you have written I will write it in plain language I will write it here
so in plain language you write zero colon four if someone tells you
tell me what will be its output print name minus three colon minus two
you tell him keep it cool what will you tell him
you will tell him you keep it cool I will convert it into
corresponding positive indices because you are a negative man you are giving me negative
means don't say like this to the interviewer ok means
I am telling you convert minus into plus ok
the interviewer will trick you you just have to do do it in your mind
what I am saying do it in positive see what will happen with practice
you will do it in your mind but practice first you can tell him
brother I want the paper or you always have in 99% of the cases
you have a computer in your hand otherwise paper is there you can always solve the question
if you are putting MCQ etc then you start marking from here minus one minus two minus three
minus four minus five zero one two three four and after that
convert negative into positive that's about it it will be done after that
ok I hope you understood I know you understood
negative indices in most of the cases not in practical programs
you will get to see ok and in my programs
means if I am working with someone he uses negative indices so first of all
I maybe him if he is my close friend
then I can give him a laugh ok which I keep giving
in joke it also means to improve
and if there is someone else then I will tell him
brother why are you using negative indices but you should know see I know
see to convert negative into positive and the interviewer will ask it's his job to ask
now see here there is a concept of skip value what is this skip value
see what happens like say this word is equal to amazing ok word is equal to amazing
what you do if someone tells you 1 colon 6 colon 2
so what will you do from 1 to 6 first of all go from 1 to 6
0 1 2 3 4 5 6 ok 6 will not be included so what will be the output of 1 colon 6
this will come now what you do in this that
take the first one ok jump two steps ahead
like after m a and z then jump two steps ahead 1 2
and after this you can't jump two steps ahead I will show you in REPL suppose I have a is equal to
0 1 2 3 4 5 6 7 8 9 I have this string now I say
a 1 colon 7 colon 3 what will be the output of this
so if I write here a 1 colon 7 colon 3 then what will happen
first of all we will resolve 1 colon 7 which will be from 1 to 6
means 1 2 3 4 means if I start marking from here then all these indices will come up to 6
so 1 2 3 4 5 6 this much portion a 1 colon 7 will give us ok
so first of all we resolve a 1 colon 7 after this we have given skip value of 3
so first 1 will come then 1 2 3 4 will come then 1 2 3 will not come
so its answer will be 1 4 and you can see that its answer is 1 4
this skip value you start from here 1 2 3
1 2 3 like this keep taking it ok
if I give you an example of a big string so I will give you g h i j k l m n o p q r s t
u v w x y z ok assume I took this and I will make it a is equal to or b is equal to
now assume we do the same thing here 1 colon assume we do 9 colon 4
ok do this so first of all what we will do how did this come
its output first of all let's see bf is its output why its output is bf so first of all we will resolve b 1 colon 9
once we have this then what we will do we will take 4 number we will write b first
after b see 1 2 3 let me zoom in it will be fun let me zoom in
see 1 2 3 4 what will come after b see b is here
1 2 3 4 what will come after b f after b f
so see b will come first then after b 1 2 3 4 f will come then 1 2
3 can't go on 4 so bf will be its output I hope you understood its clear you must have got clarity
see I zoomed in its little bit wrong but you got the point
first resolve this much then whatever number is written here jump that much
like here b will come first then after this 1 2 3 4 f will come 1 2 3 4
go as far as you can I hope you got the point and I hope this was very simple for you
everything is written in notes Now let's talk about some more advanced slicing techniques which I have already told you that if nothing is written here then
put zero in it, if nothing is written here then what to put? to put its length, 1, 2, 3, 4, 5, 6, 7 was the length so here it is 7, if it is empty here then 7
if it is here then 0, okay? I hope you got the point, okay? Now there are some functions of string by using which we can manipulate strings
or we can get some information about them the first and most important function is len so what I will do here is 0, 3, underscore
and I will write str functions.py, the first and most important function is len, okay? which tells you the length of the string
so if I have this string and I will print len of name, then what will happen? that you will get the length of this string which is 5
now see here 5 is printed, means the length of this string is 5, okay? then after this there is another method called ends with, it tells you that is this string
ending with rry so here the string which is written, that variable will come like this like in this case if my name is there, then name will come, so what I will do here
I will print, I will write name.endswith and you see how good VS code is that it is recommending you, below it is suggesting things
ends with written rry, so true will be shown here so this thing will give me the truth because this string harry is ending with rry
is rry ending with a? no rry is not ending with a, so it is false rry is ending with a, rry is not ending with a
so you should know this thing that what is ends with? like ends with, starts with starts with
and here ha if I write ha, will it start with true? now you tell me in the comment below if I write small h, will it give false or true
you are right, it will give false because capital H and small h are different characters this function is case sensitive, you should know this thing
so I wrote name.startswithha I wrote name.endswithrry, there are many methods of string like I tell you, if I write print
and I write name.capitalize so what will happen? it will return capitalized string means if my string is small h arry
so what it will do? it will capitalize it will capitalize if I write h arry r, it will not do anything to r
it will capitalize only h means it will capitalize first character and if you give something else after space, it will not do anything
so many people are confused that does it capitalize every word? there are many methods of string and we will use chat gpt
you should know this and I will tell you how we will use chat gpt we will open a new chat in chat gpt
and we will write give me most used string functions in python
you know I wrote wrong spelling of functions this is beauty it told me about len, str, typecasting
if you lower dot, it will come in lower case if you upper dot, it will come in upper case if you capitalize dot, it will be capitalized
if you title dot, it will do first character of each word means like I told you if I write harry r, it will capitalize a
so like this you can explore many methods do you need to remember them? no, I have put all the methods in python handbook
which will be useful for you so here we will do string.find
this function finds a word and returns the index of first occurrence of that word in the string so what will it do?
it comes on word case index 5 for the white space I hope you got it
now we will replace old word with new word it will replace first occurrence of old word so string.replace will replace all occurrences
let me show you I will write python a is equal to
harry is a good boy now if I write a.replace replace good with bad
it will be bad bad boy it will replace all occurrences so you should know dot replace function
now we will talk about escape sequence characters what are escape sequence characters? escape sequence characters are special type of characters
by the way, before I tell you about escape sequence characters I want you to write two methods in the comment section
you can use chart gpt I am in favor of using chart gpt people think chart gpt is bad
so I am a person who is different from traditional classroom teachers
they say don't use chart gpt because they think you will cheat in exam I am here to take you to industry that's why I am telling you to use chart gpt
and write two methods in the comment section think how much we will learn including myself
so everyone write two methods in the comment section now let's come back to escape sequence characters
if you want to print new line in a string 0 4 underscore escape underscore sequence dot p y
I have written a is equal to harry is a good boy but not a bad boy
let's say I am a noob and I don't know anything I will try to run it
what is this? this is right you can't do like this
you can't you can't you can't do like this
I want a new line after boy you can use escape sequence character use backslash
this is your backslash this is backslash I will show you
this is backslash backslash n is a special character which is manipulated in new line
we have some escape sequence characters which have special meaning backslash n means new line
let me run it if I put a new line after bad will there be a new line?
harry is a good boy but not a bad boy and a new line and space
escape sequence characters are amazing are there more escape sequence characters? yes, there is a backslash t
which gives a space I have escape sequence characters and you should know that
escape sequence characters have special meaning for example, if I want to put a double quote suppose I want to put boy in double quote
python will get confused python will ask where is your double quote python will not know
python will say this is your double quote if you want to print double quote
but if I want double quote I will write backslash double quote backslash double quote will be manipulated as double quote
python will read it as a double quote and if I run it but not a bad boy
double quote this thing is amazing harry is a good boy but not a bad boy
I love the way python is designed if I talk about single quote I can write single quote like this
if I made this string with single quote I have written single quote like this if I run it, single quote is there
if I make this string with single quote same problem will come how will I know if this is single quote
or single quote same thing, single quote will come from backslash single quote so this was our escape sequence characters
I want to request you go to chat gpt and bring some more escape sequence characters
like I showed you single quote backslash t is tab, backslash n is newline backslash backslash is literal backslash
if you have to use backslash literally I want you to go to chat gpt and tell me some more escape sequence characters
in fact more than enough escape sequence characters but go and explore yourself this will increase your productivity
by using AI but before I move to chapter 3 practice set I want to request you to like this video
and write in the comment section this python course is helpful and I will give you a heart
if you write like this, I will know you have come this far and you have completed this course so please like and leave a comment
I will feel good so guys, I am enjoying a lot let's set chapter 3 practice
and make some ambience I will make a folder for chapter 3 practice set and I will shift right click that folder
I will make a new file first question is write a python program to display a user entered name
followed by good afternoon we have to ask user to tell his name so we will input
enter your name we asked user and that name came in name variable now we will print and say
good afternoon and after this we will write name now I will tell you a new way to write name
which is a new method in python, python 3.6 onwards you write here f you have marked string as f
and you can use variables in this string if you have written f here and you have written good afternoon
and if you have written name here then name variable value will come here and good afternoon will be printed
but the condition is that you have marked it as f people used to use format function and sometimes they used to do like this
which they had to do they used to do concatenation like this wrote name, then wrote plus
then put double quote you have written one string so you have to write plus
you don't have to do this now I will run and I will write my name so you see good afternoon harry
if you want you can put comma yes you have to write f if you are using f string because if you don't write f then it will not know that you want f string
and it will be printed like this I have seen this kind of errors in applications and I understand that someone has tried to make f string
and variable name comes now we will put f we are not that kind of developer
we will write very clean code let's move to next question write a program which will fill name and date in this letter
how will we do first we will make a file and here
problem2.py dear name you are selected
and then date I will say print and I will say letter.replace
I will say do this replace the name replace it with name
which is harry this whole string will return and I will
replace this dot replace now you will say what is this
I will explain it again and here we will write date 24 september
24 september 2050 we will run it
dear harry you are selected if you don't want these spaces dear harry you are selected
24 september 2050 what happened first I said
replace name with harry now this whole string then I said replace date with this
so that string means if I don't write this and print it
so date is written like this and placeholder of date but what I did
now I can do chaining dot replace function you can do chaining
I hope you got the point I hope you understood how to do chaining
next question write a program to detect double space in a string how will you detect double space in a string
this is an interesting problem problem3.py and what we have to do
double name is equal to is a good double space
boy so I have to detect this double space so I will write
name.find find method gives us index index of what
of any substring I took a substring with double space as soon as I run it, it will give me index
it gave me minus 1 which means there is no double space if I put double space
I removed double space if I put double space it gave me 15
which means your string has double space
so this is a program to detect double space which tells me if there is double space or not
if it is minus 1, it means there is no double space and if it is not minus 1 it means there is double space
let me tell you find function returns
index of this substring in this parent string in this parent string
what does it mean it means if I write here
goo it will tell me in this string
0,1,2,3,4,5,6,7,8,9,10 11 index if I run it, its output will be 11
so it will say your goo is on 11th index in the same way, double space
is on which index and if it is not it returns minus 1
this is the rule of find method so the method of string is find which returns minus 1 if it didn't get occurrence
and it returns index if it got substring so in this case, our substring
is double space and if there is double space, it means if it is minus 1, it means there is no double space
if it is not minus 1, it means there is double space so this is a double space detector I hope you understood
make your own programs now it is saying replace the double space from problem 3 with single spaces
again, once again an interesting problem I will copy problem 3 and write
problem 4 problem4.py and you see here
harry is a good boy and here what I have to do I have to replace
so I will replace double space with single space and this is a function which will
replace double space with single space double space is gone double space is single space
easy peasy lemon squeezy it was a very easy problem I hope you understood
we used a simple method once again I want to tell you strings are immutable
if you used method on string, then original string will remain same if I print name after doing this
it will remain same so name string will not change and you will say where did this come from
this is very important so I brought this from middle name string has not changed
it has been printed as a new string let me repeat name string has not changed
it has been printed as a new string so this function makes a new string and prints it it does not change your original string
this is called immutability of strings strings are immutable I will write this, I want to emphasize
strings are immutable strings are immutable which means
that you cannot change them by executing or running functions
on them we will see the list which changes if we run a function on them
if you run a function named dot then original list will change remember this
I hope I am able to explain all the concepts so we have done fourth question now we will do fifth question
which is write a program to format the following letter using escape sequence character
now this letter is not formatted properly, we have to use escape sequence character to format it
so problem 5.py and here letter is equal to I will make it small l
because strings are usually started with small letter not with capital, classes are started with capital
which we will study later don't worry about it so how will I format it
if I print it like this then it will look like this it is not looking good, I will run it again
it is not looking good dear harry, this python course is nice thanks, I want a space
after comma, new line sorry, I will add new line from backslash n
after this I want a new line backslash n and I want
backslash t and capital t so I have used some escape sequence
characters and now see how well it is formatted dear harry, this python course is nice thanks, so I have given an indentation
and this python course is nice very good, so here
I have given this space with backslash t with backslash n I have given new line and here also with backslash n
I have given new line and that wraps our chapter 3 practice set I hope you are enjoying
do tell me in the comment section that you are enjoying and I will give a heart to all your comments
you will get a notification that I have given a heart to your comments because if you tell me that this course is helpful
I am going to give you a heart now we will move towards chapter 4 let's go towards chapter 4 with full energy
so guys I am so happy that we have reached chapter 4 so I will close the chapter 3
and shift right click and open the chapter 4 vscode instance
I will click on open with code and I will create a new file and I will name it
01-list.py what is a list and what is a tuple python
so in any programming language if you want to store many similar elements
then you use array you use list so in python programming
this type of collection is called list and we will use list to store things
so list in the simplest form is container to store a set of values of any data type
so you see here friends is equal to apple, akash, rohan, 7 or false so I will write friends here
and here will come apple and after that will come orange and after that
I wrote 5 345.06 false
akash and rohan so you see
this is a list and there are different types of data types you see
apple, orange, 5, 345.06 false, akash and rohan
now if I write here friends and after that 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
Now it's upside down, starting from 11, then upside down, 6 to 62, 34 and 1 So if you want to reverse your list, then you can use the reverse method, okay? Great, now what we will do here, we will see the insert, what is insert?
Look, we have seen append, what does append do? It inserts at the end, that is, it joins the list at the end If I have to get stuck somewhere, if I have to get stuck on a particular index, then how will I do that? For example, let's say I want to add a new item, let's say I add an item here
And I want this item's name to be 33333 and I want it at index 3, okay? So you will see here that it is telling me which index you want to insert and what you want to insert Look, it is written here that first tell me the index, then tell me what you want to insert
So you get a hint from this in VS Code So let's say I want to insert 33333, 0, 1, 2, 3 will be its index, so it will be inserted after 62 So look, it is inserted after 62, okay?
So we basically said here that insert 33333, how many 3s have I written? 2, 2, 4, 2, 6, okay 6 Insert 33333, such that its index in the list is 3, okay?
This is what I have said, when I am saying l1.insert 3,333333 I am basically saying that insert it in the list such that its index is 3 If I make it 2, what will happen?
It will be inserted after 0, 1, 2 i.e. 34 It means that its index should be 2 in this list, insert it in this way So where will it come?
Obviously, it will come after 34, as you have seen here, okay? So this is our insert method Then comes the pop method
What does pop do? It will delete the element at a particular index, okay? And it will also return its value
It will delete a particular index element from your list And it will return that element Now we are not seeing its return value
What does return value mean? Return value means that if I print l1.sort, then what will come? If I print l1.reverse, then what will come?
It will be called return value of l1.reverse In the case of list, because it is changing my original list I have nothing to do with return value
Are you understanding this? Because it is changing my original list So I am just running the function and taking a leave
I am saying okay, I have run the function on the list My original list has changed My work is only 35
So now you see here, I will comment it out and show you the pop method So if I say l1.pop and write 3 What is on 3 index? 0, 1, 2, 3
This 2 will pop out Now see, when I print, it will not be 2 It is not 2, see, after 62, 6 is coming
What happened to 2? It popped out 2 popped out And you can see here, if I print it
See what happens, whenever we run a function, it will work Like in this case, what is l1.pop doing? It is changing l1
And it will return something Means if I print it, what will come? If I do a is equal to l1.pop, I will show you this thing
So when I run l1.pop and print it So it will print the value at index 3 Which is 2 in this case
Which is 2 in this case It will print that So you saw here, when I did print l1.pop 3
So the 2 was printed here Because the value on the 3rd index was 2 The value that popped was 2
2 values were erased from the list It was printed I could have written it like this too
Like if I write here, value is equal to So basically what I said Take the return value in a container named value
Means take it in a variable named value It is called a variable, not a container I am telling you container to explain
Value is equal to l1.pop 3 And print value This will also print the same thing
See, it will print 2 only So I hope these things are very easy for you I want to tell you another way to run a python program
If you want to run a python program Then you write python By giving space, whatever code you want to run
Suppose I wrote 02 here And make sure that your terminal is open in that folder In which folder your program is
For example, I have a program in chapter 4 folder I have this program in chapter 4 folder Now what do you do?
Write 02 and give tab See, only one file is starting from 02 Listen to me carefully
Only one file is starting from 02 So if I give tab after 02 If I press tab key
It will auto-complete for me If I hit enter, the program will run So if you want to write the full name of a file in your terminal
And it is a very complicated name Like in this case, it is a very long name It will take some time to type 02
So what do you do? Write 02 and press tab It will auto-complete
But one thing you have to ensure If something else is starting from 02 Then you have to write till there
Until it becomes unique Like if there is a 02 list There is a 02 tuple
So you have to write 02 L and press tab Then it will be able to understand Which file you want to open
Anyway, this is the knowledge of the terminal I come back to the knowledge of python So if you want to print the return value
You can do something like this L1.sort, L1.reverse I don't care about the return value
Because my work is done If I want, I can print its return value But I won't
Because L1.sort will sort my list L1.reverse will reverse my list My work will be done
That's it Was our string changing in the case of string? It was not
And this is the difference That string is immutable List is mutable
So we are able to change the list Because the list is mutable Means the function is running on it
We are able to change the original list But the string is immutable Means it returns a new string
But it can't change your existing string Then comes our remove method Which removes a particular value from the list
So try the remove method yourself I want to give you as a challenge Try the remove method yourself
One method which I want you to try yourself Are there other methods? Are there other methods of list?
Yes, there are other methods And I want you to comment at least one method of list You can ask chatgpt
Tell me some more methods of list It will give you a comprehensive list I want to say one thing
The things I have put in the handbook Those are the most important methods You follow them
Whatever chatgpt is giving you Take it with a pinch of salt Means don't take it so seriously
As seriously as you will take this handbook Because I have put a lot of effort in this handbook I have put a lot of effort in this
And I will give you the handwritten version of this handbook You will get that too So yeah, let's talk about tuples
What is tuple? Tuple is an immutable data type in python Which is like a list
What is the difference between list and tuple? Tuple is immutable List is mutable
As I have already told you The string is immutable You can't change the string
You can change the list But somewhere we need such a data type Which is a collection
And along with that it is immutable So this type of data type is our tuple So let's talk about tuple
How will tuple be made? So first of all we will make a file 03 underscore tuple dot py
So if I write here a is equal to And here in the bracket 1, 2, 5, 6 And if I print and show you
Type of a So a is a tuple As you can see
You can see class tuple here So this is a tuple Why is this tuple?
Because you have made a bracket like this Now if you want to make an empty tuple So you can make it like this
You want to make an empty tuple You make it like this This is your empty tuple
So you remember this thing This is your empty tuple If you want to make such a tuple
Which is empty So how will you make it? Will you make it like this?
No, you won't make it like this I will show you what will happen if you make it like this If you make it like this
Then python will understand That you have written a number Which you have enclosed in the bracket
And you can see This class integer is telling here It's not even python's fault
So you will say I want to make such a tuple Which has only one element
How should I make it? So what you have to do You have to give a comma after 1
Then python will understand Okay, you are not talking about integer You are talking about such a tuple
Which has only one element So as soon as you run it You will get the class tuple
Printed here So here we have made such a tuple Which has only one element
But usually we deal with such a tuple Which has multiple elements And if there are different data type elements
Then there should be no problem So if I write false here If I write Rohan here
If I write Shivam here And if I run it Then you see
Class tuple You can see here Okay
So can I change the elements of tuple? You will comment this below And after putting a timestamp
You will comment and tell me Can I do it or not? If I do A0 is equal to 45
Or do 453 What will happen? Tell me in the comment section below
Just tell me this much Will there be a change or not? And I am glad
You people commented If you commented You have commented absolutely right
Can't change Because The point of tuple is
That you can't change it You can make a new tuple All those things will be done
Tuple You can't change Means
I said A is equal to So it became a stone line Just like a string
You can't even change the string If you said A is equal to
In double quote Harry So it means
A is equal to In double quote Harry will remain
You can't change Its third character Nor can you change
Its fifth character So you made A is equal to This tuple
You made it It is a stone line Okay
It became a stone line So tuple can't be changed This is the main point
Okay Now you see We have taken a tuple here
And we will talk about some methods Of tuple And there are many other methods of tuple
About which we will talk So first of all I will tell you about some methods of tuple
And what will I do here Right now I will print it
And I told you If you do print A This will happen
If you do print type of A This will happen So all these things
I have already told you Okay That class tuple will come
Now we will see Methods of tuple So
04 underscore tuple Underscore Methods
Dot py Okay So I will do one thing
Let's take this Let's reuse Let's reuse
Our This tuple And if I say here
A dot So you see That
If I do A dot count 45
Okay And I write No is equal to
A dot count 45 Print
No Now how will you know That it is changing existing
See You have to remember Which one is mutable
Which one is immutable String is immutable That's why
It will not change existing string Always a new string Will return
And In the case of list Existing list will change
But Here you see When I am saying
No is equal to A dot count 45
See A cannot change By running A dot count
Because A is a tuple Which is an immutable data type That's why
If I run it You see It has told me
That 45 Comes in your tuple Only once
Okay And if I put another 45 It will return 2
Okay Do use chat gpt I say it again and again
And I will keep saying Give me Some
Tuple Methods Using chat gpt
Is very important For you See
It has told me That Count is one
Index is one Okay I have included both
I have also included Index You see
A lot of things Have been told How to make
Concatenated tuple You always see New tuple is being made
Existing tuple is not changing They have also done Concatenation
New tuple is being made Okay So I will do one thing
Here My tuple is equal to A my tuple dot index 3
What does the index method do? Returns the index of The first occurrence
Of a specified value In the tuple If it is not there
In your tuple Then it will raise Value error
Like if I say here I is equal to A dot index
Tell me index Of 45 I will try it
Which index will it tell me? See It will return 011
That's it It will not search further Because it has got 45
Once it has got 45 It will not search further After that
Okay I will show you See
It has returned 1 01 If I do 3424
3424 3424 3424
3424 3424 If I run
It is telling Your 3424 is on 3rd index
0123 Your 3424 is on 3rd index
It is telling Hope you have understood This thing
That 3424 Is on your Index
Sorry 3424 Is on your 3rd index
It is on index 3 Index means Counting from 0
Index means Counting from 0 0123
And we will count From 0 only Because this is
Programming I am not talking Only about python
Later if you see C language Rust
C++ If you see Languages like this
Then your counting Will start from 0 You have to note
The tuple chapter And here The things
That chat gpt Has told you It is very good
It has told How to do Concatenation
You can make a new Tuple by doing Plus concatenation
Then repetition Tuples can be repeated Using star
Now it is telling If you want to repeat You have to make
A tuple Which has repeated Values
Like there is a tuple Now you want This tuple to repeat
3 times You do repeated Is equal to
My tuple Into 3 Membership
You can check If an item exists In a tuple
Using the in keyword This is also very good You can check
If there is Any element In your tuple
If you say Print 2 in my tuple Output will be
True 4 in my tuple Output will be
False How cool is that So use chat gpt
My friends Use chat gpt In your college
If your teacher Is telling you Or your professor
Is telling you Which I have heard That people say
That chat gpt If you don't open Someone else will open
And will go ahead of you Okay So here
You have to use A lot of AI You have to use
A lot At the time of learning Keep a little less
Use of AI So that you can learn Later you have to
Use AI My tuple is equal to You can take out
Length of my tuple You can take out Length
I include it here So if I Print
Length of a So its length See here
It has told 8 You will count 1 2 3 4 5 6 7 8
Okay Then slicing Exactly the same concept
Of slicing Which I told you Just remember one thing
That Slice Slice
Slice Slice Slice
Slice Slice Slice
Slice Slice Slice
Slice Slice Slice
Slice Slice Slice
Slice Slice Slice
Slice Slice When you do slicing, a new tuple will be written.
Unpacking, tuple can be unpacked into individual variables. You can do unpacking of tuple. Like if you have a tuple, if you have a comma b comma c is equal to my tuple,
then in a, b and c, 1, 2 and 3 respectively will be assigned. I hope you understood this thing. It's great, it's fun, it's fun, it's fun, it's a lot of fun.
Okay, come back and now we will move towards the practice set of chapter number 4. I am very motivated. Let's go to the practice set.
So guys, practice set 4. The first question says, write a program to store 7 fruits in a list entered by the user. So there is a list.
In it, you have to ask the user the name of 7 fruits and enter it. Did you understand? If you don't understand, I'll explain it.
So what we will do here, we will make a folder by the name of chapter 4 ps. I will close the old folder. I will open this folder by pressing shift right.
And I will make a new file, 01 underscore problem 1 dot py. So what do we have? First of all, what do we have to do?
We have to make a list. And they give a good name. It's not like that.
They give a good name. Fruits is equal to, I have made a blank list here. What will I do after this?
I will say f1 is equal to fruit1 is equal to input. So what will the user do? Enter fruit name.
Okay. What will I do after this? I will write fruits.append f1.
Okay. I will append f1. Then what will I do?
I will do the same thing with f2, f3, f4. Okay. So I will do the same thing with f2.
I will do the same thing with f3. I will do the same thing with f4. How many fruits?
How many to do? Where did the question go? We have to make 7 fruits.
So we have to make it up to f7. Yes, it is a cumbersome program. But I will tell you how you can make it better.
4, 5, 6, 7. One more. Just one more.
So here it is after 4. 5. By the way, I don't even need to change its name.
And I will tell you why. 5, 6. And here comes 7.
And by pressing alt, I will toggle the screencast mode for you first. So that you can see what I am doing.
I selected this. I pressed alt on my keyboard. I pressed up arrow.
And I can move it. You should know how to do things like this. And finally we will print our fruits.
Let's go. And you see here. Enter fruit name.
Banana. Banana. Then after that.
Another fruit's name. Grapes. Then grapes.
Then after that, I don't know any other fruit's name. Tomato. Potato.
Which is not a fruit. I don't know. You can write in the comments.
I don't even know if it's not a fruit. I selected it. I could have selected pineapple or watermelon.
But 1, 2, 3, 4, 5, 6, 7. 7 out of 7. Fruit's list is made here.
So you can run this program as per your convenience. Try it yourself. It will be fun for you.
Let's move to the next question. Write a program to accept marks of 6 students. And display them in a sorted manner.
So you have to accept marks of 6 students. It is exactly the same program. It is exactly the same program.
01. Sorry. 02 underscore problem.
2 dot py. And where these fruits are written. Now I will show you one thing.
You can double click. You can select this. Fruits.
Right click. Change all occurrences. Do you see this?
Learn this. It is very useful. Change all occurrences is visible.
Click on it. And instead of fruits. You write marks.
Okay. Here this fruit. Change all occurrences.
You can also do it by selecting. Change all occurrences. And you can also select by double clicking.
So basically you have to select this. Right click. Change all occurrences.
Now you see. As soon as you did change all occurrences. Here you got multiple cursors.
Press backspace. And you guys write here. Enter marks here.
Okay. Write something like this. Okay.
You wrote. Enter marks here. Okay.
So what will happen? That your first mark, second, third, fourth. We have to take marks of 6 students.
Okay. We have to take marks of 6 students. 1, 2, 3, 4, 5, 6.
And here you see. After that we will print marks. So I run it.
Enter marks here. 23, 1, 2, 45. Then someone got 65.
Then these are my marks. 100. And these are not printed in sorted manner.
So what we have to do? After appending in marks. We have to sort marks.
So what we have to do? We have to write marks in sorted manner. So now if I write it right.
Sorry. If I run it here. And I put here.
Whatever marks are there. If I put. So see it came in sorted manner.
Okay. It did not come in increasing order yet. Why didn't it come?
Why didn't it come in sorted manner? And our type is hidden behind it. The type of this.
What is its type? Its type is string. So because its type is string.
This has happened. So what I have to do? I have to replace input with int input.
But you see. Even after being a string. Sort function worked on it.
It sorted it alphabetically. First comes 2. It wrote strings starting from 2.
Then it wrote strings starting from 3. Then it wrote strings starting from 6. If you have seen in Excel.
If you have seen in MS Excel. Sort function. Or even if you have not seen.
It has sorted alphabetically. Basically. So what we will do?
We need integer marks. So the input bracket we have written. We will change it.
To int input. So I will do change all occurrences. And here instead of input.
I will write input in int and bracket. And again I will open the bracket. Okay.
And I just have to open one bracket. Because the rest of the brackets. I have to put it at the end.
Okay. So I will delete it by pressing the delete key here. So I wrote int.
Then I wrote input. Then I will press the end key. See I pressed the end key.
I will close it by pressing the end key. A little tricky. I have used multiple cursors.
But you should know. How to do it. I selected it.
I did change all occurrences. Then whatever keys I pressed. It came on your computer screen.
You can also do it one by one. If you are a beginner. You are not able to do it.
No problem. You can do it one by one. But I like to do it fast.
Okay. So I will run it here. I have run it here.
And see it is saying. Enter mark share. Let's run it again.
I have run it here. And you see. Enter mark share.
34 came. One came 78. One came 23.
One came 79. Mine came 100. Then I have a friend.
His came 98. And see it is sorted. In increasing order.
So he gave me here. Sorted list of integers. Earlier you saw.
The list that was being printed. Around that. A single quote was coming.
Why was that single quote coming? That single quote. Was coming because.
Because it was a string. Okay. So our problem 2.
Is also solved. Let's come to problem 3. Check that a type cannot be changed.
In python. This is a little mistyped here. Check that.
Tuple type cannot be changed. In python. So let's do problem no.
3. Come on. Solve it.
Okay. Let's solve it. Okay.
Let's solve it. Let's solve it. Come on.
Solve. 03 underscore. Problem.
3. py. And here I will make a tuple.
And I am going to name it. A. And I am going to write here.
34. And in this. Let's add Harry too.
So I will try to change. A2. And I will say.
Do A2. Larry. So what will he do?
There will be an error. You see. Tuple object cannot.
Does not support. Item assignment. This means.
Understand. This means. That your assignment.
You can't do that. In tuple. Because tuple.
Is immutable. You can't change that. Okay.
Simple. Come on. Towards the next question.
Which is saying. Write a program to sum a list. With four numbers.
So we have a list. There are four numbers in it. And what will we do to it?
4. py. So I have.
L is equal to a list. Which has four numbers. And here.
These four numbers. I have to sum them. So I have.
L is equal to a list. Which has four numbers. And here.
These four numbers. I have to sum them. So what will I do?
To sum them. I. L.
I will print. Sum of L. So sum.
Is a function. Which sums. Now what is this sum?
Sum is a built-in function. And here. What can you do?
You can make it hold a list. And it will sum you. So we have.
Some numbers. We have summed them. So I make these numbers.
A single digit number. So that you can even check. So 5 and 1 is 6.
And 6 and 6 is 12. And 12 is 12. And 12 is 12.
And 12 is 12. And 12 is 12. And 12 is 12.
And 6 and 6 is 12. So is it 12? Yes.
It is 12. Which means. That our program.
Has become. Perfect. Okay.
Come to the next question. Write a program. To count the number of zeros.
In the following tuple. So let's copy the following tuple. First of all.
So I will copy this tuple. I will make a new file. I will name it.
05. Underscore. Problem.
5. Py. Okay.
A is equal to this. What will I do? I will say.
N is equal to. A. Count.
What do we have to do? We have to count 0. And print n.
Although it is seen. That there are 3 zeros. But if there were 30,000.
Then maybe. You would not have been able to count. So here.
I did a. Count 0. So here 3 came.
So we had to count zeros. If I had to count something else. Then I could have done that too.
And yeah. That wraps. Our chapter 4 practice set.
I hope. That you understood. Tuples.
And lists. Now I will teach you one thing. Before ending this video.
What will I do? That. I will show you.
That we solved this question. Okay. Write.
Check that a type cannot be. Let's take this one. Let's take the first one.
Okay. Let's take this one. Let's take this one.
This is the question. Okay. And I will start a new chat.
I will say chat gpt. I have. Submitted.
This answer. To this question. Suggest.
Me. I have. Submitted.
This answer. To this question. Suggest.
Me. I have. Submitted.
This answer. To this question. Suggest.
Me. What I did. Right.
Or. Wrong. Now I will hold my answer.
So here was my answer. I guess this was problem number 2. Yes.
This was problem number 2. So I am telling chat gpt. See.
I solved it in this way. Tell me. So he is telling.
Your code correctly accepts marks for 6 students. And displays them in a sorted manner. Here are some suggestions and improvements.
Correct use of list and append. You use a list to store. See.
I did this right. Now he is saying. What are the suggestions of your improvement.
And he told me something. That. I have not read for loop.
So I did not make you do it. But. You got the point.
Chat gpt. Does not just solve the question. He also has the courage to tell you.
Where you are right. Where wrong. And what could have been improved.
That's why chat gpt. Include in your flow. Include in the learning flow.
Do it in the professional flow. Include in the learning flow too. Come on.
This learning is very good. I hope you liked it. If you liked it.
Then. Like it. Share it.
Share it. Share it. Share it.
Share it. Share it. Share it.
Share it. Caitrion is very good. Now I am going to go to chapter 5.
Because that Very important. So guys what is chapter 5. It's a amazing chapter.
That's because. We are going to study about sets and dictionaries. So.
Now what I will do. I will come to this PDF. Which I have made for you.
Dictionary and set. And our existing folder. In the chapter 4 practice set.
I will close it. What are Dictionary and sets. This is also a minority learning.
But. If one is selected. This is also a collection.
This is also used to store data. In python. But.
How is it different from list and tuple. I tell you. The dictionary in python programming.
They are key value pairs. Okay. Like.
If you want to store the names of some students. And their marks. So you can map their name.
With their marks. And like this. Many.
Names and marks. You can store the pairs. What does that mean.
And how to do that. In python programming. Come on.
I will tell you. What does that mean. And how to do that.
In python programming. Come on. Let's learn.
So. What will I do. I will come here.
I will make a folder. Of chapter number 5. Chapter number 5.
And after making a folder of chapter 5. I will open it in VS code. So the first file that is going to be made.
That is. Dictionaries. Dig.py.
So what will I do. If I write here. Let marks.
Oops. Marks. Is not there.
What will I do. I will open this. I will open this.
So. What will I do. I will make a folder.
Marks. Marks. Is equal to.
And I write something like this. And I write Harry. I will say.
Harry's marks are. 100 out of 100. Then.
Shubham's marks are. Suppose. 56.
Rohan's marks are. Which is not a good boy. He could only get 23.
So 23. And now what will I do. If I print.
Type of marks. First I will print the marks. Then I will print the type of marks.
Fine. If I print these two. Then you will see.
And along with that. Class dict has been printed. Which means.
This is its type. Dictionary type. And this.
Dictionary type. Python has recognized it. Python is telling.
Brother. This is a dictionary. The sole use of a dictionary.
This is that. You can play in key value pairs. In short.
The thing is. If you want. Key.
Value. Be associated with each other. And that's it.
You can play in. Key value pairs. And that's it.
You can play in. Key value pairs. And that's it.
You can play in. Key value pairs. Now.
What is the benefit of doing this? I will tell you a little inside. That.
Why is the need of dictionary in python programming? Basically. What happens is.
This lookup. That is. If I have 1000 marks.
1000 students. Then this lookup. Is very fast.
That is. If I. Find marks of harry.
Then. Big O. Of one time.
I will be told. Here. That.
100. Fine. You understand this.
Harry's marks. 100. Shubham's marks.
56. So. If I.
Try to write this thing. In the list. See.
I am telling you inside. You will feel a little uncomfortable. But.
I will tell you. Because it is important to know. If I store like this.
Harry's marks. 100. Means.
I can make a list of list. I can make a list inside the list. List of list.
Is allowed in python. So. I can make this.
But. If we do this. Then.
We directly. Take the name of harry. And not take the 100.
So. and if you write complex logic then it will be computationally expensive anyways these are high level talks so I will delete them
but in total you should understand that dictionary data type is very important in python programming and it is very important for us to understand it coming back to the notes
if you have written key, value, harry code, marks 100, list then you can print its values like this similarly if you write a list then you will get 1 to 9
because list key corresponding is 1 to 9 I hope that makes sense properties of python dictionaries
let's talk about some properties first of all it is unordered second it is mutable
third it is indexed what does it mean? it means first of all order doesn't matter in this
second thing it is mutable what does mutable mean? mutable means we can change
list was mutable string was immutable tuple was immutable
dictionary came in the game this is mutable means you can change existing dictionary you can add things in dictionary
you can change corresponding values of existing keys and along with that it can't have duplicate keys
so this is a very good thing it is indexed means it is indexed
means if you say Rohan then you will get 23 immediately it won't look up one by one
it is not like marks will check is this Rohan? is this Rohan? no, it will be indexed
and corresponding 23 of Rohan will be printed so let's see some methods of dictionary so what we will do is
we will keep our dictionary and after that we will see dict methods so I will write methods here
.py and here I will keep our marks dictionary like this so can key value pairs
be like this? can it be 0, Harry? yes it can be here you can
use string you can use integers now I want to tell you some methods of dictionary
the first method of dictionary is .items so if I print here and I say marks.items
so marks.items what it does is it gives all these items of dictionary
it gives a list of key value pairs and key value pairs which we get are in the form of tuples
now I will tell you so if I print here marks.items so you can see
I got dict items now I can iterate it using for loop which we will read later
next method is .keys if I do a.keys here
if I do a.marks.keys here in this way so I will get marks.keys
you can see I got marks.keys now I will do one thing just so that you have this code
so I will write it like this so what is the meaning of keys? Harry, Shubham, Rohan and 0
which is on your left hand side and on your right hand side 156, 23 are values
and we can print them by doing marks.values so if I do marks.values
so dictionary values are printed so I hope you understood what is marks.values and
what is marks.keys now what we can do is we can update our dictionary
for example if I write here if I write here
marks.update and I will say that my Harry
his marks should be 99 cut one of his marks no one should give 100
so this happens in CBSE if you know then tell me in the comment one mark is cut
99 is given not 100 so you can see here marks.update harry colon 99
so my original dictionary will be updated and this will happen because my dictionary is mutable
so you will get the proof of mutability here if I print marks
and I will comment this values print my original dictionary has Harry 99
so what you do is you write marks.update and pass a new dictionary and in that dictionary
the keys which are matching they get updated and the keys which are not like Renu
I will say Renu is more intelligent than Harry his marks are 100 so Renu will be added
you can see so the key value pairs which are not there will be added where they are not there in marks
if I write marks.update I have written marks.update I have passed a dictionary
so the dictionary which I have passed in that the key value pairs which are there in my marks
they will be updated like Harry 100 so it will be updated and the ones which are not there
like Renu was not there that will be added means the dictionary will be updated
according to this the key value pairs which are there will be updated
it is simple so this was our update method which is a very simple and straight forward method
now I will tell you the get method what is the meaning of get method if I write a.get name then what it will do
it will return the value of the specified keys and value is returned so I will show you here
if I come here and write print and I write marks.get
if I write shivika and I write shivika and if I write
harry then what will happen then I will get the marks of harry which is 100
I have commented this 99 so it is showing 100 now you will say that marks.get
harry and this marks.get harry what is the difference
and this comes in the interview so listen carefully what I am telling you if you say to me
marks.get harry and marks.get harry means you said that
this is marks.get harry this is marks.get harry if you say to me that they are same
then I will tell you that they are not same why they are not same because if I run it now then both of them gave me 100
it is same but if I write harry2 then this line will give me none
but this line will give me error and if you use square bracket then you will get error
it does not exist in the dictionary and if you use dot get method then you will get none
so I will run it and show you you will get key error but here you will get none
I will write here prints none and what will it do
returns an error it will return an error as simple as that
so I hope you understood so these were some methods of our dictionary now I will tell you to go to docs
but better than docs I will tell you to go to chat gpt and go to chat gpt and ask
give me some dictionary methods
and if you write python here then it will be a good prompt give me some python dictionary methods and see here
dig.clear is a very good method it will empty the dictionary another method is dig.copy
it will make a shallow copy of the dictionary you can use some keys to make a new dictionary
in which what will happen the keys will be taken from here and the value will be zero so these are some methods
that you can use, pop is there pop item is there, set default is there update is there, but the examples
I have told you here those are the most important methods rest you can always use chat gpt
and google, like pop method of dictionary is very famous, you must try it out
I will tell you which methods you must try you must know pop method
where is pop method you must know pop method then you must know pop item
so you can see pop and pop item rest I have told you all I hope you understood
and now we will talk about sets what is set set is a collection of well defined objects
did you remember if you are a science student then you must remember set is a collection of well defined objects
and here I will teach you some mathematics you will remember mathematics
and you will remember mathematics because it is based on mathematics it is based on mathematics
if you have seen chapter number 1 in your class 11 I am talking to a science student
commerce students also study maths so you will know that this set
how it works I will tell you from the beginning what I will do here
03 underscore sets dot py if I write here s is equal to
so what will happen it will become an empty dictionary if I do like this
it will become a set so if you want to make an empty dictionary I will write here
d is equal to if you do like this so what will happen
it will become an empty dictionary so I will write here empty dictionary
so this is the way to make an empty dictionary so let me explain you so if you write here python
and you write here d is equal to and then you write
print type of d so this class dict
so how to make an empty set because set is written like this we know that there are key value pairs in the dictionary
there are key value pairs in the dictionary but in the sets you just put values
and both sets and dictionaries are made using curly bracket
so now you will ask me that ok what you said was very good
15 or 32 will make a set with values 15 or 32 and dictionary is written like this
but how to make an empty set because set and dictionary are enclosed with curly brackets
so to make an empty set I will put here e, you can put any name
it should be a valid identifier e is equal to set so if I come back to apple
and this time I will zoom otherwise you will say that why this person doesn't zoom python and then
if I write here s is equal to set and if I print type of s so this class set is giving me
so to make an empty set this is not used this makes an empty dictionary
this is used to make an empty set set don't use s
is equal to a as it will create an empty dictionary
so this is asked in the interview and many people make this mistake and reject the interview
that you don't even know how to make an empty set you want me to hire you understand
so don't become one of them now see what is set why is set used
why is set made in python no element can be repeated in set if you wrote 15, 32 in set
if you wrote 5, 5, 5 twice and if you print s
so you see here let me run this so here what is
our set it will take only once although 5 is written here 4 times
but it will take only once because elements can't be repeated in set so whenever we have a use case
in which elements can't be repeated we use set I hope that is clear
so 15, 32, 54, 5, 5, 5 we wrote here but we got 32, 1, 5 and 54
now you see the order here it didn't maintain so note this thing
if you want order then you have to use list but if you just want
that my elements can't be repeated then you can do something like this I hope that makes sense
now the way I told you about set I told you about list, double, dictionary, sets
I will tell you about 04 underscore set underscore methods dot py
let's go so we will use this set other data type elements can come
like I can write here after 5 can I do this? yes I can
so if I do print s and type of s and run it you see my set is printed
and class set is telling me which means it is valid I can do this, very good
now I will tell you about set methods in set methods most important method is add
repetition is not allowed in sets so if you are using add in sets then what I will do
here I will simply write s dot add suppose I want to add 566, I will print again
s and type of s so it will remain set and 566 is also added
and class set is showing me by the way you can make it as a tab just so that you don't know
if you don't know then let me tell you you can see it as a tab so you saw that the methods of set
in s dot add 566 if I type s then 566 is added
now I will come to chat gpt and I will say give me some most important
set methods in python let's make it set methods in python and it will give me set methods
so here you see we have seen add, after this there is a clear which clears the set and there is a copy
rest difference and difference update I will tell you there is a discard
after this there is an intersection so we will study about all these before that what we will do
we will study the properties of set first of all they are unordered you can't
expect order from set it's not like you gave 1,2,3,4 so you are thinking it will be stored like 1,2,3,4
we have already seen this example our 32 was being printed later now you see here
there is no way to change items in sets once you added in set it's not like you do something else
you can remove that item and add a new one but changing existing item doesn't make sense
because set is a collection of well defined objects which are not being repeated sets cannot
contain duplicate values sets can't contain duplicate values I have already told you
and sets are unindexed means you can't access elements of set by index
it's not like you can count 0,1,2,3,4 now let's talk about operations
on sets, what all operations you can do on sets first of all, you can take out the length of a set you can also take out the length of a dictionary
in case I didn't tell you in dictionary that you can take out the length of a dictionary so let me tell you that if you here
I will write it as D, I don't want to confuse you if I write here 0 colon 1 and 1 colon Harry basically I am making a key value pair
I will close it with curly bracket and if I length of D, you will get 2 here because it has two key value pairs so length works in dictionary and length works in set
length is a function which works in our list, works in tuple, works in dictionary and works in set it will return 4
because length of set is 4, it is returning 4 because it has 4 values we can remove any element and our set will be updated, if we run s.remove, let me show you
if I write here s.remove and I write here 1 then 1 will be removed from my set and if I duplicate it, just so that you know how I duplicated it
I will toggle the screencast mode if you press alt shift down arrow key it will be duplicated, by pressing alt you can take it down with arrow key
like this, these are the shortcuts of VS code, just so that you know if we run it, you can see what we did we removed 1, that's why 1 is gone from our set
after we ran s.remove after running s.remove, 1 is gone I hope you understood this
now I will come back and show you s.pop what pop does is, it removes a random element from the set and I won't say that you should use pop on your set
because it is removing a random element from the set, which is not good in my opinion what clear will do, it will clear your set
and your set will be cleared if you run s.clear now I will tell you about union and intersection if I talk about union in sets, then union means
take those values which are in both sets and I will make a new file for this and if you are a math student, then you must be knowing this
set union intersection dot py suppose I have a set called s1 and its values are 1, 45 and 6 and I have a set called s2, in which values are 7, 8 and 1
and let's take one more, let's take 78 if I say print and say s1 dot union and I say s2, I say s1 dot union s2
then union means take its values and take its values with no value repeated whatsoever so what will happen, 1, 45, 6, 7, 8, 1 was there and 78
so now see here, all these values are there value can't be repeated in this set, this was our rule from the beginning so that rule will also be followed, so what will our union do
union means take the values of these two sets now there is one thing called intersection in sets intersection means, take the common values in these two
so what are the common values in both, there is only one so what will happen in the value of intersection, there will be only one there will be a set in which there is only one
if there would have been 78 here also, if there would have been 78 here also then what would have been its union, union would have remained the same because its values and its values will also be included in union
intersection means, make a set in which there are overlapping values which is 1 and 78 and union and intersection should come to everyone
so at least these two should come to you there are many more operations on sets, but I will say that these should come to you so 1, 6, 7, 8, 45, 78 and 178 intersection came
so this is union and this is intersection I hope you understood this thing so if I run it again and show you, then this is union and this is intersection
rest you do the experiment yourself, if you have any doubt that what will be its output how to do this, how to do that run RAPL in your computer, right click and click on the terminal
and click on the terminal and write python and run RAPL means no one told me that this is free, means sometimes we feel that who should do it run RAPL and see for yourself
you will learn more from that if you ask someone that what will happen if you do this in union do it and see for yourself, I hope that makes sense
there are many more operations in our sets but I will tell you only union and intersection if you are not a math student and you don't want to be associated with math
you are learning python programming only for web development or for AI so for now you can learn only union and intersection before going, I want to explain this diagram to you
suppose the first circle has area A and this circle has area B and this is R1 and this is R3 means this area is R1 and this area is R3
to explain this better, I will take a snip and explain it to you so basically if you see here I will take yellow color
this area is R1 and if I take another color so this area is R3
and R2 is the overlapping area so if I take this color Cn so this is R2
and what is A? A is the whole circle and B is the whole circle I don't want you to get bored by listening to mathematics if you haven't done much mathematics
but this diagram is very important and explains things very well R1 is this much area
so if I talk about R2, it is A intersection this is the symbol of intersection intersection means the values which are in A and B
and this overlapping area is called R2 and this intersection is called B if I talk about R1, R2, R3
then it is A union B means the values which are in both the sets means the combined values of both the sets
after this, A triangle B R1 plus R3 is A triangle B so if we talk about R1 and R3
then it is A triangle B then R1 is A minus B A is the whole area
and B is the minus area so it is R1 and R3 is B minus A
I hope you understood this so in total this is how it works
and if I show you in RAPL and let's say I wrote Python here let me zoom in
by the way, I am going a little more into mathematics but I want to explain this to you in case it is useful to you
so what I will do 1,23,45 and I will write S2
let us say 6,1,3,23 and then
54 if I say S1 minus S2 then it will give me a set
the values which are in S1 will be minused by the values which are in S2 so 1 will be deducted
and 23 will be deducted, so only 45 will be left why only 45 is left? because the value which is 45
is here but not here, so it is S1 minus S2 now the way we saw
that we can minus in set we can also do these operations in set I have already told you about union
but if you come here then you see there is a difference method, there is an intersection we saw union, this subset
this superset, there are two methods what does subset mean? let's say I have a set, now I don't want to go into set theory
because this is a Python tutorial but I will tell you about this subset for example, what will be the subset of this?
1 will be its subset 1,45 will also be its subset subset means
any set will be a subset when its elements are already in this set
so will 6,78 be a subset of this set? yes will 1,2,3 be a subset of this set?
no, because there is neither 2 nor 3 will 7,8 be a subset of this set? yes
why will it be a subset? because there is neither 7 nor 8 so this is a subset
and this will be a superset of that subset so if you think this is too much information
then you can use these two methods I will tell you to use s1.issubset and set1
so if there is any subset then it will tell you or else it will return false
now see, our set1 is 1,2,3 so this is a subset of 1,2,3
that's why .issubset is returning true so I will tell you to spend some time on this
if you want to go to set if you want to learn related to set
then I will tell you to spend some more time on set so that was about chapter no.5
I am so happy that we completed chapter no.5 now we will see practice set of chapter no.5
it is very important practice set is very close to my heart so we will solve this
and then we will move to chapter no.6 I am very excited so let's go to chapter no.5
practice set alright guys, first question of chapter no.5 practice set
so I have closed my existing VS code and I will open with code on chapter 5 PS
and you can see here I will write 01 underscore one dot py
so I have written problem1.py here first question says write a program to create a dictionary we have to make a dictionary
of Hindi words with values as their English translation so you have to tell
values as English translation like I will tell you so what we have to do
words is equal to like I will write help and I will write here help
then I will write chair chair
then I will write cat I will write here cat then what I will do
word is equal to input enter the
word you want meaning of
here a word will come and after that you have to print words
word now see what I have done here this word is a variable
and whatever you will input it will come inside the word and when you will do words
then what will happen if you have written help then words will come and if you have written something else
then it will come and what I will do here enter the word
so you have seen how easy it was to solve this problem first of all we made a dictionary
after making this dictionary we used input we said inside the word
tell us the word you want to know then we put this variable here
words word and it will be printed if I will write a word which is not in our dictionary then what will happen
then you see here this key error is giving me so here is a condition and what is that condition
that word should be in your dictionary alright let's move on to the next question our next question says
write a program to input 8 numbers from the user and display all the unique numbers
once so you have to input 8 numbers and this is a simple classic problem
of sets I have told you the use case of whole set so what I will do here
I will write n is equal to input and I will write enter let's capitalize this
number 1 and how many times you have to do this you have to do this
8 times so what I will do I will use same variable and here
I will take 8 times input and after taking 8 times input what I will do I will write here
first of all we will make an empty set and we know that set in fact an empty set is made in this way
we know this and after this what we will do s dot add and n but no
we have to do int n or we have to do int input because we have to make set of integers and this was the trickiness of this problem
that you have to make integer and then put it in set so yes
and here I will not do number 1 why not I will do like this enter number and I will
duplicate it 8 times 7 times 1 2 3 4 5 6 7 so here I pressed on keyboard
alt shift and down arrow I will show you one more time
alt shift down arrow 7 times and finally what we will do we will print set
so you see here when I will run this program 3 5 3
2 1 1 2 2 and see it gave me 1 2 3 5
because unique numbers were to be displayed did you see how easy it was
and the question number 2 I have framed it in such a way that you get the use case of set
that you will know how to display numbers uniquely can we have a set with 18 as an integer
and 18 as a string as value in it is it possible let's try
so what we will do is 0 3 sometimes to do some things you guys
should run the program like let's say I make a set set.add
18 s.add s.add 18 as a string
let's print I want all of you to comment below and tell me what will be the output
will it be 18 once or twice do put a time stamp so that I know what problem solution you have given
so let me run it and show you I have run it and yes you see 18 as a string and 18 as an integer
both are in your set because these are two different values one is integer and one is
string so yes we can do this so what is the answer yes we can have a set with 18 as an
integer and 18 as a string value in it now see here it is saying
what will be the length of the following set you added 20 then added 20.0 then you added 20 as a string
the answer is simple these are three different values length of s will be
23 so here let me copy the code question number 4
question number 4 where did you go vs code question number 404
underscore problem 4.py and we will print s
sorry length of s let's print s length of s will come
what is this we thought 3 will come but why 2
and this is a very important interview question in python let me explain it to you
it's my duty and it's very important so I will write python here and I will write 1 equals to equals to 1.0
you will think that 1 equals to equals to 1.0 will return false in python programming but actually it returns true
because when both numbers are same python floating point number and integer number shows true
and you can see here it's true now if I take this thing to chat gpt and I ask chat gpt to explain
explain this behavior so what will happen what did I write behavior
now see here chat gpt explained so use chat gpt use AI
see here it is telling see comparison operator it will say that
comparison operator checks values if both values are same then data type doesn't matter to python
so here you can see that 1 equals to equals to 1.0 evaluates to true
because while comparing floating point number and integer python compares their values
and determines that if they are numerically equal then true
so this is python's property and because of that what happened
that the length of this set what will be the length of this set it will be 2
what an interesting question right what an interesting question so if you do len of s
so what will happen it will be 2 very interesting question
and that's why I have put it comes a lot in interviews so you guys
ask this to your friends who think that python knows ask this question
maybe they will get it problem number 5 says s is equal to
if you made it like this then what is the type of s many people will say it is set
no bro this is dictionary if it is s then it doesn't mean it is set are you getting it
understand the ways to confuse s is equal to the interviewer has given you
with an empty curly bracket he is giving you that hope that if you have written the name of the variable as s
then it will be set no bro if I call this
if I call this as iphone 15 pro max then it won't be iphone 15 pro max it will be this AC's remote
so it is the same thing I hope you understood the things what will be the answer to this
I have already told you the answer to this s is equal to do this
if you do type of s then it will be dictionary alright let's move on
to the question number 6 and I will solve question number 5 here quickly
05 underscore problem 5 dot py s is equal to do this
print type of s and it will be a dict because it is dictionary
now what we will do is we will make a place for problem 6 problem 6 dot py
what is problem 6 saying make an empty dictionary allow 4 friends to enter their favorite language
as value and use key as their names like harry python alright
shubham c++ rohan assembly language like this alright
assume that their names are unique alright their names are unique
so what we have to do we have to write a program in which what we will do
first of all we will make an empty dictionary empty dictionary is made alright
then what I will do name is equal to I will do input and I will write
enter friends name alright then
lang is equal to I will do input and I will say enter language name
enter language name and after that what I will do I will add in dictionary
what I will do d dot how to add
I will write d dot update name colon lang and you see here how many times I will do this
I will do this 4 times and why I will do this I will do 1 2 3 times more
means total 4 times why I am doing this 4 times because I have 4 friends
I have to do this for 4 friends you can see here now if I run this program
then you see after running this program assume I am writing here harry
and then I am writing python actually at the end here I should have printed d
then only you will see how my final dictionary is looking so I will type quickly
here I will write harry then I will write python then I will write rohan
then I will write assembly then I will write shubham then I will write c
then I will write divya then I will write cpp and you see here
our dictionary is made name is in key language is in value
harry python rohan assembly shubham c divya cpp easy peasy lemon squeezy now see question number 7
if the names of two friends are same means in this case if the names of two friends are same
then what will happen then nothing will happen just that
our update method will keep updating like if I write here rohan c
then the language entered later will be considered as rohan's language I will show you
like if I write here shubham python harry python
suppose I wrote c for rohan and then I wrote js for rohan so only js will come for rohan
because rohan's key got updated till here when I entered this input till there rohan's language was c
but after that when other rohan entered js then it got updated because I used update method of dictionary
I hope you understood this I hope you understood this when I wrote d.update
then my existing dictionary got updated I hope you understood this next question says
if the languages of two friends are same then what will happen in the program see if the key is same
then it will get updated like here we saw shubham, harry and rohan were our keys
because rohan entered twice and it got updated in the second time once it got set it was c
and then it got updated to js but if the value is same like here shubham and harry are both python
the value can be same because in dictionary identifier is key
like it's identifier is shubham if I want python then I will write
d.shubham then I will get python d.harry then I will get python d.rohan then I will get js
so the value can be same but the key which is our identifier should not be same
or else you will face some problems, basically it will get updated like we saw already
I hope you got the point question number 9 says can you change the values inside a list which is contained
in a set, so it's a very interesting problem, so let's do this and try to update
and can we update or not, we will get to know so this is problem number 9
so here I will make 07 underscore problem7.py
and I will write here the values entered
later will be updated and here in 08 I will write
problem8.py and here I will write nothing will happen
nothing will happen the values the values can be same
I have written here now the 9th problem code I have copied on clipboard
so I will write problem 9.py and I will paste it here
and what we will do we will try to change it how to change it
I can't do it now tell me what do you want me to do should I do this
0,1,2,3,4 but I told you there is no indexing in this set but suppose some idiot
try to do 0,1,2,3,4 and after that he says to make 0
is equal to 9 python will say what are you doing what did you eat, what did you drink
what did you blow, so this will say python you can see here S40 is equal to
9 is written here he is saying an hashable type list he is saying that first of all
list can't be a hashable set because list is not hashable second thing is
even if it is you are not allowed to do it so there are two reasons
we can't update first thing is we can't include list in a set
second thing is if we could do it we can't change value by indexing
do you understand so if I hold this chart gpt and say to chart gpt
give me more wisdom around this I will give him the whole question no you cannot change the value inside a list contained in a set
infact you can't even have a list as an element in a set because sets in python require all the elements
to be immutable and hashable do you understand this all the elements in your set
should be immutable and hashable lists are mutable I have told you what is mutable
it can be changed and not hashable so they cannot be added to a set
now he is saying whatever you want to do he has given more wisdom
that's why I am saying chart gpt or whatever AI is use AI to your advantage
use it to learn fast if you don't understand anything ask chart gpt to give you wisdom
he will give you wisdom do you understand I hope you understood
I hope you understood everything in the coming time
we will do more powerful projects but for now let's move to chapter 6 see you there
guys chapter 6 is about conditional expressions now what is conditional expression see what happens sometimes
we do our work according to certain conditions for example let's say user entered 7
maybe we want to show him that you entered an odd number and if someone entered 6
then we want to show him this is an even number so to make such programs
we use conditional expression I will explain with some analogy I will explain with my temper
I have made chapter 6 folder I will shift right click and open with code and this folder will open
so the first program is conditionals.py what is conditionals
conditionals is if, else, elif in python why do we use it, how do we use it
see what happens sometimes you work according to certain conditions today is sunday, let's order food from outside
we can't order food from outside everyday infact it's not healthy so let's do it on sunday
let's eat ice cream on sunday maybe we won't eat ice cream everyday but let's eat ice cream today
so this is a condition that today is sunday let's eat ice cream today is my friend's birthday, so I will give him a gift of 10,000
no one gives a gift of 10,000 but again, if someone is giving a gift of 10,000 then on birthday
it doesn't mean that he will give it everyday it's a condition that it's your birthday that's why I am giving you a gift
otherwise your face is not so good that I will give you a gift everyday so here sometimes we want to play pubg on our phone
if the day is sunday let's play pubg on sunday, we study the rest of the day like this
why do we order ice cream online if the day is sunny if the day is sunny then we will say that it's too hot
the temperature is 40-50 degrees let's eat ice cream sometimes we can go hiking
if our parents allow us or mom says that there is no need to go hiking then we won't go
this is the type of scene this happens in python programming also that we want to execute some instructions
only when a particular condition is met
one more time in python programming we must be able to execute instructions on a certain condition
being met and this is what the conditionals are for
till here we understood now what we will do is we will understand this in code
suppose I said user enter your age enter your age
and obviously the input is string output
so we have to type cast it to make it integer so I put a is equal to int input
now I can say if if means if the age is
greater than 18 I will put colon this is the syntax of python
understand things here I put colon colon means
you have entered now as soon as I press enter my cursor didn't come here
it came here and this space is called indentation in python
one more time when I wrote if greater than 18 I put colon
which is the syntax of if as soon as I hit enter it will automatically give me this space
this space is put in python to indent indent means you have entered
inside the if I wrote print you are above the age
of consent and after this hit enter and come back
means you were inside the if this space means you are inside the if to come out of if
you have to come here and else you do print
I will copy this I will copy this you are below the age of consent
ok, now let's understand this program I will toggle the screencast mode for you
if age greater than 18 then we are inside the if you are above the age of consent
and if I want to write another line inside this if suppose I want to write good for you then I can write that
but I have to be inside this if means I have to give this space space means you are inside the if
you must have seen in English that it is done like this that we are inside this heading
so indentation is given you must have seen this thing in books that there is a heading
then whatever is written below is written with a space and when the second heading starts
then we come back here indentation is used in python and by using this indentation
we can come inside the if inside the else and outside the else
now here this if is connected to this else if your age is greater than 18 then this will be printed
if less than 18 or equal to 18 then this will be printed so I will make it greater than equal to
just so that greater than equal to let's run this program now someone will ask me to enter your age
do you know? I entered 56, you are above the age of consent, the spelling of consent is wrong, consent or good for you ok, you are above the age of consent, good for you, so if I run it again and write my age as 5, you are below the age of consent
ok, and here I correct the spelling of consent again, and if I write 0 here then what will happen? if I write 0 then what will happen? so you see you are below the age of consent so what will happen is that the program's control, once we get only one value, the program will enter this if
it will say that whatever it has entered is greater than 18, if yes then print this means execute whatever is inside if and ignore the rest else after this our control will come here directly, control means the program will come here directly
let's say I write end of program, let me run it and explain the flow, let's say I wrote 56 here now you see what happens if I write 56, you are above the age of consent, good for you means if this condition is true then the program's control comes inside this
and it executes the instructions inside this and ignores else because when the condition of if is true then else does not work else works only when the condition of if is false, so because the condition of if is true here so it will run and after running the program will jump outside the else means here
that's why end of program was printed, I hope I explained this to you, this is if else now I want to tell you about elif, sometimes what happens is that we do things on the basis of multiple conditions let me tell you the use case of this, first of all I will check whether 1 is greater than or equal to 18 or not
if it is greater than or equal to 18 then I will print this, but think if someone writes his age as minus 78 then also he is saying the same, I want if someone gives negative age then he will get a heads up and the program will tell him that what happened to you, why are you writing negative age, negative age does not happen
means have you ever seen minus 8 years old, now don't write in the comment that I have seen, you must not have seen now you see here that if I want to give a head up to my user, it sounds weird but I want to put that why are you putting negative age in my program, why are you playing with my program
then I will write elif, I will say elif a is less than 0 means if a is negative and I will put colon I will hit enter and I will write here that you are entering an invalid age and now let's understand this program, this if, elif and else are connected to each other
first thing you have to understand that if you wrote if then elif and then else then these three are connected to each other and these three work together, how do they work together first the condition of if will be checked, once the condition of if is checked then what will happen
if it is true then this will be printed and these two will be ignored but if this condition is not true listen carefully, if the condition of if is not true then what will happen that then your this will not work because the condition in your if is not true then it will check the condition of the next elif
if this is also not true then after this if there is any elif then it will check the condition of that suppose I want to put an elif here and I want that if a equals to equals to 0 means if a is equal to 0 you are entering 0 which is not a valid age, so here I want that if my condition is true then this will be printed
otherwise if someone put a negative age then I will tell him that don't put invalid negative age I wrote invalid negative age then if someone put 0 then I want that you are entering 0 which is a valid age and after that if any condition is not matched then this will work, so these four work together
so this is called if elif else ladder, I will write here if elif else ladder so we will do one thing here, we will make a program called 02 underscore if elif else ladder dot py and I want to keep it readable, if elif else ladder and I want that once we finish this course
then you will have everything properly and in conditionals I want to put only if or else here I want to put only if or else, this is our ladder, this is called if else and this is called if elif else ladder this is our if else, if else statement, I want to document things properly so that once we finish this course
then you will have a set of programs like a resource, int input enter your age, once your age enters let me run this program and show you and then I will tell you how it is running so here enter your age is asked, suppose I put 78, as soon as I put 78 then the condition will match
that is, the condition inside if will match, 78 is greater than 18 and because it is greater than 18 so these two lines inside it will be executed and because they will be executed so this elif, this elif and this else will be ignored, very good, now because this is executed
and this is ignored so after running this the program will come to the end of program because this print end of program is not inside any if, elif or else, look carefully this line is inside if because there is space after if, this line is inside elif because there is space
I will call it indent, not space, I will call it indent, its name is indent elif is written here, this line is inside this elif because indent is written here indent means this space, this line is inside else because indent is written here
very good, we understood, this line is outside because indent is not written here this line is stuck here, now you see here that you entered age, if condition matched the masala inside if was executed which was two lines, both the lines inside if were executed
and after this end of program came, what came? end of program, very good, it was fun now what I will do, I will run this program again and I will put minus 78 now when I put minus 78, this condition did not match because minus 78 is greater than 18
and it is not equal here, so it will check this condition in elif this condition is true because minus 78 is less than zero so this condition is met here and because this condition is met
that's why this is printed and once if, elif, elif, elif, all the elifs and else if any one condition is matched, then the rest is ignored so because this condition is matched, the rest of the elif, else will be ignored
and our control will come here directly and you see here that you are entering an invalid negative age, end of program, ok, end of program came here now you see that I will put zero and show you here and if I put zero here
then you see here it is saying you are entering zero which is not a valid age that why are you putting zero, ok, so what happened, this condition here it did not try to match, this condition did not try to match
this one matched because it was zero, that's why it printed this and directly came out of this ladder and printed this, ok so only one thing will be executed from the ladder, ok
now see here, I will run it and show you and if I put some number here and which is 4, ok, so this condition did not match, this condition did not match this condition did not match, you are below the age of concern came
end of program came, I hope you understood this thing let's come to the notes, I have written exactly the same thing in the notes that if else and elif statements are a multi-way decision taken by our program
due to certain conditions in our code, ok, so this is our code I have told you that what happens if there is an elif then here I have given a code example of if else
now you have to do a quick quiz write a program to print yes when the age entered by the user is greater than or equal to 18, what you have to do is
that you have to write a program to print yes when the age entered by the user is greater than or equal to 18 very simple program, in fact, I have written a similar program here
so you may not need to write this program because I have already written it, it will be copy-pasted but write a program which will print yes if the age entered by the user is greater than or equal to 18
otherwise you can print no, ok so you have to do this, ok do this and tell me in the comment section by writing a timestamp
that I have finished the quick quiz I want you to tell me in the comment section below that I have finished the quick quiz now let's talk about relational operators
what are these relational operators relational operators are used to evaluate conditions inside the if statement some examples are equals, greater than equals or less than equals
I have already told you about relational operators that equal to, equal to, greater than equal to these are also called comparison operators
comparison operators and relational operators are the same thing so you should know this, I have already told you we have also talked about logical operators
and we have discussed about AND, OR, NOT we said that AND is true when both are true means if both are true then AND will return true
means if I do true AND true then it will return true otherwise it will return false true AND false will also be false, false AND true will also be false
false AND false will also be false but in OR there is no such case, OR needs one operand true to be true means true OR false will also be true
false OR true will also be true true OR true will also be true but false OR false will be false and what does NOT do? it inverts
true to false and false to true, we have talked about these three now I have explained you about the Elif Clause that how many conditions you can have
you can check one by one so I have already told you about the Elif Clause and Elif is not necessary for you to write
you can write only if else means you can write such a program where there is only an if and an else and you can also write such a program where there is an if
and an else I will give you one example which confuses a lot of people and after that we will wrap up this chapter
that example is of 03 underscore multiple if statements
example of multiple if statements I will write here if age is greater than 45 and I am going to write the same program
I am going to write the same program here and then I will explain it to you and here if age is greater than
we wrote 18 here we wrote if we wrote Elif here we wrote Elif let's say I write something like this
I put two if here I write here if a percent 2 is equal to equal to 0 means if a is divided by 2 and the remainder is 0
then print a is even and after that I did this so how will this be executed
I will explain this to you the first thing you should do is which if, which else and which Elif
determine this first if you see this program then this if after this there is no else and after this there is no Elif
so I will name this if statement 1 if statement no.1
and I will name this if statement no.2 so if statement no.1
and if statement no.2 so if statement no.1 is this and if statement no.2 is this and you can see here that this will run independently
and this will run independently I hope you understood this a is greater than or equal to 18
these three will run independently and I will do one thing I will write here end of
if statement no.1 and I will write here end of if statement no.2 here I wrote end of if statement no.1
and I will write here end of if statement no.2 so what happened here is this statement will run independently and this statement will run independently
so I hope you understood this if there were more if else statements the first thing we would have to do is
how many if statements will work together we will segment them basically if I enter 18
or if I enter 28 so what will happen in 28 first this statement will run
and this will say a is even and after that this whole ladder will run in this what will happen a is greater than 18
this will run, this will ignore, this will ignore and then end of program will run you can see this so you will have to understand these complex if else statements
you can run this program again because you will understand only if you run it again there could have been more multiple if statements
if I would have put one more if statement after this then it would have run independently if I would have put one if statement between these two
then it would have got that if statement so you have to determine which if else will work together
because some if statements are independent like here if is independent if doesn't need else, if can be alone
else can't be alone but if I just write else here, else print see here it is showing me an error
suppose if I do like this, then it is saying whose else is this? whose else is this? it won't work if I run this it is saying this is an invalid syntax, whose else is this?
so else can't be alone elif can't be alone elif can't be alone
if I do elif a greater than 5 then elif can't be alone ok
so I hope you understood this thing so here our if statement is working with this if statement
and after this this end of program is working on its own so I hope you understood this thing
so I will come back to my notes and you can see here
there can be any number of elif statements e will be small here in elif e is small
and the python programming is case sensitive last else is executed only if all the conditions inside elif fail
means the last else is executed only if in this case, if I show you
this else will be executed only if this, this and these three conditions can't be executed
ok, I hope this thing is clear to you and this ends our chapter no. 6 we are moving ahead at a very good pace
I am really very happy about how good things we are doing it is fun here
I am getting enthusiasm I will meet you in chapter 6 practice set music
so guys for chapter 6 practice set I will make a new folder I will close this
and here for chapter 6 practice set I will make a new folder chapter 6
ps and here what we are going to do we will do the first question of chapter 6
so I will open this in vscode by right clicking and I will solve the first problem which is write a program to find
the greatest of four numbers entered by the user so what we will do here
we will solve this question very easily problem 1.py and here we have four numbers
so I will say a is equal to int input and what I will do here
I will write enter number 1 and here I will
toggle screen cast mode and show you how I am duplicating
and I will do one thing a1, a2, a3, a4 and I will do 2, 3 and 4
we have four numbers a1, a2, a3, a4 we have to find which one is the biggest
so I will put an if statement and I will say if a1 is greater than a2
and I will write and and a1 is greater than a3
and I will write a1 is greater than a4
which is the greatest number I will print number is
a1 and I will print a1 so I will say
a1 is the greatest number but if this thing is with a2 means
a2 is a2 is greater than a1
a3 and a4 so what we will do we will print a2
so our a2 will be the greatest number but if our if and I will write
because I have to do one thing I have to print a2 so if a1 is greater than a2
then it will be a3 a1, a2 and from a4
then our a3 will be the greatest otherwise our a4 will be the greatest
I want to show you one thing even if you put only LFLF then also it is a valid program
so if I put a4 here and a4 is greater than a3 means a4 is greater than
a1, a2 and a3 then a4 will be the greatest so let's test this program
and then you will understand how I wrote it run it and after running suppose I took one
two, three and four and greatest number is a4 if I run it again
and if I enter more numbers even if I enter negative numbers and if I do this
then it is telling me greatest number is a3 2, 3, 4, 3 so you can see it has told me which number is the greatest
and which is not let's understand it again first of all I took a1, a2, a3 and a4
as an input from the user using input function because I know that output of input function is a string
that's why I type cast it from string to int all four numbers are in integer form
then I saw that if a1 is greater than all numbers then a1 is the greatest
if a2 is greater than all numbers a1, a3 and a4 then a2 is the greatest
if a3 is greater than all numbers then a3 is the greatest if a4 is greater than all numbers
then a4 is the greatest simple as that so our problem no.1 is solved
let's move to the second problem passed or failed if it requires total 40% and at least
33% in each subject to pass and we have to assume three subjects and user will tell the marks
so what is that we have to take input of student's marks in three subjects
and after taking that input we have to see that all subjects should have 33% i.e. in every subject
33% should be there out of 100 and if we calculate total percentage
if you don't know how to calculate percentage see I will tell you very easily very easily
because my purpose here is to explain python to you and if small maths are coming in between
then explaining that is also my work so what I will do is marks1
int of input this int of input you can make a knot
you can make a knot that you have to put int whenever you have to take a number enter marks1
and same thing we will do with marks2 and 3 so here I will put marks2
I am using multi cursor functionality of VS code I will press alt wherever I will click, cursor will come
see I have done here, now if I will do 3 I can do 3 on both sides, I could have put 50 cursors very good
now marks1, marks2, marks3 have come to us, first of all what we will do check for total percentage
which should be 40% so what we will do, this is marks out of 100 we are assuming this is marks out of 100
so what we will do we will say total underscore percentage is equal to
always name of big variable which is descriptive, you should use that I will not keep name of this variable as T
I will keep total underscore percentage and whenever we talk about our complex programs there we always
name of our variables even if they are big, but we have to keep descriptive name of our variables
descriptive means after reading it, we can understand total percentage
so you should know this now what we will do, marks1 plus marks2
plus marks3 now I will show you a trick if I will write m3
so it will automatically suggest marks3 here VS code is telling me that I have suggested marks3 because
you have written m3 and marks3 m and 3 are matching see m and 3 are highlighted
no one tells you this, I am explaining you with nitty gritty if you want to write marks3, write m3
if you have a big variable, total percentage write tp and enter it, it will automatically suggest I will show you, let's say I have
total percentage, I will write tp and enter it so write name of big variable and use ID like VS code
it will definitely help you now total percentage will be divided by 300 see all my marks
I have summed them see total marks 300 out of 300
you understand this if you have 300 marks so individually if your marks1, marks2
and marks3 are coming, let's say 70 so all three will be summed and divided by 300 now I will say
if total percentage is greater than equal to 40
I will write print you are pass ok
otherwise I will write else I will print
you failed try again next year, ok
this is harsh try again next year it feels weird
total percentage should be greater than equal to 40 only then you are pass or else you are not but
we don't have to see only this we have to see individual subjects too let's say
one student got 40 marks then 41 marks then 23 marks
you failed, try again if I run this program again 66, 78, 21 marks
still he failed because 40 marks is not much
but if I do here 100 again and 33 or 31
still he failed let me check 230 divided by 300
ok I have to do this into 100 I have to do this into 100 I have to do this into 100
so I will say total percentage is 100 multiply
100 multiply by this divided by
300, ok I have put brackets so nicely I don't need this
I said multiply 100 by its sum and then divide by 300 you will get percentage
total sum divided by 300 you have to do 300 into 100 for percentage now let's do
21, 100 you are passed but
if someone got 21, 33 and 21 then he will fail because percentage will be bad
so let me do one thing I will write his percentage so let's do one thing
let's write tp here and here let's write his tp here
so that we can know total percentage so see here I wrote 234 wrong
it was 100 but let's run it again this is 2% marks
who is this useless kid who will get 2 out of 100, 4 out of 100 and 1 out of 100 but I know people like this
I won't take their name because what is the use of taking their name
let's implement this thing again at least 33% should be there in all subjects
so what we will do here, total percentage should be greater than equal to 40 and marks 1
should be greater than 33 and marks 2 should be greater than 33
and marks 3 should be greater than 33 ok should I put equality
what is the meaning of this should I put equality it will be passed even if it is 33
33 should be there in all subjects and total should be 40
total percentage should be greater than 40 and individual marks should be
greater than 33 in all subjects or else you will be failed now see here
21 marks in one subject but in other two he got 100 even after getting 73%
why he failed he failed because he didn't have 33 in all subjects
but there is one more person who got 34 34 and 100 marks he passed
because his total was 56% and he maintained more than 33 ok, so if I come to the program
let me tell you this from programming aspect first we input marks 1, marks 2, marks 3
as an integer then we get the total percentage which is sum of the marks divided by 300
and multiplied by 100 ok, so I intentionally wrote this formula
I could have removed 2, 0 and 100 but I want you to understand the mathematical aspect then we said
total percentage should be greater than or equal to 40 and individually in all subjects marks should be greater than or equal to 33
then you pass or else you will fail we showed the total percentage
I hope this question is clear and you understood how we used it now the question number 3
is very close to my heart because in this I will tell you about IN keyword IN keyword
and don't say that I know these comments I didn't tell you in chapter 6, why are you telling me in practice set
when I am making you practice set chapter 6, there are learnings in that too after that you will be able to do all the questions related to this, so I will tell you about
IN keyword a spam comment is defined as a text containing following keywords
if this word is used in any comment, then it will be considered as a spam it happens that people say come on telegram, I also get comments on youtube
a lot of comments say hi code with harry 1 code with harry 77
and replies to people in the comment brother, I want to give you macbook pro on telegram
what is this I want to give ferrari, I don't have ferrari how will I give it to you
some people are innocent, get stuck in it so how to detect such spam comments we have to write a program for it, so first of all
what will I do here, these phrases I will take them and I will make them
in the name of p1, p2, p3 see how I make this program and after that you will know
problem3.py after that you will know that I will change all occurrences actually this is not a double quote, this is some other
character, so by changing all occurrences I replaced it with a valid double quote and
and I will do this too change all occurrences, backspace double quote
and now what I will do I will save them in the name of p1, p2, p3 I will save them in the name of p1, p2, p3
I will save them in the name of p1, p2, p3 I will write here p1 means face1 is equal to
and here also I will p2, p3 and p3 here and p4 here
p3, p4 so I have made 4 faces here now message is equal to
input, message is a string enter your comment message is a string, you will write a comment
then I will say if p1 in message or p2 in message
or p3 in message or p4 in message and what is the meaning of this
I will do one thing, I will put brackets here just so that readability increases so I will do this
and I will do this and this and understand this, what does in keyword do
I would like to come back to RAPL whenever I have to explain something, I come to RAPL to show you let's say I have a string A is equal to Harry Bhai
is a good boy now if I write if I take a string Harry in A then it will be true
if I write Shubh in A then it will be false because the string Shubh does not come in this
so if this string comes in this then it will be true or else it will be false so you should know this
so now you tell me in the comment section will it be true or false if I do capital H-A-R-R-Y in A
I have to tell you in the comment section, I am waiting for you and please keep two way interaction you guys do comment
yes you are right, it will be false because its H is capital and it is small this string is not its substring
this string is not its substring this string is not its substring because its H is capital and its H is small
I hope that is clear, now I will bring you back to the question now I will bring you back to the question I will type colon and enter and I will write print
this is a spam this comment is a spam otherwise what will happen
else I will print and I will write comment is not a spam
so we have made a kind of spam filter here this kind of logics are written, this kind of logics are written in youtube in the back end of youtube
which one is spam and which one is not, it is written in a similar way let's run it write a comment
hey I am good, please buy now this comment is a spam if I write nice video bhai
nice video bhai, so this comment is not a spam but if I write nice video bhai click this, so this will be a spam
because in this click this comes, this is a very old implementation of spam filter, a long time back spam filters were written like this, now a days they are written through AI
but in total this kind of things can be done by using if-else statement, so I hope you understood that why we did p1 in message, p2 in message, p3 in message
and what is the benefit of doing this I will move to the next question write a program to find whether a given username contains
less than 10 characters or not, super simple question super simple question 04 underscore problem 4 dot py
and here you see what we have to do we have to take a username name is equal to input
we don't need int this time enter username if username's length
if len of username if len of username is greater than what was the question, contains less than 10 characters or not
less than 10 your username contains less than
10 characters else all is
well so if my username is harry your username contains less than 10 characters
I will make a colon space, it will look good when I type I will run it as soon as I type all is well
all is well I wrote it randomly we have to detect here that username is less than 10 characters or not
I will write it properly your username contains more than or equal to
10 characters super simple question write a program which finds out whether a given number name is present
in a list or not and in keyword list I will write here
05 underscore problem 5 dot py I have a list with l name and it has 5 names, one is harry
one is rohan, one is shubham and one is divya name is equal to
name is equal to input enter your name
if name in l print your name is in the list
else print print
your name is not in the list your name is not in the list let's run this program
if I write harry with capital H then it will show me your name is in the list if I write here roshni
then roshni is not in the list if I write here shub if I write shubham with capital S
then your name is in the list if I write divya in small letters then your name is not in the list
divya in lower case is not in the list if I decapital divya then it will show me divya is in the list
your name is in the list this was a simple question we can use in keyword with list
we can use in keyword with string now you know about in keyword write a program to calculate the grade of a student
from the following scheme from 90 to 100 marks from 80 to 90 marks
from 70 to 80 marks in IIT kharagpur this is a mark
this is a A we can do like this this is a mark
finally we will make problem number 6 06 underscore problem6.py
what we will do marks is equal to enter your marks
then what we will do if marks is less than equal to 100 less than equal to 100
and marks is greater than equal to 90 greater than equal to 90 then what we will do
great is equal to ex then what we will do and we will make great is equal to empty
and we will make this also empty and see what i am doing how many times i have to replicate
5 times i have to replicate so i will select this and like this 1,2,3,4 total 6 times i have to replicate
10,9,8,7 7,0,6 6,5
8,7 6,5 5
less than equal to 50 less than less than
less than great is equal to great is
great is greater than greater than
greater than greater than greater than
great is great is your greatest
or your greatest your
your your your
your your your
your your your
your your your
your your your
your your your
your your your
your your your
your your your
your your your
your your your
your your your
your your your
lower lower lower
lower lower Harry
Harry Harry Harry
through through through
through Harry Harry
Harry Harry Harry
Harry Harry Harry
Harry Harry Harry
Harry Harry Harry
Harry Harry Harry
Harry Harry Harry
Harry Harry Harry
Harry Harry Harry
Harry Harry Harry
Harry harry harry
harry harry harry
harry harry harry
harry harry harry
harry harry harry
harry harry harry
harry harry harry
harry harry harry
harry harry harry
harry harry By the way, keep in mind, we used to write 1, 6, so it used to be 1, 2, 5
So here if I print and write i, then what will happen, the value of i will go from 1 to 5 And what does this range function do, see 1, 2, 3, 4, 5, it printed exactly the same thing Now come back to the PDF
Sometimes we want to repeat a set of statements in our program Like we have to print numbers from 1 to 1000 Let's make it easy for the loops programmer to do this
We have two types of loops, one is a while loop and the other is a for loop The loop I just showed you was a for loop, we will talk about it in detail But now I just wanted to give you an introduction to it
So let's start without wasting time with while loops So while loops.py is a .py extension, a python file Okay, now we will come back to our PDF
What is a while loop? Okay, while loop and for loop are two different types of loops, we talked about this So are they different, which one is better?
See, both are two different ways of doing the same thing Which one to use when, I will teach you all this in this chapter So first of all, learn the while loop
Assume I have a variable i, its value is 1 Okay, its value is 1, listen carefully I will write while, I will write condition in it
Assume I write i is less than 6, I will put colon and hit enter, see indent Means I have entered in the while loop And now if I write print i here, what will happen?
i's value is 1, condition will be checked, is 1 less than 6? Yes So while loop is executed only when condition is true What is the condition in this case? 1 is less than 6 is true
So it will come in the while loop Print i whose value is 1 And after this I will write i plus equals to 1
Means what should I do to i's value? I should make i's value 2 So i's value was 1 earlier, control came here
Control means our program Checked, 1 less than 6 was true Printed, 1 will be printed in the console
This will happen, i's value is now 2 While because there is a loop, so this control goes back and checks the condition Listen to this carefully
Once this is executed with 1 value, then here i's value will be 2 It will come back, will check the condition Then will run this code, then will come back, will check the condition
Then will run this, then will come back, will check the condition And then will run this, and this will continue till condition is true If condition is always true, then this will become an infinite while loop
Which we don't need in ideal scenarios But you guys see here, what will be the output of this program I will do one thing here, I will write output here
Let's make a multi-line comment, in which I am telling you what will be the output To explain, I will use triple single quote And let's toggle the screen cast mode here
So that when I do such operations, you don't ask how I am doing it See directly which key is pressing below, okay? Now you see here, when i's value was 1, then this condition was true
Print i, means 1 was printed, i's value is 2 now Is 2 less than 6? Yes, so 2 will also be printed So this will be printed, then i's value will be 3
Is 3 less than 6? Yes, print i will be 3 Then after this, i's value will be 4, then this condition will be checked Is 4 less than 6? Yes, so 4's value will be printed
Then after this, i's value will be 5 Is 5 less than 6? Yes, 5's value will be printed Now i's value will be 6, is 6 less than 6?
No, it is not, that's why this loop will not be executed on 6 less than 6 This loop will exit, means it will come out of the control loop And the execution of this loop will end, and our output will come 1, 2, 3, 4, 5
So the work I did with for loop, the same work is done with while loop So what will the while loop do, it will check the condition once, will execute all the masala inside Will check the condition again, if it is true, it will check again
Everything will be executed again, will check the condition again and so on Until the condition is false One important thing in while loop is that you have to make the condition false at some point
Otherwise your while loop will never exit, means whatever you are checking in the condition You have to alter that in the while loop, like in this case, we changed the i's value And eventually our i's value became less than 6, means greater than or equal to 6
Less than 6 became false, basically I will say that our condition became false eventually Eventually our while condition became false, because of which our while loop exited I hope that is clear now, I hope you understood
Now come back to the notes, I have written it very lovingly for you This block will keep on executing until the condition is true So I just want to tell you about the while loop
In while loop, the condition is checked first, if it evaluates to true, the body of the loop is executed Otherwise not, if the loop is entered, the process of condition check and execution What is happening here, condition is being checked, then it is being executed
Then condition is being checked, execution is happening, then condition is being checked Execution is happening, then condition is being checked, execution is happening and so on It keeps on continuing until the condition becomes false
Quick quiz for you, write a program to print 1 to 50 using a while loop Which I have already done So what we will do here, we will do 51
So if we do 51, then our values from 1 to 50 will be printed It is simple, because when i's value will be 51, then nothing will be printed i's value will be 50, then only it will be printed
So this loop will keep on running First i's value will be 1, then 2, 3, 4, all the way till when value will be 50, then also condition will be true And when 50 will be 1, then also the condition will be false
So I hope this thing is clear to you and you understood Now I want you to solve the quick quiz by yourself Here we have given an example that we have to print Harry 5 times
So what we did, we made i's value 0, while i's value less than 5, inside we wrote print Harry We wrote i is equal to i plus 1, which is same as writing i plus equals to 1 You can write this or i is equal to i plus 1, it is the same thing
I will also write here for you, so that you keep on learning You can also write i is equal to i plus 1 You can also write this
Now here is another quick quiz, write a program to print the content of a list using while loops How will you do content of a list using while loops? Come, I will show you
So what I will do here is, 03 underscore list using while dot py So I have a list in which there are some numbers There is 1, there is Harry, there is false, then there is this
Then there is Rohan, then there is Shubham, then there is Shubhi Now what I want is to print this from while loop So I will do i is equal to 0, while i is less than length of l
And plus 1 Why did I do plus 1, I will tell you now Now what I will do is, I will write print and I will write li
And I will do this and as soon as I run it, you will see And i is equal to i plus 1 is very important here If I did not write that, then it will go to infinite loop
So you see here, what happened, list index came out of range In fact, I should have done length of l Because it will start from 0 and go to length of l minus 1
So my bad, I should have done it like this And you see here, it has printed So it has printed, you can see here
Harry, false, this, Rohan, Shubham, Shubhi And it must have also printed 1, I will run it again and show you You can see, 1 came, then Harry came, false, this, Rohan, Shubham, Shubhi
It has printed all these values Why did it print all the values? Let's understand step by step
First of all, value of i was 0 So value of i was 0, i is less than length of l What is the length of this list? 1, 2, 3, 4, 5, 6, 7
This list length is 7, smaller than 7 is 0 So here it will print l0 Then value of i will become 1, then it will print l1
Then l2, then l3, then l4, then l5, then l6 Then when value of i will become 7, 7 less than 7 is false And our while loop will exit
Because of which this while loop will exit And we will finally get the content of this list I hope that was easy, I hope you understood how
We will print the content of a list using while loops Let's talk about for loops, it is the brother of while loop I say while loop's brother because
Whatever can be done with while loop can also be done with for loop So let's do 04 underscore underscore four underscore loops.py And it is very similar
We will write for i in range And whatever number we will write in range, suppose we wrote 4 So it will automatically make it 0, 4
So if we will do i in range 4, then it will go from 0 to 3 So I will show you by printing i And I will show you what happens, it printed 0, 1, 2, 3
So basically what does range function do? It generates numbers from 0 to n-1 on the fly So if you will do for i in range
So what does range 4 mean? In range 4, there will be 0, 1, 2 and 3 So what will happen?
It goes from 0 to n-1 So if I show you the range function So range function in python is used to generate a sequence of numbers
We can also specify the start, stop and step size Like we already saw Where did we see?
We saw in string slicing So you should explore this step size What is step size?
But I would like to show you quickly in rappel About step size So if I will write python here
And I will write for i in range And I will write 0 to 100 And I will write step of 4
And I will write shift enter And I will press tab And I will write print i
And I will press enter So see what it did, it started from 0 Then it made it 4, then it made it 8
So if I do step size 4 So what will happen? First of all, take numbers from 0 to 100
Then start from 0, 1, 2, 3, 4 Like I told you earlier Like we saw in string slicing
You have to jump So I will tell you the meaning of jumping So I will make a new file
Suppose I have 0, 1, 2, 3, 4 5, 6, 7, 8, 9 So first of all, 0 colon 100
All the way till 100 Write 0, 1, 2, 3, 4 Write 4, 1, 2, 3, 4
Write 8, exactly the same 0, 4, 8, 12 I have already told you in string slicing
That's why I am closing it But I hope you have seen the string slicing chapter In which I told you about step size
So you can give step size in range function Which is very good So for loop is used to iterate
Through a sequence like list, tuple or string You can put for loop in string You can put it in list
You can put it in tuple Now I will show you how easily You can iterate a list
For loop Iterate.py Iterate.py
So suppose I have a list Which has some numbers Like 1, 4, 6, 2, 3, 4
6 again And 7, 6, 4 If I write for i in l
And print i Then all the elements of this list Will be printed one by one
1, 4, 6, 2, 3, 4 6, 7, 6, 4 What is happening is that all the numbers
Are being printed one by one If I put i in l Now if I do the same thing
With tuple If I write t is equal to Suppose I make a tuple
If I start with 6, 2, 3, 1 75, 1, 2, 2 And if I do for i in t
And print i Then you will see that All the elements will be printed one by one
6, 2, 3, 1, 75, 1, 2, 2 And this output is of the top one So if I just comment out
The top one Then you can see 6, 2, 3, 1, 75, 1, 2, 2
And these four are In this tuple So in this tuple there are 6, 2, 3, 1, 75, 1, 2, 2
And if I do for i in t And print i Then all the elements will be printed one by one
I hope I was able to explain you properly That how you can iterate I have already written in the notes
That you can iterate tuple, list, even string So I will give you an example Of string
And I will write for i in s And print i And I will comment out this
And you can see H, A, R, R, Y Is written here
So characters will come one by one H, A, R, R And Y will come as values of i
So for i in s H, A, R, R And Y
And we are basically iterating The string So I will do one thing
I will give you some good headings So as you can see This is for loops with tuples
This is for loops with list And this is for loops with strings I hope you understood this
So this example is for list, tuple And string Now I have already told you
About range function Explore step size of range function I have already told you step size
Now I have given an example of range function Which I have already given you For i in range 0 to 7
So it will go from 0 to 6 From 0 to 6 Now here is a very important concept
Which is taught very less And very less people know And you can become different by knowing this
That is for loop with else With for loop An else statement can be used
An optional else can be used Which What it will do
I will show you So basically we have a list I said for item in l print item
So what will happen When it will exit for loop Then it will execute else
So with this example I explain you for loop with else So basically what happens
The loop will continue Till it will get values And once it stops getting values
Then what it will do Then it will execute else I will indent it properly
it is not indented properly so I will indent it properly and you see what happened here
my list was 178 for item in L and I did print item
then I did else, then I did print done this is printed when the loop exhausts okay
so see what will happen that I did for item in L and did print item so first one print will be done, then 7 print will be done, then 8 print will be done and finally when there is no value left to print it
then what will it do? it will print this print done
okay so this thing will be printed so if I run this program and show you
what will happen first? one print, 7 print, 8 print finally it was trying to print next value
so it couldn't because for loop was exhausted and that's why it
entered in else and printed done so when do we use for loop with else? for loop control will go inside else
when it will exhaust okay when it will exhaust
I hope you understood this thing so for loop with else is asked a lot in interview is it used in programs?
I will say that if I talk about practical programs then for loop with else is used very less but yes, interviewer can ask you to trap you
that how to use for loop with else? tell me this so that you get trapped and you get trapped
and after that he can make you turn so all these things interviewer can do with you okay
now we will talk about break, continue and pass statements okay
what is break? break is used to come out of the loop when encountered it exits abruptly
okay abruptly exits this program like suppose we have a for loop
which is printing from 0 to 80 okay I will quickly tell you break, continue and pass
I will do one thing break and continue.py suppose I have a function
for i in range 100 I have done print i here so you will say yes, it will print from 0 to 99
what to do then? I will tell you what is break if I say that for i in range 100
you keep printing values one by one but if value of i is 34 then you break from here
means print from 0 to 33 I got value of i as 34 similarly run this break
what is the meaning of break? exit the loop right now forget everything and exit the loop
what is the meaning of break? forget everything and exit the loop so if I run this
you will see here that it printed from 0 to 33 because break means forget everything
and exit the loop forget everything okay
so I hope you understood what is break we wrote i in range 100 i equals to equals to 34
break means exit the loop right now okay there is a continue also
continue is also very similar but understand the difference in both break means exit this loop
and forget this loop but continue means forget this iteration skip this iteration
it means skip this iteration what is the meaning of iteration? value of i is 0
that is an iteration value of i is 1 that is an iteration
value of i is 2, 3, 4, 5, etc up to 99 that is an iteration
okay so here from 0 to 99 every value is an iteration
so what will happen here if I am saying if value of i is 34
then continue so what will it do? it will say value of i is 0
this is false so it will print i value of i is 1
this is false it will print i value of i is 3, 4, 5, 6, 7, 8, 9
it will go all the way till 33 so what will it do?
it will print till 33 as soon as value of i is 34 it will see continue
so it will not execute the code below it will start running for the next value continue means
skip that particular iteration that means in this loop
this iteration the part that was executed till now is done
but it should not be below now the value what should you do?
for the next value you go what should you do?
for the next value you go okay
so what will happen here? continue means for the next value
you go so what will it do? value of i was 34
this will not run because we have continued in 34 it will run for 35
okay so I will show you it will skip 34
so if you see it closely 31, 32, 33 skipped 34
then 35 so as soon as continue is seen it skips that particular iteration
but break is seen it exits the loop break means
from now on no value will run this means the story of the loop is over
the break statement kills the loop but what does continue do? continue says
for this value for the value below whatever is left
that don't execute execute the next iteration
like i was 34 whatever is below that don't execute continue
work for 35 okay then i was 35
this will be false then 36 this will be false
so if continue is seen then that iteration skips if break is seen
then the loop skips okay we will ask questions
about break and continue you will understand so
if I come back to the notes then see continue is used to stop
the current iteration of the loop and continue with the next one it instructs the program
to skip this iteration continue means skip this iteration
and what does break mean? exit the loop now it instructs the program
to exit the loop now and what is the instruction here? skip this iteration
okay I told you from 0 to 80
if we break at the value of 3 then what will happen?
0, 1, 2, 3 will be printed but as soon as this break is seen it will exit from this loop
what will happen here? if the value of i is 2 then the iteration will skip
2 3 and 4 will run here
okay so I request you that you guys
run it in your computers don't look at it like a movie
I mean seriously don't look at it like a movie okay
let's talk about pass sometimes what we do that sometimes we simply
don't want to do anything in the for loop so let's say
I want that this is my for loop let me show you
0, 8, underscore pass.py let's say I say
for i in range 645 colon
now I want to do this later let me write another while loop first
while i is less than 45 let's say I want to do this later too or I do it right now
print i and then after this i
plus equals to 1 let me do it here plus equals to 1
let me do it here i is equal to 0 let me do it here
now I will think that this is not working and this will work
but as soon as I run I am getting an error indentation error
he is saying how is your indentation? what are you doing?
here you see that you left half incomplete complete the for loop
but I say that I will do it later I want to leave it now
and do this I want to see its output I want to leave this
so for this python has given a convenience keyword
whose name is pass python says write pass
it means that you will work here later run
see it worked from 0 to 44 the output of our while loop
has come okay so we write pass
so if I tell you what is pass? pass is a null statement in python
it instructs to do nothing if you don't write pass then there will be an error
but if you write pass then it means that in for loop
don't do anything here so this whole statement will be skipped
okay without pass the program will throw an error
okay now we will see some questions because I have understood
the loops very well and what a great codes are being made here
I am really happy with myself such a great explanation is coming out of things
I enjoyed it I am enjoying it and I am very excited
that how much you will enjoy when this video will be published and tell me in the comment section
by writing I like it from the heart when you give feedback
I will meet you in the practice set of chapter 7 practice set of chapter 7
so guys now we will move towards the practice set of chapter 7
I have made a folder in the name of chapter 7 ps and I will right click here
first I will close this folder then right click and open this folder in vs code
the first question is of practice set 7 write a program to print
multiplication table of a given number using for loop it is a very simple problem
first of all I will make a file and after this I will write
let n is equal to input and I will write
enter a number colon I will space it
and then what I will do I will write for i in range I will go from 1 to 11
then I will get from 1 to 10 because it goes from this number to n-1
so I will write print and I will use an f string I have already told you
what is f string what is f string here basically you put an f here
and write a normal string and now you can use variables in this string
so you can write here n and after this we will write multiply by
and we will write here i is equal to and we will write here
n multiply by i ok
so whatever will be its value it will come so let's see it
so this number is asking me suppose I want to write 7 so see
it gave me a table of 7 but again I made the same mistake
about which we were talking in this whole course that
whenever we are talking about integer then you have to do int input it is your duty
you have to do it if I will write 7 here then the right table of 7
will be print ok so you have seen here
that 7 into 535 it will be correct it is python
it is not you and me who will make a mistake ok
great so this is our problem number 1 that is done
let's see problem 2 what is it saying write a program to greet
all the persons names stored in a list l
which starts with s now it is saying
basically this is your list ok it is being told to you
that there is a list here you just have to greet it whose name
starts with s so what I will do here that
I will write for name in l and I will write if name.starts with capital s
print hello name and I have to make it f string
and I will run it so what will happen that here
hello Soham hello Sachin came Harry and Rahul
started with h and r that's why their name didn't come now how did I make it here
I said for all names in this list for name in l
one by one the value of name will be Harry, Soham, Sachin and Rahul I said if name starts with s
if name.starts with s then what you do print hello and name
means if name starts with s then you greet it otherwise you don't greet it
so here you see that Soham Sachin was greeted Harry and Rahul were not greeted
ok so our question number 2 is also done so question number 3 is saying that do problem 1 with while loop
means the table we wrote we have to do this with while loop so I will do problem number 3
here from while loop I will do problem number 1
in problem number 3 so first of all I will write this code as it is then what will I do
I will say i is equal to 1 and I will write while i is less than 11
and what I will simply do here here I will do i plus equals to 1 so what will happen
i's value will start from 1 and finally what will happen that our table will be printed
so basically this is a very basic conversion from for loop to while loop ok
I will write 8 8's table will come what did I do
I said look n is equal to take a value from the user
which number table do you want to write then do i is equal to 1 do i is equal to 1 and start
then after this while i is less than 11 while i is less than 11 means it will go from 1 to 10
you print the table and in every iteration of the loop keep incrementing the value of i
ok keep incrementing the value of i in every iteration of the loop
ok so this thing is very important great
I think amazing good stuff
ok come back and here what we will do
that write a program to print the following star pattern but before that write a program to find whether a given
number is prime or not so what happens that any number
to declare a prime number what do we have to do I write here
04 underscore problem 4.py so any number is a prime number when either it is being divided by 1
or that number is dividing itself so to check this thing we will first take the number
as an input from the user so here n is equal to int enter a number
then we will say for i in range 1 to n and what we will do
we will write if i percent and we will write
sorry if n percent i is equal to equal to 0 means it is divided by any number
so we will write here print and write number is not prime
ok and I will not include 1 here I will start from 2
and here I will write only n because from 2 to n minus 1 will be checked
that this number is prime or not ok
now here I will show you one thing I will run this suppose I wrote 6
number is not prime number is not prime ok
now I want if number is not prime then I will break it
because if it is divided from 2 to n minus 1 from any number
then this number is not a prime number so I will break it by saying number is not prime means
I don't need to divide by other numbers and check ok
so I will say number is not prime so I will run this suppose I write 4 here
so number is not prime if I write 5 then 5 is a prime number
so here if I write 5 then nothing will be printed
so I will do one thing that if
this for loop is complete ok so what I will do
I will put else here and I will write print here and I will write
number is prime ok great
see what happens now if I wrote 7 here then number is prime
but if I wrote 8 here then number is not prime is 2 a prime number?
yes, 2 is a prime number so if I write 2 here then number is prime
ok great very good
so prime numbers are those in whose factors either 1 comes
or number comes so 2 is also a prime number and I will show you here
as soon as I run this I will put any number here suppose I put 565
then it is not prime because it is divided by 5 like if I put 1
then you see that 1 is 1 is shown as prime
then if I do 3 here then number is prime so this is our program to make a prime number
so what we will do, we will go to next question and this is write a program to find the sum of first and natural numbers using while loop so with the help of while loop, we have to sum these numbers
it is a very easy program how will you do? while i is less than n
sum plus equals to i and after this print sum now you will say what are you doing?
so i will tell you what i am doing and i am not looking anywhere, i am writing from here i is equal to zero
and here we will take the number as an input from the user so i will write n is equal to int of input enter the number
now i will write 6 and here i have to write sum is equal to zero sum has to be initialized from zero
i has to be initialized from zero and here i will write sum plus equals to i so i will start from 1
and here from first and natural number it will be less than equal to i and i will explain you, you don't worry
i wrote 7 so it is 28 if i write 3 then it will be 1 plus 2 plus 3 is 6 if i write 3 then it should be 6
so my program is working now i will tell you how i wrote this program first of all we took the input from the user
give the user input which number sum should be i have to start from 1
and i have to go all the way till n because i have to go from 1 to n so what i will do
i will write in condition i is less than equal to n so this loop will work for 1, 2, 3, 4
all the way till n it will work for n if i didn't write it equal to
then it will only work till n-1 suppose n is 10 if n is 10
then your loop will work from 1 to 9 if i is less than n but if i is less than equal to n
then it will work from 1 to 10 sum value is zero so we added i
so first we will add 1, 2, 3 all the way till you want so if i wrote 7
you will see the result so i hope you understood if i run it again
if i write 56 in enter number i wrote a very big number if i write 5
so 1 plus 2 plus 3 which is 6, 6 and 4 is 10 and 10 and 5 is 15
so this 15 came here easy peasy lemon squeezy now let's move to the next question
what we have to do factorial of a given number using for loop ok
now understand what factorial is you have to understand things if you want to move ahead
i am very excited to explain 1, 2, 3, 4, 5 if i have to write 5 factorial
which is written like this if i have to write 5 factorial how will i write
1 multiply by 2 multiply by 3 multiply by 4 multiply by 5 this is the definition of factorial
that you start from 1 and do multiplication till that number it is simple so how to get factorial 5
how to get factorial n first we will take the number as an input from the user
once i took the number as an input from the user i will run for loop
i will write for i in range and i will write from 1 to that number so to go to that number
what i will do i will write n plus 1 because i have to go to that number
after that i will write product is equal to
product multiply by i and i will make product as 1 i made sum as 0 and product as 1
why? because we are adding in sum so when we add, we initialize from 0
and when we multiply, we do it from 1 i have already started from 1 1 multiply by 1, 2, 3, 4
so finally what will happen we will print factorial of
and here we will write n is and here we will write
the value of product let's run this program product, so if i run this
you can see i did everything right enter the number 6
factorial of 6 is 720 factorial of 3 is 6 factorial of 3 is 6
great so here you can see how you can print factorial of any number
i will explain one more time first i took input from user initialized product from 1
then i multiplied 1 first 1, then 2, then 3, then 4 all the way till n
now i know that range goes from 1 to n-1 if i write range 1 to n
then range goes from 1 to n-1 but i have to go to n so i wrote n plus 1
you understood right so i wrote from 1 to n plus 1 so it will go from 1 to n
because n plus 1 minus 1 is n is it looking like mathematics yes, use your brain
use pen and paper from 1 to n plus 1 and you saw that
it will go from 1 to n if i write range from 1 to n plus 1 then product will come like this
so if i write 5 then it will go for 1, 2, 3, 4 so i have to write 1 to 6
if i want from 1 to 5 so i wrote n plus 1 good
we will revisit factorial when we will study recursion but for now write a program to calculate factorial
problem 7 we have to write we have to make this star pattern star pattern programs
are very useful i will say so we have to make this kind of star pattern
how will it be i have to print the following star pattern we have to make this star pattern
1 star then 3 star then 5 star let's see how it will be
so we have to make a file 07 underscore problem 7
7 dot p y and i have to make this star pattern how will it be
to make this star pattern what i have to do see here
1 space if n is equal to 3 then how many spaces i have to give
1, 2, 3 i will explain 1, 2, 3, 4, 5 stars
5 stars are here then here 3 stars 1, 2, 3
then 1 star is here so here 2 spaces
1 star then here 1 space then here 3 stars
then here 0 spaces 5 stars and this is for n is equal to 3
so when n is equal to 3 i want to put it on a multiline comment i want to put it on a multiline comment
so that i can easily explain so see what happened i will write here
for n is equal to 3 for n is equal to 3 star then triple star
then star star star star like this for n is equal to 3
so for n is equal to 3 in starting how many spaces we need 2 spaces, then 1 space
and how many stars we need in first line we need 1 in second line we need 3
so for n is equal to 3 we will start with 2 spaces what if n is equal to 4
if n is equal to 4 then how many spaces we start with we start with 3 spaces
so first we will take value of n let's try first we will take value of n
we took value of n and here we will print 2 stars so here we will say
print in first line how many stars print star and multiply by how much
here n is equal to n minus 1 n minus 1
you have to print spaces and how many stars you have to print how many stars you have to print
you have to print i what is i i will start with 1
and all the way till n so for i in range 1 to n plus 1
because i have to take from 1 to n and here we have n minus 1
printed spaces like how many spaces we printed here we have
printed 2 spaces for n is equal to 3 after this we have to print one space so i will make it n minus i
i will make it n minus i n minus i spaces will come and this i will come
and here i want to tell you one argument of print statement which is
end is equal to this by putting end is equal to this print statement by default doesn't add a new line
and after doing this what i will do i will write print
slash n and now i will run it for n is equal to 3
i will run it for n is equal to 3 so this is how we printed 1, 2, 3
whereas i wanted 3 and they wanted 5 so here i made a mistake
so i will fix this so 1 is printed properly here i had to print 3 and here 5
so the number of stars i am printing is wrong instead of printing 3 stars
i am printing 2 stars here i am printing instead of printing 5
i am printing 3 so i will make it 2n minus 1 i will make it
number of stars will be 2i minus 1 2 into i minus 1 which is odd numbers
series now you will say this is so complicated
but i will explain i will make it 3 and see our pattern is printed
how i printed this pattern i will tell you if i just write print
then also i will get a new line then it will be correct so this is our star pattern
if i run it for 5 so this is the star pattern so to explain this
i need your time and i will take that time and i kept this complex question
i don't want to make you happy by giving you a simple question so yes the question is difficult
and listen i will explain first what i did here i took an integer
n and asked user to input what i did
run for loop for n is equal to 3 we have 3 lines
so i will run from 1 to n plus 1 so it will be n lines and i asked user to input
blank space n minus i so here we have 2 spaces
so here 3 minus 3 minus 1 2 3 minus 2 1
so n minus i how i came to know this this will come from practice
when you make for n is equal to 3 you can make for n is equal to 5 let's make for n is equal to 5
if i make for n is equal to 5 for n is equal to 3 we have 3 2 5
so n is equal to 5 how to make for n is equal to 5 n is equal to 5
in the last one 1 2 3 4 5 in that we have
many stars above that there will be only one space
rest will be stars then it will come like this then it will come like this
then it will come like this so how many spaces are coming see here 1 2 3
4 spaces are coming in first one 1 2 3 spaces are coming in second one so n minus i
what is i my row number 1 2 3 4 5 so n minus
i times i have to print space and star i how many times i have to print
i times i have to print if i had to print i times here one star or two or three
it is not like that it is series series of odd numbers
which comes from 2i minus 1 if you make value of i as 1 it will come as 1
if you make value of 3 as 5 it will come as 1 3 5 7 so i have printed star as 2i minus 1
for this your math should be strong if you have to do all this if i starts from 0
then we write 2i plus 1 what happens by writing end is equal to blank see the print statement by default
gives a newline if you don't want to give a newline then you write end is equal to blank
this is the argument of print statement by putting comma so it will not give you a newline
by default ok good so we have printed these two things
but after this we had to give a newline if i would not have printed this without end is equal to blank
then newline will not come i wanted a newline once my row is printed
so i want a newline so for newline i have put an empty print i have not put print backslash end
because if i will put that then newline of backslash end will come and newline of print by default will come
so i have made print blank don't print anything and print by default adds a newline
i got that i hope you understood so this is our star pattern
yes it is difficult but i wanted to include and i will include
i will make you do difficult things 7th problem was a bit difficult i agree
but again i wanted to do i will make you do difficult things according to me
the most wrong thing i can do is that i will make you do easy things and then you will know
that things are not easy problem number 7 as i have done
problem number 8 you don't have to do so much because it is a very simple problem
what we have to do in problem number 8 simply print i times
that's it now i will write 5 and if you do 7th
then 8th will look like halwa absolutely halwa if i do 3 then it will look like this
just print i times for star Alright, what is the next question, how will the next question be, see what you have to do for n is equal to 3
see, here it will be complete, here one will be missed, and then here it will be complete, okay so I want you to try the 9th question by yourself and if you are not able to do the 9th question, then you have to see the solution
but I am making you do the solution so here we have a simple pattern, in which what we have to do is we have to print all the stars in the first and last line
and in the rest of the middle line, we have to print only the first and last stars so come on, first of all we will take input from the user and run our loop this is going to be exactly the same, so I will copy this whole thing
I will copy this whole thing, and here what I will do, I will say if i equals to equals to 1, i equals to equals to 1 or i equals to equals to n
then what you have to do, then print, print the star how many times, i times print i times, okay, print i times, not i times, print n times, okay otherwise what you have to do, else, otherwise what we have to do, we have to print star in the starting
otherwise what you have to do, print, print star, okay and after this, print one, here I will blank n is equal to and after this, we have to print spaces, how many times, n minus 2 times
see, we have to print spaces n minus 2 times, okay because 2 times will be star, and n minus 2 times will be our space so I will write here, in bracket, n minus 2
and after this, we have to print this star again, okay and after this, what we have to do, we have to print a blank so that we get a new line, okay
and what we are going to do, we will make this end is equal to blank let's see if I was able to make the program properly or not I will run it for 3 first, yes, it worked for 3
I will run it for 7, yes, it worked for 7 as well see, a whole ring has been made for 7, okay so I hope you enjoyed it and understood how to make this kind of program
so what I did, I will explain it again what I have to do, I have to make a ring like this, okay to make this ring, I need all stars in the first row and all stars in the last row
so I said, if the first and last row are there, then print star n times, okay print as many stars as n is otherwise, what you have to do, print star in the beginning, print star in the end
and how many are left, n minus 2 because in the beginning and in the end, star has been printed twice so how many are left in the middle, how many spaces are left
this is left, n minus 2, okay, how many are left, n minus 2 n minus 2 is left, actually I have given a space here for readability but here only one space will come, just so that you don't get confused
let me tell you, it will be printed like this so here n minus 2 spaces are left what did I do n minus 2 times
printed space, then printed 1 star in the end then after that one iteration was done, so I took a new line because I have to take a new line here as well
simple, simple, good the 10th question is telling us to write a program to print multiplication table of n using for loops in reverse order
it's a good question, it's a good question, it will be fun to do I am excited with simple problems, there is not as much excitement as with difficult problems
so if I have to print a straight table, then what will I do I will print something like this I will say that n multiplied by i is equal to
put one more bracket n and star and i it's a simple thing, it will be done
take n as an input from the user but we have to do it in reverse what do we have to do
there are many ways to do it either I take it in a list, reverse it and print the list but can we do better than this
yes we can I want to print from 10 first but it is being printed from 1
so what will I do here I will write 10
it is going from 1 to 10 I don't want to take it from 1 to 10 I want it from 10 to 1
it is going like this 1,2,3,4,5,6,7 I want 10 instead of 1
I want 9 instead of 2 I want 8 instead of 3 I want 7 here
I want 6 here I want 5 here I want 4 here
I want 8 here I want 3 instead of 8 I want 2 instead of 9
I want 1 instead of 10 how will it be done what is the sum of both
it is 11 if I write 11-i will my work be done
yes it will be done by writing 11-i my table will be printed in reverse
but remember you have to write 11-i and
it is good I wrote 5 I wrote 5x10
then 5x9 11-i means 1,2,3,4,i
10,9,8,7,11-i n plus 1 minus i if I talk about general formula
I hope you understood if you do IIT
you know generalization is taught a lot
but let me tell you if you are finding it difficult no problem
no problem if you understood it matters
if you are able to solve it that's it don't worry
I am able to do it you will do better in the future with that said
now we will move to chapter 8 so let's go to chapter 8 I am very excited
see you in chapter 8 so chapter 8 is our function it is very close to my heart
it is a very important topic and you will enjoy understanding it what is function?
group of statements performing a specific task this is our function when a program gets bigger
then sometimes we want to separate a particular logic from the program
and then we use function let me show you function in action so that you know
what is function and how it works so we will make a folder in the name of chapter 8
and in this folder we will shift right click and open this folder first of all I will make
01 underscore functions dot py so I have a program
and it is calculating average of 4 numbers so I have
a is equal to 12 and I have b is equal to 45 and I have
c is equal to 56 these are 3 numbers let me keep it simple
now what I am doing I am saying average is equal to
I am writing a plus b plus c divided by 3
and I am saying print average very simple program
no surprises whatsoever but think I have to do this again for some other numbers
I have done this for these 3 numbers suppose I have to do this for some other
set of numbers suppose my next number is like this
3,2,1 I have to print the average of 3,2,1 if I am doing the same thing again and again
I am printing the average it means my program is getting repeatable and let me show you
when your program will be repeatable suppose I am saying enter your number
and I am doing this for all 3 numbers I am taking all 3 numbers as an input
from the user and I am printing the average I have to do this for 50 times
suppose I am taking 3 numbers and I am printing the average suppose I have to do this
for 50 users so in my program, the number of lines will increase to solve this
we will say don't do this use functions
with functions you will give a name to this piece of logic I am saying again
you will give a name to this piece of logic let me tell you how to make this easier with functions with functions
we can separate this logic we will say def we will write the name of the function
you can give the name of the function as well we will put brackets
we will put colon and enter and we will copy this logic and put it inside this function
we will put it inside this function means we will put indent inside this function so I will select this and press tab
why I am pressing tab? because I want this to be inside the function so this line is inside the function
this line is inside the function and these two lines are also inside the function so you should know this
now see I have written a, b and c average is equal to I have printed average
now if I run this program, what will happen? nothing will happen literally nothing will happen
there is no output of this program and the reason for that is you said AVG means
do this but when to do AVG? you have to tell this
how will you tell? you will tell like this and you said run AVG
means this whole code will run you said run AVG means we write AVG
name of the function and opening closing parenthesis means run the function means run the code inside the function
so if I run it 5, 3, 2 and it gave me the average
if I want to do this 5 times I will simply replicate it 5 times 1, 2, 3, 4, 5
so I will show you I can do this 5 times I can do this 5 times
so see 5 times 1, 2, 3, 4 and 5 times it gave me this
because I called the average 5 times how cool is that? so let's reiterate the function
function is a group of statements performing a specific task when a program gets bigger in size and its complexity grows
it gets difficult for a program to keep track on which piece of code is doing what
what code is doing it becomes difficult to track so we want to separate the logic
we want to make the logic so that we can reuse it and that's why we put a particular logic inside a function
a very simple function works like this you wrote func1 which is a name
we can give a name to a function and after that you wrote print hello and as soon as you wrote print hello
as soon as you call function1 your hello will be printed means the logic inside it will be run
so this is the syntax of a function this function can be called any number of times anywhere in the program
so this is the syntax of a function means we wrote the syntax you made a function
very good, you made a function but when you have to run it to decide this
you have to give a function call function call means after writing func1
opening closing parenthesis here you are asking to run the function whenever we want to call a function
we put the name of the function followed by parenthesis as follows means wherever you wrote this
it will find the function func1 and it will run the function and it will run the function
whenever you wrote it in the flow of the program like let's say
after every average I wrote thank you after every average
I will copy this line and I want to write thank you after every average then I can do this
and I want to write thank you after first, second and third average and after that I can't write thank you
and you can see here that after first and second average I wrote thank you
after first, second and third average I wrote thank you and you can see my average ran five times
so basically in my flow I can make any function or any function, multiple functions
in a program, I can use them as many times as I want I hope this thing is clear to you
this is called function call so what is avg, this is a function call this is a function definition
very very important terms let's write this function definition
this is the definition of function this is the definition of function and this is function call
and this will be useful I am telling you that this is not a book term this will be useful when you work in the industry
you will be told to remove function call go to function definition then you don't get confused
what happens is people learn coding in the air they don't know what is definition what is call, they are told to go to function call
where is function call happening call, what is this, phone call no, you have to be aware of terms
are you getting me you should know what is what so this is function call
and this is function definition this is the whole function definition part containing the exact set of instructions
which are executed during the function call that is the function definition let's do a quick quiz
write a program to greet a user with good day using functions it is very simple
so what I will do here is I will ask his name I will make a quick quiz
with the name quickquiz.py and what I will do here
I will make a function with the name and what I will do here I will simply
follow and I will write good day
I will call good day function and I will write good day here you can print his name after that
but I solved the question it was a very simple quick quiz you wrote a program which greets a user
with good day now there are two types of functions one is built-in function
and one is user-defined function what is user-defined function user-defined function is
the function which you write yourself what is built-in function built-in function is
the function which you automatically get in python means those functions
which are already present in python without any import like print is a built-in function
so what we will do here we will explain built-in functions
First of all, the built-in function is len, which you do not import from anywhere, Python already provides it to you. Range function is there, we did not import it from anywhere, we got it, it is a built-in function. Print, we can use without importing anything, print is also a built-in function.
But func1, we made this function, defined it, then after that we called it, so this is a user-defined function. So built-in functions, which are already in Python, you do not import from anywhere. User-defined functions, they are sometimes defined by a user.
So here you see the concept of function with argument, you will also enjoy. What happens is that sometimes we want to get a particular argument for the function. What is the meaning of this?
Like if I write function with arguments.py So if I want to take the name of the user who is greeting, that is, if I write here, good day Harry, then print good day Harry.
If I write good day Rohan, then print good day Rohan. So what will I do, I will say that good day function, you do one thing, that you also take this name as a variable name, understand the concept very carefully.
So what did I do, I wrote name here, I wrote Harry here, So this Harry will go in the name variable and I can use the name here. And maybe you will understand what I am doing here.
So what did I do here, I did string concatenation, I said good day and name. So these two strings will be found and these two strings will be added and good day name will be made. Let's run it, so you see here good day Harry is written,
if I put a comma here, it will look better, good day Harry. So I wrote good day Harry here, so good day Harry came. So what is the name here, name is a parameter, name is a parameter.
We passed Harry as name, which came inside a variable. If I write here good day and I write here Rohan, then good day Rohan will be printed. If I write here good day Divya, then good day Divya will be printed here.
You see here, good day Harry, good day Rohan, good day Divya. So these are functions with arguments, it is taking an argument. Now assume that I want to take one more argument instead of day.
Assume I want to take an argument with the name ending. I want this to end in thank you. And I want to print this ending also.
So I can take one more argument with a comma. So here what will pass in name, Harry will pass. And here what will pass in ending, thank you will pass.
So when I am writing good day Harry comma thank you, then what is happening, Harry will pass in name, thank you will pass in ending. And when this function will run, good day will be here.
What is the value in this variable? Harry value, so good day Harry. Then after this, thank you will be printed because there is thank you in ending.
Let's run this now. You see good day Harry thank you, good day Rohan thank you, good day Divya thank you. Assume Divya is a little stylish, she doesn't say thank you, she says thanks.
So we will do thanks here. If we run this, good day Harry thank you, good day Rohan thank you, good day Divya thanks. So here she said thanks to South Delhi.
So if you want the South Delhi user to say thanks and the rest say thank you, then you can do that basically. So this is a function with arguments.
We have passed two arguments here, name and ending. Name came in name, what came in ending? Whatever you write in the ending, you write thank you, thank you came.
You write thanks, thanks will come. It's a very good thing. So this was the function with arguments.
A function can accept some value it can work with. So basically we gave a value to the function with which the function can work. Like we gave the name and gave the ending.
And after that what happens is that we put these values in parentheses. Something like this, like I made a function called greet here. Def greet, I took the name in this.
And then when I called it, I did greet Harry. Now what happens is that there is also a return value of the function. That is, now you see I was just running the function.
If I say here, I remove these two. If I say here a is equal to and I write good day and I print a, then good day is not giving any value.
Means if I run it here, then see none came. So what happened here? Printed good day Harry.
Printed thank you. Then it came back. What came inside a?
Nothing came. If I want something to come inside a, then I will return it from this function.
So I will say to this function that function, you take a value with you. And whatever variable you ask, give it to it.
What will I say to the function? What does return mean? Return means function, you take a value and whatever variable you ask,
give it to it. Who will get this value in this case? A will get it.
So let's say I am writing return here. And I am writing done here. Fine.
So this string will be returned. This time it will be done in a. You see here it will be done.
Fine. If you return 7 here, then 7 will come. Fine.
If you return ok here, then ok will come. So whatever value you return, it will be its value. And if you give that value to any variable,
then that value will come in that variable. So we saw the concept of arguments. These are name and ending arguments.
We saw the concept of return value. Ok is a return value, which will be assigned in a if we write a statement like this.
And when do we use this? We use this when we have to give a value. For example, in the case of average.
I am printing the average here. But if I want to take this value, When this function runs, take the output.
Means we basically run a logic. By running that logic, we got a value. If I want that value to go back,
then I will do this a is equal to. I will do a is equal to average. That value will come in a.
Whatever this function is returning. In this case, this function is not returning anything. So that value will be none.
But if I average the return, then what will happen? So that value will come in a. If I write a is equal to avg.
You must try this thing. So we saw the concept of return value. We saw the concept of arguments.
Now let's see the concept of default parameter value. What is this? We can have a value as default.
As default argument in a function. So you see what happens, what does it mean? So I do one thing on you.
I make it with the name of default argument.py. I want that man. We are doing good day.
We are doing good day. And let's take the name, let's take the ending. But ending by default will be thank you.
So if someone runs good day harry. So good day harry will run thank you. But if someone, I write the name here.
Or what do I do this time? I do it by putting an f string. Don't be confused at all.
I can do it however I want. I can do it by putting a plus, I can do it by putting a comma, I can do it by putting an f string. I can do anything.
This logic will basically run. Okay. With value harry as name.
Okay. So here you see I wrote def good day. Ending is equal to thank you.
I printed with f string good day and comma name. And you see if I run it, what is happening? Good day harry is being printed.
And after this I want to print the ending too. I want to print the ending too. So you see as soon as I run it.
Good day harry thank you. Now I did not give anything in the ending. But even after that it worked.
If I don't do it like this. Suppose I don't do it like this. And if I run it, it complains.
You have a required positional argument in good day. You are missing. But because I have written it like this.
It means I am saying this. If I supply you the ending. If I supply you the value of the ending.
Then you take it. But if I don't give. Then by default you keep this value.
Which is thank you. Okay. I hope you understood this thing.
So here if I say thanks. Then. Good day harry thanks will come.
Okay good day harry thanks came. But if I don't give this thanks. I mean I write something like this.
I write good day rohan here. Then what will happen. This default value will be used instead of thanks.
Basically I am saying. If I give value then it is fine. If I don't give then by default you use this.
I hope you understood this thing. It happens that whenever you book a seat in the railway. Then you get any worth.
But by default you get any worth. But by default you get any worth. But if you supply that I want this worth.
Then you get that worth. Okay. So here I wrote good day harry thanks.
So this value of thanks will come. But if I wrote only rohan. Then it will take thank you by default.
Because the default value of ending is thank you. It was a very simple concept. So this is our default parameter value.
Here I have given an example of greet function. Which is very similar. You can also see this.
The next concept I want to tell you. That is recursion. And the concept of recursion is very important.
And I will explain this to you with time. And you will enjoy it. So what is recursion.
I will tell you what is recursion. Recursion is a function. Which calls itself.
Now you will say why is it calling itself. One function calls itself. Because.
Its logic is connected to it. What is the meaning of this. My mind is blown.
What are you saying. I will tell you what I am saying. Now I want to take you back to factorial.
See what happens. Which is factorial. Suppose I write factorial 5.
What is factorial 5. I told you. 5 x 4 x 3 x 2 x 1.
This is what I told you. Some functions are like this. Some functions are like this.
Whose logic is recursive. That is their logic. Can be defined in their terms.
I will explain. And I will explain to you. I will explain to you.
If I say here. What will be factorial 1. Only 1.
This is the definition of factorial. I did not make this. This is the mathematical definition of factorial.
Factorial 3 is this. Factorial means. I do factorial 3 here.
Or I do factorial 2. So that you understand well. This will be factorial 4.
And this will be factorial 2. Factorial 2 will be 2 x 1. Factorial 3 will be 3 x 2 x 1.
Factorial 4 will be 4 x 3 x 2 x 1. Factorial 5 will be this. Do you understand so much.
Do you understand so much. Do you understand or not. You should know.
Now see what I am going to tell you. Now I am going to tell you. Now I am going to tell you.
If I say factorial n. Means if any number comes here. Then you can say it will be n.
Then n minus 1 will be. Then dot dot dot dot dot. This will go to 3 x 2 x 1.
Will it go or not. Will definitely go. Now if I say.
What is factorial n. That is. n into.
Factorial n minus 1. Did you understand what I said. See I can write this as factorial n minus 1.
What is factorial n. Go from that number to 1. So I can write this as factorial n minus 1.
So to get factorial of any number. I can say that that number. From its number minus 1's factorial.
Make it into. Can I say. This is a very good example.
To explain recursion. And very few people understand recursion. And sometimes some teachers skip this topic.
I am not one of those. I will teach you this. So we saw factorial from 1 to n.
And we understood this formula. That why this happens. Now once we have this formula.
We will use this formula. To get factorial of any number. Suppose I have to make a function.
Which gets factorial of n. I wrote here def. Wrote the name of this function.
Wrote n which will accept this parameter. I entered this function. Indent automatically gave me vscode.
What I will do. I will say return. Factorial of n minus 1.
n into factorial n minus 1. Return it. Ok. Very good.
But. Before that. If n's value is 1.
If value is 0. Then return 1. This is the rule.
Factorial 1 is also 1. And one more thing you need to know. Factorial 0 is also 1.
Factorial 0 is by definition 1. Factorial 0 is also 1. And factorial 1 is also 1.
So if someone gave 1 or 0. Then value will be 1. Otherwise.
This formula will break. And it will call itself. Then it will resolve the values.
Let's try this. I will do one thing. I will do n is equal to int input.
And I will write enter a number. And after this. I will print an f string.
And I will write. The factorial of this number is. What is the factorial of this number.
I will write f string here. And I will say. The factorial of n.
I have called this function. Let's see if it works. Enter a number 6.
The factorial of this number is 720. Let's see 1. What is the factorial of 1.
Let's see. What is the factorial of 2. It is 2.
What is the factorial of 3. It is 6. 6 should be 24.
It is 24. All these values are correct. We have used this formula.
We have used this formula directly in this function. And we have used recursion. Now some of you will say.
I would have used for loop. No. We have used this property.
This function can be written in its terms. Because this function can be written in its terms. We will use this property.
We will use recursion. Why will we use recursion. The code which we write in many lines of code.
We have written the same code in 4 lines of code. We have used this formula. We have used this formula.
We have used this formula. See this diagram. This is a very good diagram.
When you write factorial 5. Then 5 multiplied by factorial 4. Then it will convert.
5 multiplied by factorial 4. it will convert to factorial 4. So you wrote factorial 5, so 5 will come here factorial 5, if n value is 1 or n value is 0, then return will be 1
n value is neither 1 nor 0 n value is 5 so 5 into factorial 4 will be returned
so what will happen? 5 into factorial 4 okay? then 5 into 4 into factorial 3
so 5 came here, it will resolve factorial 4 it will resolve factorial 4 in 4 into factorial 3 it came to us
after this what will it do? it will resolve factorial 3 5 into 4 into 3 into factorial 2 came to us
then what will it do? it will resolve factorial 2 5 into 4 into 3 into 2 into factorial 1 came to us
whose value is direct 1 see we have written here that if n value is 1, then return will be 1 and finally we will get this calculation
we will get this particular calculation and finally we will get our result now here programmer needs to be extremely careful while working with recursion
to ensure that the function doesn't infinitely keep calling itself because if programmer makes a mistake, then it is possible that the recursion never ends so it is very important to put a base condition
so that finally this recursion gets resolved so here recursion makes things very easy if you remember the mathematical formula
but what happens sometimes that if you make a mistake then this recursion keeps on calling itself
and stack keeps on going ahead this is basically called stack this function keeps on running
on loop and finally your program crashes if you make a mistake
so recursion makes things easy if you know the formula but sometimes what happens
that it causes a problem now why did we use recursion in factorial? because sometimes it is a very direct way to solve a problem
like we saw in this case so we use recursion because sometimes it is the most direct way
to code an algorithm it is the most direct way to code an algorithm
I hope you understood function I hope you understood recursion I will explain you function and recursion
in this chapter's practice set so in this chapter's practice set you will get more clarity
so I will meet you in chapter 8 practice set where I will give you more clarity about recursion and functions
let's move on to chapter 8 practice set now so guys now we will do practice set so first I will close this file
and here I will make a new folder which I will name chapter 8 practice set chapter 8
and here practice set so I will write chapter 8 ps here
and after this I will write shift right and open with code and here you can see
the first file I will make problem1.py and first question is
write a program using functions to find greatest of 3 numbers this time we have to use functions
how to use functions to find greatest of 3 numbers so what I will do here
I will make a function named greatest and it will take 3 numbers a b c
a b and c and I will write same logic here if a is greater than b
and a is greater than c then greatest is a or I will do one thing
I will return a then I will put an aleph I will say aleph
if b is greater than a and b is greater than c
then return b otherwise what will happen otherwise I will use same logic
and I will say c is greater than b and c is greater than a and here
a b and c we have taken 1 2 3 and what I will do
I will print greatest of a b and c so if I print greatest of a b and c
like this then it will print 3 so I have printed return value of this function
if I make it 23 then it will print 23 so basically what I have done
I have written logic in a function great now let's move on to the next question
write a python program using function to convert Celsius to Fahrenheit so what is the formula of Celsius to Fahrenheit
if you don't remember then let me tell you c upon
5 is equal to f minus 32 upon 9 this is the formula
so if you write 37 then in Fahrenheit
it will be 102 103 so let's do one thing we have taken this formula
so what will be the value of c it will be 5 multiplied by
f minus 32 upon 9 so what I will do here I will write f is equal to
int input enter temperature in f
and here I will print c but I remember I have to do it using function so I will test it like this
if I do 100 in Fahrenheit it will be 37.77 so what I will do is
I will put it in a function and what I will do here I will write f to c
that is Fahrenheit to Celsius and what I will do here my f will come here
and I will do it like this I am showing you how to do it
and I will do return return this and you will see here
I have taken f value as an input and I will write f to c and then I will write f
if I write 100 then it will be 37.77 and it is a good practice
that we do colon space so if I run this and I do 66
then it will be 18.88 degree Celsius if you want you can write it as an f string
write f double quotes your temperature
and your degree c and one more thing I want to show you
that you can copy degree from internet degree symbol so if you search degree on google
or I do one thing I say give me degree sign here I got it
I will copy it and I will paste it here and now you see
if I write 100 then it will be 37.77 degree Celsius now
I have done it like this now here it is not looking good like I am writing 100
so in python there is a round function which I can say like this that round it to
two decimal places so first argument of round is the value which you want to round
and I will make a variable c c is equal to so that our program is readable
otherwise our program's readability was ending so here I said c is equal to f to c
and I gave it f and I made f string and said round c,2 degree Celsius
now you see I will run it and I will write 100 so it will be 37.78 degree Celsius
so if I do it like this it will look better so yes you have to try it 2-3 times
it will come out great cool man cool next question
how do you prevent python print function to print a new line at the end I told you about end argument
so the same thing is done here I told you earlier and in notes
if I write print a and I write b and I write c
and I write d now you see I will use multi cursor functionality
by pressing alt and you see what I will do I pressed alt
I need a cursor here and I will write end is equal to blank
so you see new line will come between a and b but not between c and d because I wrote end is equal to blank
so what does end is equal to blank do it avoids new line in python so if you don't want new line to come
then you will write end is equal to blank so our question is how do you prevent new line in
print function at the end awesome write a recursive function to calculate the sum of
first and natural numbers we have to write a recursive function to print first and natural numbers
now this is going to be a bit tricky if you don't understand recursion and this is your one more chance to understand recursion
and I have also added a question because I want to explain recursion to you so let's start with multi line comment
if I want to sum first and natural numbers 1 plus 2 plus
3 plus 4 all the way till end
sum of 1 will be sum of 1 will be equal to 1
sum of 2 will be equal to 1 plus 2
sum of 3 will be 1 plus 2 plus 3 sum of 4 will be
1 plus 2 plus 3 plus 4 sum of 5 will be 1 plus 2 plus 3 plus 4 plus 5
sum of 5 will be 1 plus 2 plus 3 plus 4 plus 5 sum of n will be
1,2,3,4, all the way till end now we will write sum of n
in terms of sum of n minus 1 can I write this
because here n minus 1 will come can I write this sum of n minus 1
and after this I will write plus n so if I make def sum
make a function of sum so I can say return sum n minus 1 plus n
and I have to apply a base condition because otherwise it will be like this
sum minus 1 will go to sum minus 2 when n value will be 1, then it will be 0 sum of 0, sum of minus 1
if n equals to equals to 1 return 1, because sum of 1 is 1
so it will calculate when it will get sum of 1 it will write 1
so what we will do we will print sum of 4
what will be sum of 4 4,3,7,3,10 10 is being printed, why not
10 is being printed so what happened I have written it in an exif form
and applied a base condition why do we apply a base condition we apply a base condition
so that your recursion does not go infinitely what is the meaning of this
suppose you calculated sum of 3 it will say 1 plus sum of 2 plus 1
then sum of 2 will be calculated sum of 1 plus 1 then sum of 1 will be calculated
if you don't have a base condition sum of 0 plus 1 then sum of 0 will be calculated
sum of minus 1 plus 0 then sum of minus 1 will be calculated so we don't go to the sum of minus 1
that's why we stop it at n is equal to 1 that's why we apply a base condition
so that n is equal to 1 hits and as soon as n is equal to 1 hits if n equals to equals to 1
return 1 I hope you are understanding do you understand
let's do the next question print this pattern print the first n lines
what will I do here I can do this with recursion how will I do
to print n print n minus 1 and print 3 stars
problem 5.py so what I will do I will make a function named pattern
I will give n to this see to print n
first print star how many times print star into n
print star and then run the pattern for n minus 1
simple and what will be the base condition if n equals to equals to 0
then print nothing print nothing
that's it I will return this what happens with return
we have to return a value if we use return in a function
then it will pass to the variable a is equal to function
we have seen this before if you didn't understand you can see it in chapter
but I explained it to you second work of return if this function is running
as soon as it sees a return statement it means that the function will not run
the call of that function will end there if it sees a return then
there will be no call below there will be no call below return means it will end
return means it will end so if I call pattern 5
see here pattern 5 is called see here
n is equal to 3 I will show you for n is equal to 3
for pattern 3 you can use int input but 3 star, 2 star, 1 star
you can see here 3 star, 2 star, 1 star and that was question number 5
question number 6 write a python function which converts inches to centimeters
inches to centimeters problem6.py so
def inch to cm so what I will do I will take value inch
so what I will do for inches to centimeters I have to multiply by 2.54
so I will return inch multiplied by 2.54
so what I will do I have to do 5.4 so what I will do
int input enter value in inches
and after this what I will do I will put f string the corresponding
the corresponding corresponding value in
cm is inch to cm's end
so let's run what is 6 inch?
so 6 inch is 15.24 cm so what is 6 inch? 15.24 cm
what is 7 inch? 7 inch 7 inch is 17.78 cm
what is 8 inch? 8 inch is
20.32 cm i hope you understood this great
7th question write a python function to remove a given word from a list and strip it at the same time
we have to remove a word from a list and strip it
at the same time so basically what is the meaning of this problem let me explain
the meaning of this problem is 07 underscore problem7.py
problem7.py problem7.py the meaning of this problem is
if you have a list let's say i have a list and its name is
harry and rohan and then shubham
and let's say i want to remove an and then an
so if i give this list to that function if i write def n
or remove which takes a list so what i will do
for item in l i will remove l.remove
and what i will do w word
and i will return this so i will write l and word
so if i write rem and here i will write l and an
so what will happen my output will be nothing because i should not have
written l.remove i should have written l
so if i run this an is removed but we don't have to do this we
have to strip an we have to run dot strip function and do an
for item in l and what i will do i will change this item
and i will strip an so for that i will make a new list
n is equal to empty array and what i will do
for item in l and i will not remove l.remove i will say
if item equals to word item is not
equals to word then we will append item append item in n
item dot strip and word
strip function and strip function from starting and end
removes any word and here i will return
n return so here you see harry removed an from rohan
and removed an word from here and this is shubham yes i agree this question is little vague
but again i wanted to explain you that if you want to return a new list then how will you do
so if you didn't understand in first take then no problem but i framed this question
to increase your understanding and i gave you solution next question says
how to print multiplication table of a given number so
08 underscore problem 8 dot py and here we will
multiply table and we will write for
i in range 1 to n plus 1 and here i will print
what i will print here n and here i will write
multiply by i and it will be equal to n star i and
so multiply if i do 5 then it will print 5 table and it did
i shouldn't have written n plus 1 i should have written 11 because i need 10 so 5 table came
from 5 into 1 to 5 into 10 this was the question write a python function to print multiplication table
of a given number and yes we did it so that wraps our chapter 8 practice set now i will meet you in chapter 9
which is an amazing chapter and before that we are going to do a project so be ready
for that project so yeah that's about it i will meet you in project 1
friends with a lot of pride i am making this folder whose name is project 1
and project 1 is because in this project you are going to learn
how to make a game i will shift right click and open this folder in bs code and the question is very simple
infact this is not a question this is a very amazing project snake water gun
snake water gun we used to play it in childhood today we are going to make it with the help of python programming
so we all played snake water gun, rock paper scissor if you haven't played then google it
no need to google i know it so what we are going to do
we are going to make a file named main.py whenever we make a project we usually make a file named main.py
in which our main code is there so what i am going to do first of all i will say
computer ji what will be your input suppose the input of computer is snake water gun so i will do one thing
i will write one is for snake one is for snake
minus one is for water and zero is for gun we will do like this
one is for snake minus one is for water and zero is for gun
now computer will choose minus one or one or zero suppose computer chose minus one
then you will be asked what did you choose so u is equal to int of input
int of input of enter your choice so from snake water gun
you will choose any one infact i will do one thing i will do like this
and then i will make a dictionary i will say i want you to enter
snake water gun s g and w enter your choice
now what will happen you will make a dictionary now you will say what is going on
if it is one then snake s
infact let's do one thing let's keep key of s as one for w
it will be minus one and for g it will be zero
don't take tension first of all we have to convert
things in mathematical form i said one for snake minus one for water
zero for gun and after this i wrote enter your choice
i asked user to enter one from s w and g he will enter one
i will do one thing u is equal to i will write input
then i will write u num is equal to u dict
now you will say first i took input from user in form of s w and g
then i converted it if it is s then it will be one if it is w then it will be minus one
if it is g then it will be zero i will say in this dictionary put whatever user has entered
and assign value in u num it is simple if it is s then it will be one
if it is w then it will be minus one if it is g then it will be zero very good
now i have u i have computer and i have u i will do one thing
i will do u str and i will do u i can put anything in name of variable and here also u str will come
so i asked u as a string user and i converted it in u now i have computer and u
now i will say if computer entered minus one
if computer is equal to minus one and u is equal to
equal to what? u is equal to equal to one so who won?
so i will do u win if computer said water and you said
snake then snake will drink water u win but if it didn't happen
then what will happen? i will write elif here i will say if it didn't happen
computer entered minus one and you entered zero then u lose
if computer entered one
this time if computer entered one i will copy paste it
if computer entered one and you entered minus one then who will win? computer said water and you said snake
you said snake so who will win? you will win
and u is equal to minus one sorry computer is snake and you are water
so you will lose and if computer is one and you entered zero
then computer entered snake and you entered gun so gun will blow the snake
so who will win? you will win so u will win after this we will take one more possibility
in which computer said see we took one possibility of computer
we took one possibility of computer now we will take zero possibility of computer computer zero and you entered minus one
who will win? computer said gun and you entered snake so here u will lose
and here u win i hope i have enumerated it correctly and after this
i will write else and i will write print something went wrong
ok let's play this game let's play this game
it will be fun enter your choice s you win or something went wrong
why it happened? i didn't put elif here if elif elif elif
elif elif elif else now i will show you
i will say i chose water something went wrong we will have to see what's wrong
so here computer chose minus one ok so computer chose minus one
and what we chose? we also chose minus one if we and computer are not equal then it will be a draw
so we have to put first condition if computer is equal to equal to u
it's a draw it's a draw ok
after that i will say else ok
this is a very good example of nested if-else i wrote something in if i wrote something in if
in else i wrote another if-else ok if it's not a draw then only we will evaluate
otherwise we won't i hope you are enjoying let's play this game
and i will do one thing before playing i want to print
what you and computer did ok before i say it's a win or a lose
i will print f string and i will say you chose
and i will write what should i write i want to know
what you and computer chose so i will do one thing i will write computer chose
and what i will do i will make a reverse dictionary and i will write
what will be my reverse dictionary reverse dictionary will tell what is snake, what is water
what is gun i have to write it correctly i will do ctrl c
in reverse dictionary i will write 1 and i will write snake like this
and after that in minus 1 i will write water
and in zero i will write gun like this and i will write
you chose and here dict and after that you
and like this and
computer chose this so i will run it again so enter your choice water
so it's asking you chose dict minus 1
you chose dict minus 1 you chose
i made a variable you dict you dict
like this now i will run it you dict you
computer chose this key error minus 1 you dict
you dict key error minus 1 so i have to do it in reverse dict
because i have already let me tell you what is the issue i have to do it in reverse dict
because in reverse dict 1 snake minus 1 water so if u value is 0 1 2
0 minus 1 0 1 minus 1 0 whatever it is it will come here
so now this program will run well i will explain the whole program again you chose snake
computer chose water let's run it again i will choose
water, computer chose water let's run it again i chose water
computer chose water let's run it again i chose water, computer chose water
a big problem here is we have already defined what computer chose do you understand?
so we will generate random number in python to generate random number in python
to generate random number in python to generate random number in python I will use, generate a random number out of minus one, zero and one in python
and see, it will tell me how to do it by using random module so see, we can use random.choice method in this way which will help us to choose any one number from these three
sometimes chat gpt becomes very convenient because we don't remember the functions let me tell you which function is doing what
I don't remember this I remember random.choice I remember random.randint
but according to me chat gpt is very efficient in today's date so you can do one thing first do your own implementation
and then ask chat gpt what it will do now this game is made our perfect entry and choice
I did snake computer also chose snake, draw I will choose snake again
computer chose water, I won now I will choose gun and see here, draw
I am not losing even once till now are you seeing friends? I will choose gun
are you seeing friends? are you seeing friends? this means I am a snake water gun champion
I want you all to write in the comment harry bhai is a snake water gun champion everyone write in the comment
and also put timestamp everyone write in the comment and I enjoyed making this project
now let's understand this program once again we imported random module so that we can generate any random number from minus one, zero and one
we said u str is equal to enter your choice means you can enter any one choice from s, w and g you entered it
it will convert in your u what will be the value of u? one, zero or minus one
actually I didn't want you to enter one, zero or minus one I wanted the program to be intuitive the user experience should be good
for this what we have to do any one from one, zero or minus one you choose
and it will automatically convert in number now I have two numbers one is computer and other is u
so I will write here by now we have two numbers which is two numbers u and computer
two numbers variables u and computer in u and computer there is minus one, zero and one so we made a reverse dictionary
which will convert number in snake water gun so to tell you what you have chosen we used reverse dict
we won't use dict we won't use u dict we will use reverse dict
why are we using reverse dict? because in this one is snake, minus one is water
and zero is gun okay great, very good
now what we did after that we said if computer and you both chose the same thing
you also chose snake, computer also chose snake so it's a draw okay, it's a draw
otherwise what will happen we have to see each possibility
if computer did minus one and you did one so you won
okay if computer did minus one and you did zero
so you lost if computer did one and you did minus one
so you lost now when are you winning? see you are winning like this
okay one zero zero minus one
and you are winning in zero one now many people do they find a pattern in winning
they find a pattern in this what they do I will tell you in a while
what is the meaning of this but I will end this program so we saw each possibility
by putting if, else, else, else something went wrong it shouldn't come
but we still put it just in case something happened then we will know
so you saw here finally he will tell us who won, who lost
a simple program your first game of life if you made it
then write in the comment section congratulations and congratulations to all those people
from you and me who made their first game of life so do write in the comment section
congratulations it will feel good from the heart now you see here
what people do now if I sum this if I sum this
so this is zero this is zero in which I do one thing for you
I take this in a new file and I brainstorm with you see this is zero
I summed these two this is minus one I summed these two
this is zero I summed these two this is one
I summed these two this is minus one I summed these two
I summed these two this is one means I added zero and one
now see whenever you are winning whenever you are winning so I don't understand
any such pattern that I don't understand
any such pattern that you are winning so whatever
means minus one or smaller than zero I don't see
any such pattern so this pattern failed
now I will calculate one more pattern here what will happen
that will happen I will calculate computer minus you
here will come computer minus you minus two minus one
this is advanced analysis going on I will make you do
but still minus one minus zero what will happen
will be minus one one minus what will be minus one
will be two one minus zero what will happen
will be one zero minus minus one plus one
this will be minus one and now we see did we get any pattern
here you lose is minus one
you lose is two you lose
is minus one and whenever you are winning
then here is one or minus two here is one
or minus two in minus one you are losing
and in two in plus two you are losing
ok so when are you losing you are losing
when minus one is coming and when
two is coming in other cases one or minus two
you are winning so you can replace it
with a simple thing if computer minus you
you said that this metric
if its value is
what if its value
is minus two one
so is equal to
minus one and its value
is two ok in two
you are winning not in two in minus two
you are winning or its value is minus
wait a minute here we have taken out the
lose so if it is two
then you are losing or if it is two then you are losing
or you are winning ok so
you can replace so many lines with this
if this happens then print
you lose ok because you are losing
on minus one or what will happen or you are winning
ok or you are winning else you are winning
but this program will not be readable
you can do this else you can say here
else print you win
so you can do this but again this is only in your mind
that why did you write this logic ok
so if i say to comment out
this logic and here write this logic
so you see here that your program
will work so see i did snake
computer chose water you won
if you chose water computer also
chose water it was drawn if you
chose water and computer chose snake
you won so this logic we reduced
number of lines we made four lines
of code but readability is
less so here you have to
write comments to explain why did you
do this or you have to stick
with this i will not recommend this
but i just wanted to tell you
why did we do this so i will
cut this and i will make a
main shortened .py
ok let's copy this
and make this readable
yes good now what
we will do is here
i will copy this and put it
here just so you have
it and i will
put a .py just so
that you get this logic
so here i have taken
computer minus u basically
here i have taken computer
minus u computer minus u
is equal to minus 2
here basically i have
taken computer minus u
ok here i have taken
and i will write here
the below logic is written on
the basis of the value of
computer minus u ok
so according to our convention
we have predicted win or
lose ok so let's
capitalize y here ok
and here let's put an
exclamation mark and
i am taking you to the
roller coaster rides and i
hope you like it and i
can skip it if i want
and if i talk about
your happiness index it will
increase if i skip
this complex topic
but i don't want to
skip yet another programmer
ok i hope that makes sense
and with that said our project 1
is over next chapter is an
amazing chapter why am i saying it
because you can read and
write in file so i
will meet you in chapter
9 which is file io
so our next chapter is file
so i will
tell you what is a
file and how
does a program run
so whenever you run a python
program first it loads
in memory so what is the
meaning of memory it means
ram which we call
random access now
understand these terms volatile
non-volatile and persist what is the
meaning of these terms
volatile means on which
data doesn't persist like you
run a program its output
came there was some
computation in it whatever
data will be there
which is stored
in random access
memory means stored
in random access memory
temporarily there so
here we will understand
file io so
programmer wrote our
program and he can
rewrite from a file
let's say i made
a program which process
a particular data let's say
i have a very long string
from that i am extracting
email a basic simple
program to extract email
i wrote it let's say
i have a variable here
chapter number let's say
a computer program a is equal
to a very long
string with emails
with emails and
you wrote a program to extract
emails let's say you have
emails in a list
output was emails
in a list once
program is running
will you get those
emails stored no
program is running emails
are generated program
is exiting it's
over it's important
to understand that you
generated emails and
it's over but
you have mp4 file which is
video file mp3 file which is
song file there are many other
formats in all files data
is stored and if you want to
store data means whatever
generated by your program
you want to persist persist means
you want to save in your
disk forever data
will persist means
save like in your
hard disk file it
persists you run
program in your ram
it's over ram is
volatile memory if you
want to persist data
use non volatile
memory if in your
ram data is loaded
temporarily you don't
want to load data
temporarily you want
to save data
in some cases
like python program
which is taking
spam comments from
computer it can
take spam comments
words you is
for you wild
car dreams screw
fuck it over
damn you can
use ram hard
hard disks you
you necessary make
it work well
you you you
webs web web
screen you can
have loved you
weak person you
you are weak
per person you
are weak A file is data stored in a storage device. A Python program can talk to the file by reading content from it and writing content to it.
So here you can see that a Python program can talk to a file by reading content from it or we can read from a file or we can write from a file. Here we can read from a file or we can write from a file. So this is what we are going to see in this chapter that how reading and writing works in a file. So there are two types of files, text files and binary files in general.
Text files are those which we can open in VS Code and can see its content. .py file is also a text file because we write it with the help of textual character. So how will you read the file? So there is a very simple way to read the file. If I write here file.txt and write here this is a good boy. So to read the file I will write f is equal to open and here I will write file.txt
and here I will write data is equal to f.read, I will print the data and I will close f. I will run this program and you can see that Harry is a good boy output has come that means we have read this file. So what is open? Open is a built-in function in Python which helps in opening files. What happens with f.read?
F.read basically helps in reading our files. So as soon as I run f.read, whatever file is opened in f, its content will come in the data. Then I printed the data and this term is very important f.close. Whenever we open a file, it's our duty to close it. If you close it, then basically you are telling the computer that I have taken whatever I wanted from this file, now you close it.
Like there is a fridge in your house, you open the fridge, you take out a cold water bottle from it, you drink water, you put the bottle inside, you close the fridge, you also close f.close. Because you close the fridge, you also have to close f.close so that the file does not remain open, later if someone or some other program wants to access that file, then they can do it. So doing f.close is a good practice.
Even if you don't do f.close, it will still work, but we should put the habit of doing f.close. Why should we put? Because once we have accessed the file, our work is done, then we want to give a signal to the computer that our work is done, now we are not reading this file, this file is not open in our program. I hope you understood this thing.
Let's come back to notes, I told you about two types of files, one is text files, one is binary files. Now in Python, we have a lot of built-in functions, one of them is open, which is a built-in function, it takes two parameters, it takes the name of a file, it takes a mode, here you see that I did not give the mode, I just gave the name of the file,
and the reason for that is that in open function, by default, the mode is read, r for read, if I have to write the file, then I have to write w with a comma, but if I write r like this, then by default it is r, there is no use of writing it, if you want to open a file in read mode, directly open and write the name of the file, and your file will open,
so this was the basic of the file, and we have a file called file.txt, now I will teach you how to write a file, how to write in a file, suppose I have a string and that is st is equal to hey harry you are amazing, this string can be as long as you want, what I will do, I will write in a file, so I will keep the name of that file as my file,
so what I will do, I will write f is equal to open my file.txt and this time I will open it in write mode, w means write mode, write mode means that I want to write in this file, I will hit enter, f is equal to open, I wrote my file.txt write, now I will use f.write and I will give a string named st, and after this I will close f.
so I took a string, I opened a file, gave the name of that file, opened it in write mode, used f.write function, gave a string, f.write takes a string, whatever string you want to write in this file, you write it in f.write, and then I closed this file, so first I opened the file, then I wrote in the file, then I closed the file,
now as soon as I run this program, you will see that my file.txt has been created here, if you do the video a little behind, then you will know that my file.txt has just been created, let's open it and see, wow, it has this hey harry you are amazing, it means that we were able to write successfully in this file,
I hope you are enjoying it and you got to know how to read and write in a file, so the most important thing is to read and write in a file, so in any file, we do read and write using these two functions, read and write, there are some other functions related to files, like readline has a function, readline has a function, readline has a function, readlines also have a function,
what does readline do, it reads your lines one by one, for example, I have a file, I do one thing, I make it with the name of 03 underscore more file functions, and here what I will do, I will take a file, for example, I have a file.txt, in which there are some multiple lines, I am a second line, I do it,
now I also do toggle screen cast mode here, this is amazing, I do it, I write twinkle, twinkle, I am not able to write the spelling of twinkle, little star, I write the spelling of twinkle wrong, now I will write here, f is equal to open, which file was this, file.txt,
do write it in double quotes, file.txt and we are opening it in read mode, so I don't need to write r, now if I write lines is equal to f.readlines, so readlines returns a list, which is a list of our lines, I will show you, if I print lines and type of lines, then you will see that it is a list,
then f.close is our duty, now I will run this program and show you, you will see a list, Harry is a good boy, backslash n, Harry, I am a second line, backslash n, this is amazing, backslash n, twinkle, twinkle, little star, finally it came,
class list is written because it is a list, so you should know this thing, we have closed f. and we have f.readlines function, but what is f.readline function, f.readline function, the more times you run it, the more times your lines will be read,
I will show you how it works, I will write line1 is equal to f.readline, and then I will print line and I will copy it and do it like this, so that you get this code later, but now see here line1 is equal to f.readline, now I can print line1 and type of line1,
similarly I will do the same thing with line2 and line3, so I will do this with line2 and line3, and you will see that I will get line2 and line3, so once I run this function, Harry is a good boy came, I am a second line came, this is amazing came,
but after this is amazing, there is no line, actually there is, twinkle, twinkle, little star, so that will also be printed, so I will do line4 is equal to line4 and type of line4, which will be a string, you see twinkle, twinkle, little star, class string,
but if I do line5, I mean I do line5, I will replicate it, then what will happen, then what will happen, if I write line5 here, then what will happen, so I also do line5 here, I am using multicursor functionality by pressing alt, I will run this,
and you see nothing came after this, nothing came, if I am printing line5 here, then nothing is coming, an empty string is coming, so if I print line equals to equals to this, then you see this is true, so an empty string is coming, so here our readline,
keeps on reading till you get an empty string, means till all the lines finish, so if I have to do this work in loop, then how will I do, this is a very cumbersome process, so I can do this work in while loop also, I can say while f.readline is equal to equal to what,
here I will do one thing, I will say line is equal to f.readline, you read one line, now you will say while line is not equal to, while line is not equal to an empty string, print the line, print the line, and then line is equal to f.readline,
and you see, line will keep on printing till you get all the lines, so eventually what will happen, when all the lines finish, then here line is equal to single quote, and line is equal to f.readline, and I hope you understood this,
now you see modes of opening a file, R open for reading, W open for writing, A open for appending, now what is appending, we have seen read and write, read means you want to read a file, write means you want to insert some data in a file,
what is the meaning of A, what is the meaning of appending, append means you want to add something at the end of a file, we have seen append in list, append means you want to add something at the end of a particular file,
so if I open any file in append mode, I will show you here, 04 underscore append dot py, so the way I was writing in file, if I open any file in append mode, and I will make it A,
so what will happen, it will be inserted at the end of that file, now what is there in myfile.txt, only once hey harry you are amazing is written, if I run this program, the same thing will be added at the end,
and as many times I run it, it will be added, see I have added it so many times, so as many times I run it, it will be added,
so I hope you understood what is append mode, this is a mode to add at the end of a file, then we have a plus mode to update,
then we have RB mode, read in binary, then RT is read in text, which is also the default mode,
so if you write RB, it will be read in binary, if you write RT, it will be read in text, we saw how files are written in python,
and this is what I have written here, now with statement is a very good concept, I will tell you what is with statement,
and we will use with statement only, with.py, now what is with statement,
suppose I have a file, I will open it from open, I will say file.txt,
print f.read, and then f.close, the burden of f.close is very heavy,
now you will say, do f.close, why don't you want to do f.close,
do your responsibility, yes, I will do f.close, but if there was a way,
which would have done the same for me, and at the same time, I wouldn't have to do f.close,
then it would have been great, and this is the way with statement, the same can be written using with statement,
like this, how, what you do here,
write with open file.txt, file.txt, as f,
and you write here, print f.read, you do something like this,
you do the same thing, and you don't have to close the file, I will write here at the end,
you don't have to explicitly close the file, you don't have to close the file, it will be done automatically,
this is the magic of with, so you can write this code like this, and sometimes we have to write a lot of code
between f is equal to open and f.close, then you will write that code inside the with statement,
the rule is the same, indent the python colon, and you are inside the with statement,
and as soon as you leave the with statement, your file will be closed automatically, so this is an amazing tool,
call it a syntactic sugar, call it a tool, it automatically closes the file,
amazing stuff, the best way to open and close the file automatically is with the with statement,
so I have written the code here, now we will move to the practice set of chapter 9, in which we will practice,
and we will see how we deal with files, I have designed some good problems for you,
so let's move on to chapter 9 practice set now, all right guys, now we will move to the practice set of chapter 9,
in which we will see the questions of file io, so I have made very powerful questions for you, so the first question of our file io
is on your computer screen, write a program to read the text from a given file poems.txt
and find out whether it contains the word twinkle, so what we have to do is we have to make a file called poems.txt,
I will shift right click and open this folder in vs code, and I will make a new file here, so we have a file poem.txt,
and in poem.txt, suppose it is written twinkle, twinkle, little star,
how are you my brothers, now what we will do is we will read this first,
what is twinkle, twinkle, little star, I will open poem.txt and after this I will write f.read and after this I will close f. This is the last time when I am opening a file with the help of f.
I will use with only because that is the correct way, in fact the easiest way and the best way to open a file. So I will try to use with only but for now I have done c is equal to, i.e. content is equal to this. Then I will write if twinkle in, wait a minute, twinkle is a string, in content and I will make it content only,
i.e. content of this file. So what did we do? We opened this file and read the content of this file. Then we are saying that if twinkle is present in this content then print twinkle is present in the content.
Otherwise, what will we do? I will do one thing, the word twinkle is present in the content. Otherwise, what will I do? Otherwise, I will say else the word twinkle is not present in the content. So we have written here that the word twinkle is present in the content.
Then after this, the word twinkle is not present in the content. So let's see whether or not the word twinkle is present in the content. Because our content is also twinkle. If I do something like this instead of twinkle.
And now if I run this program, then see the word twinkle is not present in the content. But let's change it to twinkle twinkle little star. So you got the point. If there will be a twinkle word, then it will show, otherwise it will not show.
It's a very simple thing. Come back to VS code. And in PDF, our next question is that the game function in a program lets a user play a game and returns the score as an integer. You need to read a file highscore.txt which is either blank or contains the previous highscore.
You need to write a program to update the highscore whenever the game function breaks the highscore. So what do you have to do? What will this game function do? That it will help a user to play a game and returns the score as an integer.
Then what do you have to do? You have to save this score in this highscore file. Or what you have to do is compare it with the previous highscore and update the new score. So let's make a game function here.
Let me show you how to do this. problem2.py And here we will first make a game function.
And after making the game function, what will we do? It lets a user play a game and returns the score as an integer. So what will I do if you make me play a game?
I will generate a random number. So I will write here import random. And I will write here return random.randint.
And what does randint take? It takes two integers. Between which it gives a number. So I will give a number between 61 and 62.
Now what we have to do is You need to read a file highscore.txt which is either blank or contains the previous highscore. You need to write a program to update the highscore whenever the game breaks the highscore.
So what we will do is We will make the user play the game. So what we will do here is
We will print you are playing the game. And here I will write your score. I will print.
And here I will write your score. And here I will write score by making fstring. And I will take this in a variable called score.
So it is simple. We have to write here score is equal to. So what we did here?
We wrote you are playing the game. Now what we have to do here is If our score is greater than highscore.
Then what we have to do is We will write here write this highscore to the file. So what we will do here is
We will write highscore in the file. But we have to make a highscore.txt first. Which is blank.
So what we will do here is First we will write If highscore is greater than highscore or
Or highscore is equal to equal to blank. Is equal to equal to blank. So what we will do is
We will write highscore in the file. So what happened is Game function will run.
It will print you are playing the game. Then a random score will be generated in the score. Then what we will do is
We will write here With open Or highscore.txt as f
And we will say Highscore is equal to f.read Highscore is a string.
Then we will say Score is greater than Int of highscore
If score is greater than Let's do one thing If highscore is equal to equal to blank.
Then what we will do is Highscore is equal to int of highscore. Now see what we are doing here.
Try to understand this. What we did is Game function will run.
It will print you are playing the game. Then a random integer will be generated in the score. Between 1 and 62.
Then what we are doing is By reading this file We are taking its highscore as an integer.
So we have already read its content. Then we said if it is blank Then highscore is equal to 0
If it is not blank Then highscore is equal to 0 If highscore is not equal to blank
Then highscore is equal to 0 Otherwise highscore is equal to 0 If highscore is 0
Then any score is greater than 0 Because we are generating between 1 and 62 Any number between 1 and 62
Will be greater than 0 If score is greater than highscore Then we will write
We will use this kind of thing With open highscore.txt This time we will open it in write mode
And we will say F.WriteHighscore F.WriteHighscore
So we did With open highscore.txt As F.WriteHighscore
We will write this string In highscore.txt I hope you got the point
And it will be replaced If it is 62 Then new highscore
Will be 10 So that thing will be done automatically I hope you got the point
Your score is here Let's run it Error, write argument must be
String not integer So we have to convert it in str Because your write
Function takes a string So let's try again Your score is 43
Did 43 come here? No So this is a problem We have to write highscore
If score is bigger than highscore Then new highscore will be this So we have to write score
I will explain this program But let me check Your score is 4
I will generate another one 14 came here I am running again
45 came here So this time highscore is 45 I am running again
So highscore will be 45 So Here you can see
What we have done We made a function named game Which is printing you are playing the game
We generated a score Between 1 to 62 And then we
Opened this file and took the content We said if this file is empty Then highscore is 0
And if there is something in it Then convert it in integer And assume highscore
When we read f. It comes in the form of a string So we have to convert it in integer
So we converted it in integer Highscore and highscore is equal to Int highscore means convert highscore
In its integer counterpart So this is a valid statement Let me tell you
Then we printed the score If your score is bigger than highscore Then
Write this highscore In highscore.txt file Because you will write it as a string
I hope i explained it And you understood it If you didn't understand it
Then try to code it yourself I am moving to the next question Write a program to generate
Multiplication tables from 2 to 20 And write it to different files Place these
Files in a folder for a 13 year old We have to write Mountains from 2 to 20
It will be fun Let's say there is a kid He has a book
He used to remember tables from a book He says i have a book I don't have a table from 2 to 20
So we will generate it from python First we will write a for loop For i in range 1 to 21
Sorry 2 to 21 This for loop will go from 2 to 20 Now what we will do
We will make a function In the name of generate table And we will say give it an i
Generate table will be a function Which will take an integer And generate the table
And write it in the files I will write def generate table I will write n
Any number table will generate For i in range n What we will do
I will write 1 to n plus 1 I will write 1 to 11 So that it goes from 1 to 10
I will write Table is equal to I will make a string
I will say table Plus equals to I will add a string
I will say table Plus equals to I will put n
I will put multiply by I will put i Is equal to n multiply by i
I will put a backslash n When my string will be complete I will add this string in a file
I will say with open I will make a folder In the name of table
I will make a folder You can make a folder from here You can make a folder from here
You can make a folder from here Our file system is in sync What is seen in VS code
Is happening here This is the meaning of doing in VS code Table slash
I will write here Table underscore I will write i
Table slash table underscore i I will make f string This table underscore i
Means table of I will write n We will see the table of any number
Then what we are doing We are writing table in this We are writing table in this
We have to write it as f We have to write it as f As soon as I run it
As soon as I run it I have made a folder in the name of tables I will run it again
It is saying here No search file or directory I have to open in the right mode
I have to open in the w mode As soon as I run it My tables are generated
See this I have to do one thing I have to put .txt
I delete this table folder And generate it again I will make a table folder
I will make it manually Tables As soon as I run it
See I have generated tables Let's check
Table of 15 is ok Table of 18 is also ok Let's see table of 17
And here This is also ok And let's see
This is also ok It's great Give it to a 13 year old
He will do it I can't make any excuse See
A file contains a word donkey multiple times. You need to write a program which will replace this word with it
Let's do this It has donkey word We have to replace it
What we will do First of all I will make a file
I will close the folder Problem4.py And after this
What we will do We will write word is equal to And
We will write this And I will make it File.txt
Not a I have written this Donkey is a nice donkey but not a bad donkey
Now what I will do With open File.txt
What I am doing File.txt And I am opening it in read mode
And I am writing R As F Then what I will write
Content is equal to F.read Now I will write
Content new is equal to Content.replace And what I will do
I will replace Donkey with 1 2
3 4 5 6 And after that What I will do in write mode
I will say write again F.write And here I will
Write content So you see what I did First of all I did word is equal to donkey
I will make it word Then I Read the content of this file
What is in the content of this file Then after that I generated a new content In which I replaced word with
And finally I Wrote it again In file.txt
So let's run this program And here it is saying txtr There is something wrong
I didn't put a comma here So I had to put a comma here As soon as I run it
I have to put a comma here As soon as I run it I have to put a comma here
That It didn't work Let me run it again
I wrote it I had to write content I wrote content by mistake
Now I will run it So you have to be very careful You will make small mistakes
See I made a mistake I had to write content So you have to be very alert while coding
Let's move to the next question and this question no.5 is saying repeat program 4 for a list of such words to be censored
so what we have to do, this time there was only one word now we have to give a list of censored words and we have to do this for all the words in that list
and you see what is the benefit of writing a good program I will reuse this program and solve problem no.5 this time we don't have word
this time we have words, and let's say these are the words let's say these are the words, donkey, bad and dirty so I will do one thing, I will write donkey here
and here I will write dirty I am a very dirty donkey so I have written this
so what I have done in this file, I have written these words I have written dirty, good and everything in fact this is dirty
so I will write this and now I will write for word in words so what I will do here, I will write for word in
words and content new which we are going to generate this time, we will say content new is equal to content.replace word
and this time we will not do content new, in fact content is equal to content.replace so that we update the same variable and you see I have updated the same variable
and what I am doing this time, word and this time I will do something like this I will do this in double quote and I will say
into length of word so whatever is the length of the word, this hash will come so it looks like this, I will remove this hash
and as soon as I run this you see all these words are censored
and how is this program working, basically what we are doing and after this we are reading the content of this file then what we are doing for every word
we are updating our content string, we are saying replace content wherever the word is put star as much as the length of the word
we have done a simple thing here, then what we did here we said file.txt w
f.write content means write this content again so first we replaced the content
every word's occurrence we replaced it with hash into length of word then what we did here
so I hope you got the point and understood how our program is working now write a program to mine a log file and find out whether it contains
python so we just have to tell whether there is python in any log file this is very simple
so what we have to do suppose we have a log file log.txt now we just have to find out whether there is python in this
so if I write here lorem 435 I will do one thing, I will do html and you should know if you do lorem 435 in html
then a very big string is generated for you what I will do here, I will add python here now what I will do, first I will read this log file
so I will write with open log.txt and I will write as f then I will write f.read
I will say give me its content content will be equal to f.read and then I will write if and I will write
python in content then what should I do, print yes python is present
else print no python is not present
we have a very simple yes python is present I will run again, yes python is present and if I remove this python from log.txt
if I remove this python then what will happen, no python is not present so our question is also solved
now come to the next question write a program to find out the line number where python is present from question number 6 now you have to find the line number where python is present
how will you do that so what you will do, you will read the lines one by one so I will show you how to do it
so we will make 07 underscore problem 7.py and this time we will not read f.read we will write line number here
line is equal to 1 we will start from first line line is equal to
line is equal to f.read and then what we will do if python is in line
yes python is present what will be the line number and I will explain you how it happened
I will explain you very well, don't take tension so what we did here, we printed that python is present and we wrote line number here
so what we will do we can do it in two ways, one is read line or lines is equal to f.read lines
and then run for loop and here line is equal to 1 by default line number is equal to 1
and here what we will do line number plus equals to 1
and if this list exhausts then what we will do if this list exhausts
we will say no python is not present we read else with for but if we get python and line number
then we will break it if we break it then else will not work
if we break it, then else will not work if the loop is exhausted, then else will work we saw this in for with else
if you run for loop and it runs for all lines means for each value
then else will work else will not work else will work when for loop is complete
if you break it, then else will not work with that said, let's run it but if i go to log.txt and write python
and write python so python is coming in line number 3 let's try to run the program again
yes python is present actually i had to write line number so let's run it again
yes python is present, line number is 3 easy peasy lemon squeezy come back to question 8
write a program to make a copy of a text file this.txt is very simple we have to make a copy of this
what we will do first we will say with open this.txt
as f content is equal to f.read with open this.txt
open in w mode as f content is equal to f.write
content is equal to that's it in this.txt
i am enjoying let's say i write anything then i run it
as i run it, copy is made now come back can i write a program to find out
whether a file is identical and matches the content of another file we will have two files let's make a file for 9th question
what will happen first file will be file1 and second will be file2
we will say file1.txt as f content1 is equal to f.read we will do the same for content2
we will say contents of file1 and file2 if content1 is equal to content2
then print yes these files are identical or else print
else else you will do no
no these files are not identical let's run i will do one thing
i will compare file.txt with poem.txt and obviously content is not same file.txt and poem.txt are not identical
if i compare this.txt and this.copy this.txt and this.copy.txt
then it will show me yes these files are identical it is simple 10th question
it is very simple 10 problem10.py
how to wipe out a file i will tell you a very simple way see this
i will show you see this i will blank f.write
in any file and content will be wiped i will run
content is empty next question how to rename a file
first we have to bring the file 11th question problem.py
and here there is a file log.txt i will make old.txt
i am old and i will rename it to new.txt a very simple way to rename
what you do make a copy of your existing file and rename it to txt and delete this file
for this you have to use os module but i will not use os module i will first read the content of this old file
and to read the content we have already written a code this is the code to read the content so we will read the content of old.txt
and what we will do we will write the content we will write it
in another file renamed by python.txt we will do like this
renamed by python.txt and we will write the content and if you want you can use os module
or you can use shutil module and delete the old.txt to do this you have to import shutil
which is a module and you can delete the file but for now i will make renamed by python.txt
which was in old.txt alright so with this our practice set 9 now we will move to chapter 10
which is object oriented programming i am very excited it is a very important topic and you will enjoy it
let's go to chapter 10 so guys today we are going to see object oriented programming what is object oriented programming
it is a paradigm of programming and we have two types of approach one is object oriented approach
which we are going to see in this chapter and other one is functional approach in object oriented approach we solve the problem by creating an object and this is one of the most
popular approaches in programming this concept focuses on using reusable code
dry stands for do not repeat yourself what does it mean you must be confused
about object oriented programming but i am here to solve your confusion so let's understand object oriented programming
first of all see what is class in object oriented programming we deal with classes and objects class is a blueprint to create objects
what does it mean let's say you have to give an exam and to give that exam you have to
make a form in that form you will write your name maybe email address
and once you fill this form it will be called your form if shubham filled this form it will be called shubham's form
and if rohan filled this form it will be called rohan's form but when this form was empty then whose form was this
when this form was empty then this form was not of anyone it was a blank form
someone filled this form and it became an object so in object there is a particular information but class is a blueprint
only placeholders are there let me explain with one more example if you travel from indian railway
then you must know that there are forms in indian railway which you have to fill so you go to indian railway and write your name
when that form is empty then it is not anyone's form when that form is empty then no one's name is written no one's birth number is written
no one's preferred coach is written but once you fill that form then you write your name
how many people are travelling Finally, you fill that form and once you fill that form, it is called your form. Your form with your information, wherever you have to travel, that information is there in it.
So the empty form was a class, but the filled form has become an object. So what is a class? It is a blueprint for creating an object. We will make a sample class, but this is basically the concept that you can make multiple objects from the class.
The class blueprint is one and many objects can be made from it. Just like we have an empty form, it has a lot of copies. And someone distributes it at the station and anyone can take it.
He can fill his train number with his details and convert it into his form. So come to your notes, class is a blueprint for creating objects. Now see here, you had a blank form, you combined information and made a valid application.
So what was a blank form? In that, combines info to create a valid application. That means there will be placeholders in it.
It will be written what is your name, what is your birth number, what is your preferred coach, what is your train number, from where you have to go, all this will be written in a blank form. But once it is filled, it will become a railway form.
Similarly, if it is a student's JEE form, then there will be combined information in it. Once that student has filled it, it will become the application of that student. Similarly, class is a blueprint in which information is there to create valid objects.
And once we do object instantiation, it becomes an object. I will also show you how this process works. But before that, what will we do?
We will come to our folder, here we will make a folder named Chapter 10. And I will open it in VS Code, right click, open with code and it will open in VS Code. The first program I am going to make here is class.py.
I will show you how to make a basic class. To make a basic class, we use some syntax like this. So I said class employee.
Now I will write name is equal to. Assume the name is Harry. And I will write language is equal to.
And I will write py. I will write salary is equal to. Let's put one more zero to see what is going in our salary.
So the salary here is 12 lakh rupees. The salary is 12 lakh rupees. Now to make an object.
What will I do? I will write harry is equal to. And I will write employee here.
So if you print harry.name and harry.language. Then you will get harry and py. So this is a very basic way to make a class and objects.
But we will go ahead and understand things in more depth. So what is an object? Here harry is an object.
Now see here an object is an instantiation of a class. When class is defined, a template is defined. Memory is allocated only after object instantiation.
When you have defined a class, a template is defined. That is, we have made an empty form. And until we do object instantiation.
Memory will not be allocated for that object. Objects of a given class can invoke the methods available to it. Without revealing the implementation details to the user.
So this is called abstraction and encapsulation. What do we do? Identify your problem.
The class is an employee and a noun. Adjectives are basically attributes. Like name, salary and verbs are methods.
What does this mean? The name of the class. Like employee is a class.
Language can be a class. Animal can be a class. So these are usually nouns.
What is the adjective? Our attributes. Like the name of the employee.
Employee's age. His salary. These are called attributes.
Like in this case, name is an attribute. Language is an attribute. Salary is an attribute.
So I basically made a class in which every employee's name will be harry. Every employee's language will be py. And every employee's salary will be 12 lakh.
Which ideally we don't need. I know. But I gave you a very simple example of the class.
And the verbs are methods. Like get salary, increment. Means do something.
Doing something means running a method. Okay. So now we will talk about class attributes.
An attribute that belongs to the class rather than a particular object is class attributes. What does this mean? Now see here.
I wrote class employee here. I said name is harry. Language is py.
Salary is this. Suppose I remove the name harry from here. Because why would I want to keep the name harry?
Do you understand? I mean, why would every employee's name be harry? So I will print harry.language and harry.salary.
So I made every employee's language py. Suppose there is a company that only works with python programming language. So every employee's language will be py.
And salary, suppose I give every employee 12 lakh. Okay. So I put that here.
So if I make an employee named harry. Something like this. Its syntax will be something like this.
print harry.language harry.salary So I can print harry's language which is py and harry's salary which is 12 lakhs.
If I make another employee whose name is rohan. And I write rohan is equal to employee. Something like this.
And then I print rohan.salary rohan.language Okay. This time I printed salary first and then language.
You see here. Printed. Okay.
So what is this salary and language? It is a class attribute. Because it directly belongs to the class.
Whose name is employee. It belongs to the class named employee. And language is equal to py.
Salary is equal to 12 lakhs. It is a part of the employee. And whatever object will be made from this employee.
That is, whatever object will be made by filling this form. He will get language py. Salary will get 12 lakhs.
Okay. So we have made two employees here. So salary and language are class attributes.
Because they directly belong to the class. Now let's say I want to give a name to my employees. So I will say that harry.name is equal to.
It will be right to keep harry like this. rohan.name is equal to rohanroro. This will be right.
Okay. And let's say I put the name rohanrororobinson here. So now see rohan.name is equal to.
Now I can also print the name. So I will write here. Print rohan.name too.
And here I will write. Print harry.name too. harry.name.
And as soon as I run. You see here. Harry py 12 lakhs.
Rohanrororobinson 12 lakhs py. Because I have printed the name here first. So I can print the name here.
I can print language and salary. Now listen carefully to what I am writing here. Here name is object attribute.
And salary and language are class attributes. As they directly belong to the class. To the class.
Fine. And I will obviously keep this as a comment. So that it does not show an error in my program.
So you can make a comment by doing control forward slash. So what did we see now? Class attributes.
I will also put a comment here. As much as I can make descriptive. My program will be so good.
I will write here. This is a class attribute. And I copy this.
And here I will write. This is an object attribute. Fine.
This is an object attribute. Fine. I hope that's clear.
So here we have object attributes. They are also called instance attributes. An attribute that belongs to the instance.
And it's better that I call it an instance attribute. Because most of the time it is called an instance attribute. Instance attribute.
So here. Instance. Attribute.
So instance attribute. We saw that we put a dot and made it directly. We said that Harry.name is equal to Harry.
Fine. So this is an instance attribute. Instance attributes take preference over class attributes during assignment and retrieval.
Now see. This is very important. Fine.
So I will do one thing. To explain this, I will have to make a file. Tense versus.
Class attributes.py. Fine. Assume that I have an employee.
Fine. And I made its language py. Which is a class attribute.
Now assume that I am giving an instance attribute. I am saying that I am giving an instance attribute named language. And I am writing JavaScript.
You will say one minute. You have already defined a language attribute in the employee class. Which is a class attribute.
In that you have written language is equal to python. Why are you making it again? Same name of instance attribute.
So I will say my will. I will make. You should know what will happen.
So if I print Harry.name this time. Then nothing will happen. I remove it from here.
I remove Harry.name. If I print Harry.salary. If I print Harry.language.
Then 12 lakh will come. There is no surprise that 12 lakh will come. It is a simple thing.
It is a class attribute. It will be fetched. But if I print Harry.language.
Then JavaScript will be printed or Python will be printed. The answer is that JavaScript will be printed. JavaScript will be printed because of this thing.
Instance attributes take preference over class attributes during assignment and retrieval. You try to set or try to get. You will only get instance attributes.
So even if I do Harry.language. Instance attribute will be set. I will print Harry.language.
First he will see if there is an instance attribute or not. If there is an instance attribute, then he will print it. Otherwise, the class attribute will print this name.
Let's run this program and confirm this. And yeah, what I told you happened. But if this JavaScript.
I don't write Harry.language. If I comment it out like this. Then what will happen?
Obviously, the Python will be printed. Because it is a class attribute. And if the instance attribute is not found.
I hope that makes sense. The instance attribute takes preference. So when looking up for Harry.attribute.
It checks for the following. Is the attribute present in the object? First he will see this.
Then he will see if this attribute is present in the class. If it is present in the object, then he will write in the object. If it is present in the class, then he will write in the class.
Self parameter. What is self parameter? It is very important for us to know this thing.
So what happens is that whenever we make a method. Whenever we make a method in Object Oriented Programming. I will make a new file here for this.
And I will write here self.py. Now look, let's say I have an employee class. And I want to make a function called getInfo here.
So you can also put a function in a class. So let's say I put a function called getInfo. I print here.
I make an f-string. The language is. And I will write here.
Language. And I am telling you this is wrong. And then I will tell you what is right.
And then I will write. The salary is. Salary.
Now look here. You will say that this was a class called Employee. Language was Python.
Salary was 12 lakhs. Then I did addEqualToEmployee. Changed the language.
Now let's say you run what? Now you run getInfo. Harry.getInfo.
So what can you do by doing Harry.getInfo? You can say. That Harry is an object of an Employee class.
So automatically language came in Harry. Salary came in Harry. A method came in Harry.
GetInfo which is Harry's own method. So now you can do Harry.getInfo. Because this method is in the Harry class.
Because we have used this template to make it. But if I run it, there will be an error. You see me.
He is saying. Employee.getInfo takes zero positional arguments. But one was given.
I did not give any argument. So why is this one was given coming? The story is that.
When you say Harry.getInfo. So he is like this. Converts into such a call.
Employee.getInfo Harry. Do you understand? Now looking at this, you will know.
That we have given an argument. Which is not accepted here. So here we accept it.
In the name of self. You can accept it by any name. It is usually done in the name of self.
That is, the object on which this method is running. So I will take the language of that object. And I will take the salary of that object.
And if I run it now. Employee.getInfo. I will comment the above one too.
So you see. Language is javascript. Salary is 12 lakhs.
And what is written here. Exactly the above one does the same. There are only two ways to write one thing.
I will say syntactic sugar. If I write Harry.getInfo. It will automatically convert into this call.
Now you understand. Why was the error coming? That I have given an argument.
Because it was converting into this. And in the name of Harry. Which is our object.
We had passed it. In getInfo. Which we were not accepting here.
So once we accept the object. The language of that object. And the salary of that object.
Both can be printed here. If here. Harry.language is not javascript.
Then obviously our language is python. Language is python. Salary is 12 lakhs.
As simple as that. Very good. So we saw.
How to make methods. Whatever method we make. We have to give a self.
Whether we use it or not. This is important. Whether we use it or not.
Okay. If I make a function called greet here. And.
If I write this here. Then I have to give self. If I will not give self here.
And I directly say here. Harry.greet. I will show you.
If I say here. Harry.greet. Then what will happen.
He will say. He is saying. Employee.greet takes zero positional arguments.
But one was given. This happened because. Employee.greet.
Which is. Harry.greet. Which is.
Employee.greet. Converted to harry. Okay.
So he is saying. You gave him harry. He was not taking the argument.
So we have to make it. Something like this. Then it will work for us.
Now see. He is doing good morning. And it makes sense.
To put the good morning. Before getting phone. Okay.
Great. It's fun. It's fun.
Things are understood. Clarity is coming. Okay.
That is important. We have seen so far. Classes are blueprinted.
To make objects. Classes. There are class attributes.
Like language and salary. And we saw. Language and salary.
Like class attributes. In the same way. We can make instance attributes.
By putting a straight dot. If I write. Harry.language.
Is equal to javascript. Then an instance attribute. Will be made.
Which is. The property of an object named harry. Consider something like this.
You wrote something in your form. So if you have written something in your form. Then change the form of others.
so class attribute is that which is for every class means whatever object will be made from that class is for that but instance attribute is that which is property of a particular object
means you have written something in form of pen and it will not affect other forms because you have written in your form with pen you understood right, I hope you understood
let's move ahead, let's take things ahead we saw self, self means instance of the class it is automatically passed with a function call from an object
now see I have written self here, self is a convention self name is used, if you will make it as slf, no problem if you will write its name as kk, pp, rr, cr, nothing will happen
just that if you have written cr here, then you will do cr.language but self.language is a bit descriptive, that's why we use self I hope that makes sense
now let's talk about static method now we saw here, see we saw here that greet was a function
which was not doing anything, it was just saying good morning but I have passed the entire object now you will say that I had to pass the object
because there was an error with the positional argument, what can we do? we can do, if I want that I don't give self here means I don't pass such a big object
then I have to tell here that it doesn't take self means in this function, object doesn't work I don't need language or salary
I don't need anything from the object so I will mark greet as a static method by putting at the rate
this means that greet is a static method and it doesn't need an object and static method is a decorator
we will talk about decorator later but for now you understand that we have written at the rate static method by which we have marked it as a function
which doesn't take self means we don't need any property or anything from the object so static method, we saw what happens
now let's talk about init constructor init is a special method which is first run as soon as the object is created init is also called constructor
it takes a self argument and can take other arguments as well which you want so we understand constructor well and then we will wrap this chapter
I will take a lot of time to explain you constructor because it is very important now see the class we have made till now
we have made a class named employee we said ok, let's do harry is equal to employee then we printed harry.name
harry.salary and there was no name here so let's say I want to set the name
so we were setting harry.name like this so I want to set my name in an easier way
what will I do, how will I do what will I do instead of giving harry.name is equal to harry
like this I will make a function and I will name it init
and you see as soon as I hit enter it automatically completed the auto I will write it like this
and after writing this what will I do here I will print here
I am creating an object I just wrote here I am creating an object so as soon as I run this program
you see here there is no harry.salary because of which the error is coming
so what will we do harry is equal to employee I have done double y in harry.salary
by mistake now let's run this program and you see I am creating an object
and harry is coming 12 lakhs why is this harry coming 12 lakhs this harry is coming 12 lakhs because
I have printed here harry.name and harry.salary well this is understandable but why is this I am creating an object coming
I didn't call init I just made init I called getinfo
but I didn't called init init is a special method which is called dunder method
starting with underscore in python it is called dunder method so I will mark it here
as a dunder method which is automatically called dunder method is
that method which starts with double underscore and dunder method it is automatically called
as soon as an object is created means you write harry is equal to employee then dunder method will be called
if you write rohan is equal to employee then dunder method will be called now you can see one more time
I am creating an object ok so dunder method which is init will be called
all dunder methods will not be called init dunder method is called whenever you create a new object
only init is called ok so this init will be called when you will create an object
I hope I was able to clear things up I hope you understood
what happens is instead of harry.name it would have been better
if I would have said I want the name harry salary is 13 lakh
and my favorite language is javascript here python is written by default
but I want to do javascript I want to set instance attribute in javascript
so I can take these three arguments you can see I can do like this I can say name salary
language and I can say self.name
is equal to and I will write name then I will write self.salary
is equal to salary then I will write self.language is equal to
language ok so I wrote name, salary and language
these three instance attributes will be set so if I pass here harry
13 lakh and javascript then harry.name will be set whatever I have passed
harry.salary will be saved means instance attribute will come whatever I have passed
harry.language will come whatever I have passed if I have passed language then language will come
if I have passed javascript then javascript will come and it will come here if I have passed salary then salary will come
and it will come here if I have passed harry then harry will come in name and it will come here it is clear
we can understand things very easily so I will run this and show you rohan is equal to employee
because we have to pass three things so I will comment this and I will run it again so you can see
so once you have done like this so you have to pass these three values
whenever you will make an object so you have run this so you can see I am creating an object
which I was printing and harry and 13 lakh came 12 lakh did not come
if I print here harry.language so you can see harry will come 13 lakh will come and after this language
will come javascript so here harry came here 13 lakh came and here javascript came
so these three here harry came 13 lakh came and here javascript came
I hope that makes sense so yeah that was about constructors in python
now we will see the practice set of this chapter and I will see you there chapter 10 practice set's first question says
create a class programmer for storing information of few programmers working at microsoft
so you have to make a class named programmer and after that you have to store information of programmers
working for microsoft so here I will make a folder named chapter 10 ps
in this we will right click and open with code and after opening with code we will make a file
01 underscore problem1.py and after this
we will make a class programmer class programmer
and after this we have to store information of programmers working at microsoft so you know how I will do this problem
I will first write company is equal to microsoft which will be a class attribute
because every programmer works here in microsoft and because every programmer is working in microsoft
made from this particular class so I will make it a class attribute after this I will
put a constructor and I will say def init that
in this name salary
and pin code I will store these three things
so I will write self.name is equal to name and after this
self.salary is equal to salary then self.pin is equal to pin
after this I will write p is equal to programmer and I will put
harry and after this I will put 12 lakh salary and my pin code will be
245001 245001 so I have a
programmer if I print here p.name p.salary, p.pin
and p.company then what will be printed all information about me will be printed
company will be printed pin code will be printed harry will be printed
whatever money is coming will be printed after this if I write rohan and I will write
rohan, I will copy this and I will write r r r
r r and here rohan and after this I will run it
and you see rohan 12 lakh and microsoft came so we have made a class
in which we are storing programmers working at microsoft after this we have to make a class
capable of finding square cube and square root of a number so we will make a class called calculator so I will close
our chapter 10 vscode instance and here I will make 02 underscore problem
2.py what is problem number 2 saying problem number 2 is asking us
to make a class calculator and what we have to do in this class that
we have to make it such that it is capable of finding square cube and square root of a number
so what I will do first I will make a constructor and
in this constructor whatever number is there I will store it so I will say n, whatever number is there
give its value and I will say self dot n is equal to
n and after doing this I will write
a is equal to calculator let's say I make a class calculator and after this I make
n or 45 or let's take a small number 4 and after writing 4 what I will do
here I will make a function and I will say def square
self and I am not returning it, I am just printing it print
n star n the square is n star n
so I used this f string and printed it the square is n self dot n
into self dot n and here also n will come so if I do self dot n then I will get
value of n whatever I gave and after this if I do a dot square then here square of 4
which is 16 will be printed the square is 16 and you can do this with any number
here we talked about square cube and square root so we will make remaining functions
after square we had cube and after cube we had square root in cube we will write
self dot n this is cube and here I will do square root
and how will I get square root now you will say how will you get square root
so I will do self dot n star star 1 by 2 so square root will come
so I will show you what will happen if I do a dot square and what will happen if I do a dot cube
and what will happen if I do a dot square root square root and here
cube and if I run this see 16 64 and 2.0
came which means that we have made calculator which is capable of
squaring, cubing and square rooting question number 3 is create a class with a class attribute a
so we have to make a class with a class attribute a create an object from it and set a directly using object dot
a is equal to 0 so does this change the class attribute so the question is very simple
and what we discussed already so what I will do here problem
3.py and here question question is
we have to make a class with a class attribute a so I will make a class with a class attribute
a suppose I name it demo a is equal to 4
then I make an object o is equal to and I write demo
then I write o dot a is equal to 1 question is
does this change the class attribute it is a very interesting question we have to make object dot a is equal to 0
so I will make it equal to 0 now if I print here o dot a
and I will print o dot a before this first it will be 4 then it will be 0
I can understand this but question has confused you are you getting my point
question has confused you that o dot a is being asked that it was 4 then it became 0
or class attribute is being asked you see class attribute is being asked you are being asked does this change the
class attribute the answer to this is you will feel yes but it is no class attribute has not changed
because if I instead of o dot a would have printed demo dot a
if I would have done something like this demo dot a if I would have printed demo dot a
then it would have been 4 which means class attribute has not changed but an instance attribute
has been set when I wrote here o dot a then it is printing
class attribute class attribute because instance attribute
is not present and because instance attribute was not present class attribute was printed
but later instance attribute is set and once
instance attribute is set then this instance attribute because interest
sorry, instance attribute is present understood the story, friends? prints the class attribute because instance attribute is not present
first of all instance attribute is checked is it a or not? it was not a, class attribute was a it was printed
after this we set the instance attribute
when we write o.a is equal to 0, then an instance attribute is set if we do it directly then when we print o.a, first we check the instance attribute
it was there, so 0 is printed but class attribute is also there it was not printed
and here prints the class
attribute so did the class attribute change? no, the answer is no
okay it's a very interesting question add a static method in problem 2 to greet the user with hello
a very simple question 04 underscore problem 4 dot py what is this?
in question number 2, we have to add a static method we have to add a static method and we have to write
hello word okay
so if I write here a dot hello and run it, then hello word will come
and we don't greet anyone like this hello I will write hello there, okay?
I am running it and you see, hello there came after writing why did it come after writing hello there?
because I have written a dot hello here so this static method worked why did I put static method decorator?
I put it to mark that I am not using self here because I don't need self I don't want to access any instance attribute of the object here
that's why I what did I do? that I removed self from here
cool amazing come on guys
this is also done next question write a class train which has methods to book a ticket
get status and get fair information of running trains under Indian railway a very dangerous question yet very easy because we will do it in a very simple way
so I will write here 05 underscore problem 5 dot py so we will make a class
train and after making a class train, what we will do is there will be a method book ticket
so def book I will write self here and I will write pass
okay after this what will happen? get status
and get fair information so one will be here def get status and here self
and I will write pass here and one will be get fair self
and here pass okay so I did this
added all three methods and now what we will do here is we will say that the get status takes a train number
takes a train number and what we will do here it also takes a train number
it will also take a train number, it will also take from where it has to go it will also take from and to so I will write fro
this from is already reserved in python whatever keywords are reserved in python like def, pass, class, from
because we use from to import I will tell you how to use from in the coming time but from is a reserved keyword in python
so we can't use it so I wrote fro and here wrote to and wrote pass
now what I will do here that I will write its logic so what I will do here for now
I will write print ticket is booked in train number and here I will give the same thing to this
because see when you are booking a ticket you will also give train number from or to
train number and here I will write an f string and I will print the train number and after this I will write
from and I will write fro fro and I will write to in to
okay so this train is booked and here I will give a random number in get fare
ticket fare fare in train number this
from ye to ye is now I will give random.randint from 0 to 555
in fact I do one thing from 2 to 2 I will give 5555
so we will import random import random okay by importing random
I can write random.randint I can do one more thing better than this from random import randint
okay import randint so I can use randint directly
so this is the use of from keyword I told you the use case and now you see here
get status if you do train number and I will do one thing here
I will print that this train this is running successfully train is running successfully
train number this is running successfully is running on time
I will write okay is running on time so now you can make any class
sorry object of train class you can also set train number in it as an instance attribute
there are tons of ways to solve this problem you can also do one thing that you give a constructor here
and write a train number because the property of train number it will be better if we give it like this
and I will write self.train number is equal to train number and it is not necessary that
these variables are same but we should keep same okay so I will capitalize this N
here also I have capitalized N and instead of saying train number here I can do self.train number
because train number is now part of self part of object
so wherever I am using train number I will do self.train number and I think this is a better way
to solve this problem because if class is train then its instance attribute will be train number
and whatever work will be done after that we will do through these methods so I will simply write here
t is equal to train of 12399 after this I will write t.book I will write here from Rampur
and then I will write Delhi okay now I will run this and you see here
it is coming ticket is booked in train number 12399 from Rampur to Delhi
so the way ticket is booked here in the same way we can check other methods also
so let's do it so I did book I am going to do get status
after this I am going to do get fare from and to is coming in get fare in get status
nothing is coming just train number is required I will run
and you see train number 12399 is running on time then ticket fare is 1833
which is okay actually it is expensive it depends on which sleeper
or third AC which coach's fare it depends on that
but anyways here you can see that we have come to next question
next question is can you change the self parameter inside a class or something else
say Harry try changing self to SLF or Harry and see the effects
so for this what I will do that I will use same program so we will make 06 underscore
problem 6 dot py and I will copy problem number 5 and paste it here
and the question is that you use self here if you use SLF
then what will happen I told you whether it will happen or not I want you to comment with timestamps
whether something will happen or not I want to know from you I want you to make two way interaction
you also talk to me in the comment section below what will happen if I do SLF will there be any problem
the answer is no if you are writing SLF here then here also you have to write SLF
and as soon as I run it here you see nothing happened here okay if I write self to harry
then what will happen if I write harry dot like this see nothing will happen
but it is not a good practice means why would you want to write harry in place of self
readability of the program will be spoiled but again your output will be intact
there will be no difference in your output it is very important for you to know this and with that said
about chapter 10 is wrapped I am really very happy with the pace we are going
and it will be a lot of fun in chapter number 11 see you all in chapter number 11
so guys today we are going to do chapter 11 which is inheritance and we will talk a little more
about oops so first of all I will make a folder for you of the name of chapter 11
and after doing shift right click I will open it with code and here I will tell you about inheritance
so what does inheritance mean inheritance means I explain to you what it is
suppose we have a class and that is employee and in that class
suppose we have a lot of information we have some methods
we have a show method which does what prints the employee
the name is self dot name or the salary is name dot salary
self dot salary like this and the salary is
self dot salary okay suppose we have a class
now I want to make a programmer class okay and I want the same show method in it plus some other methods
okay I want show methods and suppose there is a show language method okay
that will be something like this and I will print suppose
something like this that the name is whatever it is
self dot name and he is good
okay he is good with this self dot language
language okay suppose I add a new object attribute
which is also called instance attribute I added that so here it will come
and suppose I want to extend it so this method is looking very good okay what we did
we took all the methods of employee we took all the properties and suppose here
we keep one more thing that address or company company
suppose we keep ITC here now suppose I want that the programmer will be of ITC infotech
so what will I do here I say that what to do with the company
company is equal to ITC infotech now suppose I make one object A is equal to employee
okay and I make one object B is equal to
programmer okay programmer and if I print
A dot company and B dot company so you see that
once ITC will be printed and once ITC infotech will be printed because I changed the company in the other one
so if I want all the methods and instances of a particular class so one way is that
I can copy it okay now here there is only one method show
if there were 50 more methods like show so what would we do you will say let's copy paste
how much code is there let's select from here let's copy and make another class
but if I copy from one class I will make 10 classes
listen to me carefully I will copy different classes from one class so my biggest challenge is
if I have to change later then I will have to change in all those 10 classes and this process becomes very cumbersome
and also error prone means I can also make a mistake in making changes
for example if someone told me for some reason here the name is
make it name of the employee we have to write this so I will have to change it here too
because I want to keep this show method same I want to keep it inconsistent within classes so this method is not good
instead of doing this what I will do I will use inheritance
I will say see programmer is a class which I am inheriting from the employee means whatever was in the employee
all that is in the programmer there are many other things which I want to change
or I want to add for example if I do something like this
let's say here I will show you that what I did here
inherited the programmer from employee now if I run this
so you can see ITC ITC info tech is coming so basically whatever work I was doing in so many lines of code
I did it in so few lines of code plus if I want to change anything
I just have to do it in one place in the employee and the rest of the places will be updated okay, rest of the places will be updated
why will it be updated? because we are making programmer class from employee and we have made a base class
this is called base class and this is also called parent class and what is this called?
this is called inherited class okay so now I come here
and show you what is base class and derived class base class
means with which we are making and derived class means which is derived
inheritance is a way of creating a new class from an existing class what is syntax?
something like this we saw write employee in the bracket in programmer
derived class or child class will be made okay we can use the method and attributes
of employee and programmer object means all its methods and attributes will come here automatically
without doing anything means you said once make programmer from employee
so all its methods and attributes will come automatically in programmer and after that if you want to add anything else
then you can write here write code here like we added one more function here
show language show language will not be in employee but it will be in programmer
because I have made programmer from employee and I have said that along with all its methods and attributes
I also need a show language function in programmer so let me tell you about types of inheritance
one is single inheritance means from base class a simple derived class is made
it is simple multiple inheritance is there like your child class inherits
from more than one parents like suppose I want employee also comes in programmer
means all its methods and attributes will come in programmer and another class will also come
so let me show you its example here of multiple inheritance multiple inheritance.py
so let me show you here suppose I have an employee class and I have a coder class
coder can be any coder who does coding in his house okay
suppose I have a function in it and I have that function and suppose I have language in it
and suppose by default language is python for everyone okay for everyone by default language is python
and suppose I write a function here and I write print languages okay
and I write self here and I will print here and I will write here f
and I will write out of all the languages here is your language
okay suppose I have made a very simple self.language
I have written here simply that your language is this and now what I will do is
I will make programmer from both employee and coder so you see
when I am making from both employee and coder then I will use some such syntax it means in programmer
all methods and properties of employee and coder will also come so if I run it
then you see itc itc infotech is coming but along with that
I can print it here and do something like this, I can say v.show, I can run v.show, I can run v.showlanguage, I can run v.printlanguages because the programmer has all his own methods and properties, this is also called property by the way, if you don't know there are also for employee and coder, because we have made the programmer, inherited it from both employee and coder
so employee and coder are the father of the programmer, means you can call them mom and dad, so they are the parents and all their properties are inside the programmer, very good, let's run it and here it is saying programmer has no attribute name, now the problem is that there is no attribute name in programmer
because why did I write name in show language, this is my fault, I should have written company so I wrote company and language, company is here in show language, language is also here, why language is here because it is made by the coder, so now I will run it, so here it is saying self.name, where did I write name
so here also I will write language, I will do one thing, name of the employee self.name, here I will write name is equal to I will do one thing, I will write default name, actually there was no name, that's why I was showing error, now this will work so here I will run it, there is no salary in this, so I have to write company, I don't have to write anything
I have to write name, I have to write company, language, language is fine, language is here also, now it will work so you see here name of the employee is default name and salary is ITC input, I will write company, it will make sense I want to write sensible programs, so that you can infer those programs later, so you don't have any problem
so I wrote name of the employee, company, then out of all languages, here is your language python here your print languages function worked, then after this this function worked, show language, the name is ITC input and he is good with python language, so we can do like this, and I hope you understood how multiple inheritance works
then there is multi-level inheritance, that you made child from parent and made child from child1 and child2 like you made programmer from employee and made programmer manager from programmer like you made programmer from employee and made programmer manager from programmer
a manager who is programmer himself, employee is also there but he is also manager so you can do like this also, and syntax will be similar, so I will show you the syntax of multi-level inheritance so if I show you, 03 underscore multi-level underscore inheritance dot py
so you can see here, suppose we have class employee, and suppose it has pass, I will write just syntax and after this we have programmer and after this we have manager and what I can do, I can say that programmer came from employee and manager came from programmer
so what I can do here, I can write a is equal to 1, b is equal to 2 and c is equal to 3 if I make an object here, I will say o is equal to employee, so if I print o dot a, it will print and it will become 1 but if I do o dot b, it will show you error here, because b is not in employee
because b is in programmer, but if I make this thing from programmer and I will write print o dot a and I will write print the a attribute and after this shows an error as there is no a, no b, attribute in employee class
after this what we will do, we will write o is equal to, suppose I have already written a, so I will write o is equal to and I will write programmer and I will write print o dot a, o dot b, I can only do this if I do o dot c, there will be an error, if I do o dot a, o dot b, now it is saying that employee object has no attribute b
so I have to comment this out because it is showing an error so you see here, it is showing 1, 1 and 2 now if I make this thing from manager, so I can do o dot a, o dot b and o dot c
because all three are in this, you can see 1, 1, 2, 1, 2, 3 and you can put methods in this and make it more advanced, see the use case explore things by yourself, so these were our three types of inheritance
now we will talk about super method, what is super method? whenever we call a constructor, I will show you here if I write def init here and I print here and I write constructor of employee
and let's do one thing, let's make a new file and keep things organized like we have kept till now, so I will do one thing, I will make super dot py here I am telling you super keyword and why not make a copy of it and don't disturb this program
just checking this program is running because I have to give this code to you later and you have to learn a lot from this, so you see if I have made a constructor here so I have said that this is constructor of employee, I will copy it
then I will paste it here and here and I will write programmer here and I will write manager here, now see when I am making employee object I will erase this, when I am making employee object then what will run?
then constructor of employee will run, now see when I am making programmer object then what will happen? then programmer constructor will run and when I am making manager object then what will happen?
then manager constructor will run, but sometimes we want that along with manager constructor, these two constructors will also run so what will we do? suppose I want that whenever manager runs
then manager constructor will run but its parent constructor will also run means programmer constructor will also run, so for that we will use super and we will write super dot init, super means super class means
whatever function of programmer class you have to call and because I have to call its constructor, I will write super dot init and as soon as I will write this, you see here
programmer will also run, constructor and manager will also run both are running, so basically when this constructor will run then it will run its parent constructor and after that it will print
constructor of manager, I hope that makes sense good, amazing, its fun, so this is super keyword nothing, just if you want to access any method or property of super class
in derived class then you can do it, good, its fun now understand this concept carefully, concept of class method concept of property decorators, concept of getter and setter
what is class method? I will make a new file here 05 underscore class underscore methods dot py class method basically is a way
to directly access class in a method suppose I make an employee here and I say a is equal to 1 then I say show and then I say
print and I put f string, the value of a is and here I write self dot a and I will write self, now you see one thing here
that the class value of a, self dot a I have done here so you see if I say e is equal to employee, understand my words very carefully and I say e dot a is equal to 45 and after this
suppose I say e dot show, so you tell me will it show 1 or 45 tell me in the comment section, I need two way interaction this time what will it show, it will show 45 or
it will show 1, what will it show, so here you see if I run it, it will show 45 because whenever we write self dot a it shows instance attribute, I want it to show class value
because I have written here class attribute so if I run it, it will obviously show 45, we all know that but I want class attribute here, so if you want class attribute
so what you do, you put a decorator class method and instead of saying self, you write cls so whenever you put a decorator of class method, like static method
what will happen by putting class method decorator your cls will be accessed and it will show class attribute, it will not show instance attribute, so you see if you run it
so it is showing 1, this is class method, directly you can use class attributes so your class method uses cls instead of self
means self means the object on which method is running and cls means the class on which method is running listen again if you don't understand, now I will come to property
decorators and getter and setter property decorators are very simple, I will write here 06 underscore property decorators dot py
what are property decorators, suppose I have a class employee and in this class there is a class method, I want I can set name directly, I can say
e dot name is equal to harry and I can do something like this, print e dot name and what I want to do by doing this
I want to set my property directly by doing this so what I can do, I can make a method and I will tell you its use case first understand what is syntax, what happens by putting property decorator
then I will tell you why we do this, now we will understand syntax I wrote here def name and then I will return self dot name
if I do e dot name is equal to harry and I want to keep my instance property in ename so I put a property decorator and after that I can do something like this
I will organize this code, by the way you will get the code very easily so don't worry, now if I say e dot name is equal to harry and print e dot name, then you see what happens
class attribute of a is 1 and if I print e dot name, then you see harry is printed, now you will say what was this
we can print e dot name suppose I write here e dot name is equal to harry khan I wrote here harry khan and what I want to do
I want two different instance attributes to be set one is my first name so I can say self dot f name is equal to
and I can say here value dot split and by space and its zero now you will say what is this, I know that
and first name and last name, so what we can do when we run dot split function on any string, I want to take you to because I want to keep your understanding
suppose I have a string which is harry khan if I do a dot split by space, then it will split the strings in such a way
that there is a space and it will make a list so there was a space after harry, which I gave after split so it made a list after harry
so what I can do, I can say the first element of the list the zero index element, set it as first name and last name so first name harry and last name khan
and here I don't want people to know that I am saving first name and last name for some reason, there can be multiple reasons one reason can be to give convenience to the user to write his full name
I don't want to write first name and last name separately, I will convert it here I have written e.name is equal to harry.khan so if I access e.name, which is not an actual property
I am just showing it here, I will do self.fname and here let's do one thing, let's return an f string and I want you to understand
and here will come self.lname and let's return an f string so as soon as I run it, I have set it as harry khan and here you can see harry khan is printed
what will happen if I print fname and lname instead of name if I print fname and lname, then both will be printed, harry and khan but name is not an instance attribute
but I have made an instance attribute which I am making by combining existing instance attributes so I used two decorators one property and one setter
name is a function which will work but make it feel like it is a property that user just printed .name and you can keep the return value
which it was doing when I was printing e.name and when I do e.name is equal to something, then take that value and set first name and last name so that user doesn't know how much hard work is being done
and here we are doing so much work so this is the concept of abstraction and encapsulation encapsulation means that you can't see the implementation details
implementation details are encapsulated and abstraction means that we have hidden the implementation details and encapsulation means that we have packed many working components
in a particular unit which is class so I hope you understood what is property decorator so now we will move to our next concept which is operator overloading
and after that we will wrap this chapter so what is operator overloading, I will make a file here and name it operatoroverloading.py
so here we have a class which is number but in this
in the class a is equal to 1 b is equal to 2
so there is a number with two class attributes one is 1 and another is 2 what I can do is
I can make a constructor here instead of giving these numbers and I will say that in this
self.n is equal to n and put a comma here and end it, something like this
so def init and here self.n
and we will do n is equal to 1 and make m
number 2 now if I write print n plus m
let me show you print n plus m there will be an error
that I don't know what is plus tell me what is plus in python everything is class
integer is also class all types are class class int, class list, class tuple
and if we want to customize
the operation on two operands we can overload these methods
I will underscore underscore add which is a function
add we can write self and num and I can say
return self.n plus num and if I do n plus m
I will get this unsupported operand for plus int and number
and here instead of num I will do num.n
because the number I am passing is a number so self.n plus num.n and if I run it, you can see 3
so what I can do operators plus, minus, star, divide I can define
add, multiply by writing these functions like mul
which is a function subtract true div
floor div when we do floor division so operators in python can be overloaded
using following methods we also call underscore underscore methods as dunder methods we also have
string underscore underscore here it will be underscore underscore so it starts with underscore underscore
I will fix this typo underscore underscore str underscore underscore a string method which is used to set
what gets displayed upon calling a string, means if I print then what will happen
or if I convert an object in str then what will happen I hope you understood this
then underscore underscore len underscore this defines what will happen if I
try to calculate its length using the len function, so if you run
len function on any object then what will happen is defined by this underscore underscore len underscore underscore, I hope that makes sense
and now we will move to chapter 11 practice set so see you in chapter 11 practice set
so guys it is time to do chapter 11 practice set so I simply make a folder for chapter 11 practice set
and I will close this python folder shift right click and open with code
first question we will solve problem1.py and first problem is
that we have to make a class named 2d vector and after that we have to create
a class which represents 3d vector, so here I will make a class
2d vector 2d vector and what I will do is
after this I will make a class of 3d vector, so I will use a constructor in this
and after using constructor what I will do is I will write here
self dot i is equal to i self dot j
is equal to j and after self I will write i and j
and after this I will make a 3d vector so what I will do is 3d vector
3d vector i j and k will come in this and this time what I will do is
I will write k in this what I will do in this is in i j and k
I will set only k and you will say why are you setting only k and not i and j
and the reason will be that I will use super keyword super dot underscore underscore init
i and j, so what will happen self dot i and self dot j will be set because this function will run
so I am saying that when you will do 3d vector like this so what should happen is
super dot init should be called first so that i and j will be set and after that we will set k I hope that makes sense
let's make their objects so here I will make o is equal to 2d vector
and I will give 1 and 2 then I will do one thing
let's make it m and n b is equal to 3d vector and here
1,2,3 and let's write a show method I will write def show
self and I will simply print
d vector is and I will make an f string
and I will write self dot i i then I will write plus
then I will write j and k so here I wrote i and then I will write j and I will write plus and then k
so here write j j and then write k great
so actually there is no need to write k because k is not there so I will do one thing, I will make
show method of this and show method of this that I have done this and here I have to
remove k so if I run a.show a.show and
b.show so you see here that I will run this so here an error is coming
and it says takes exactly one argument and here I have done some problem it says in it takes
only one argument so here the mistake I have done is that I have not made 3d vector
with 2d vector I made this mistake now I will run it and you see
1i plus 2j and 1i plus 2j plus 3k if I make it 5 2 3
so it is showing 5 2 3 like this 1i 2j and then 5 2 and 3
amazing, let's move to the next question which is make a class named pets and then further make a dog
from pets so there will be an animal, then pet and then dog then we have to add bark method in dog
so for this I will write 02 underscore problem 2 dot py and first of all I have to make a class
animal so I will make a class named animal we should always keep the class singular
it's always a good practice to keep the class singular so I will write animals only because my problem statement is saying so I have to make a class named animals
and after making a class named animals I have to make a class named pets so for now I will write pass in this and then I will make
pets, I will write pass in this then I will make a class a dog which I will inherit
so I will inherit pets from animals, I have told you that the class name should always be singular
but if your problem statement is saying to keep plural you should keep it whatever your problem statement is saying
you should do it, so I have written pass here we know pass keyword, if we want to do something later, if we don't want to do it now
then we use pass keyword so you see here pets from class animals and further
class dogs dog from class pets, dog is singular so I have written dog
then we have to add a bark method in dog, so I will write def bark
I will write a self and I will make a static method why I will make static method?
I am not complicating things I don't want self here so what I will do is
I will write wow wow and this is a static method I have put a decorator here
so if I write here d is equal to dog and I write d.bark and I run it
so you see wow wow is coming here it was a simple question we didn't put any implementation details
in pets and animals because our question didn't say and neither it was necessary now we have to make a class employee
and add salary and increment properties to it, so let's make class employee and let's make salary
and then we will put increment properties in it, so what I will do is 03 underscore
problem3.py so first of all class employee and pass
now what we have to do is I will make an object of this employee and what I will do is
I will set its salary salary is equal to let's say 234 rupees per day
and add salary and increment properties to it and I will put one increment property
there are two ways to put increment property if I increment is equal to one or let's say
I give 80% increment it will be too much, let's keep 20% so I can put something like this
so if I want to make it as a class property then I will write it here but if I want to make it as an instant attribute then
what I will write I will remove it so I will write e.
so it was a simple question after this we have to make a salary after increment method property decorator with a setter
which changes the value of increment based on the salary, now this question is interesting, salary after
increment will be a method in which a property decorator will be there, so I will do one thing
I will make a property here, so I will say salary after increment and I will make it a property
I will make it at the rate property we already read about property decorators
what did we read about property decorators if you write at the rate property then you can return anything
so what I do here I can return salary plus salary
multiplied by increment increment
divided by 100 understand it like this and yes I have to write self.increment
I have to write self.salary I will do all that, there is no tension so this salary here
I will write self. ok I will write self. and here also I have to
write self. and now if I write dot salary after increment suppose I
write salary after increment, so you see I will run this, so it is showing me
salary after increment, ok very good, now along with this we have to put a getter
let's see, if the question is saying then we will put and we have to put a setter value of, changes the value of increment
based on the salary so if you say that salary after increment is 500
so what is the increment, so we have to change the value of what, we have to change the value of increment
so I will simply write here at the rate increment.setter and I will write here
def.increment self and as I have already told you
that you can change any property here and after this you will get a value value and what you have
is that here based on salary we have to change
let me read the question once again so what we have to do, change the value of increment based on the salary, so this value
is our salary, ok now increment will be how much, increment will be how much if our
new salary is this much, so increment will be how much, see now what happens if I take you on maths
new salary is equal to how much new salary is equal to old salary is equal to old salary
plus 1 plus increment, ok
divided by 100 now if I try to get old salary from this formula
so what I will do, new salary divided by old salary minus 1 into 100, ok, new salary
divided by old salary minus 1 into 100, now try to do maths it will be equal to
increment, ok, and here 100 will come, whole into 100, ok, what will
our increment be, so I will take the value of this increment and I will set the value of this increment
so what I will do in return, I will do self.increment, self.increment is equal to
new salary which is our salary and divided by old salary which is self.salary
and I will do minus 1 self.salary divided by minus 1, I will take this in bracket
and into 100 ok, so let's see that I have done e.salary.increment
280.8, ok so e.salary.afterincrement is equal to this and I will
increment it after salary, I will increment after salary so if my e.salary.after
increment, if my salary after increment is 280 so I have to
calculate how much increment I have, so if I print here, if I print here e.increment
so you can see that my increment is 19.65%, if I do 280.8, then
my exact will come 19.99, no 20% increment came, so basically
I have solved this question, I would like to explain one more thing to you, I have put a property decorator, what I did, first of all
I made a property named salary after increment so that I can directly dot it and get from here, ok, so
what will it print, it will print salary after increment, this is my salary after increment, then if I give it salary
after increment, then it will change increment which I showed you e.salary.afterincrement
is equal to this, then we printed e.increment because it changed increment property
ok, I hope that makes sense and now we will come to the next question which is write a class
complex to represent complex numbers along with overloaded operators plus and star which adds and multiplies
them, ok, let's do it, so what I will do here 04 underscore problem
4 dot py and here what I will do I will make a class complex
and here what I will do I will make a constructor now you must be thinking that why I am removing this none
I will tell you about these types in some time that's why I am removing it and these are optional so that you know
I will tell you what it means in coming chapters but what i will do here is i will do r and i
in a complex number there is a real part and an imaginary part self.i is equal to i
if you haven't read about complex numbers then let me tell you that it is like a vector it is not exactly a vector
but very similar to vectors in which there is a real part and an imaginary part and they are added separately
so if i put a dender method here with the name __sum and if i give here self and
__c2 then i will return a complex number
which will be a complex number i will return it and it will be
self.r plus self.r plus c2.r
both the real parts will be added and self.imaginary plus c2.imaginary
c2.imaginary this is our sum formula now what we will do here
we will make two complex numbers c1 is equal to complex
complex 12 and c2 will be complex 34
ok 34 we can also do 5 but 34 ok
and after this what we will do we will print c1
plus c2 and after this i have printed
c1 plus c2 unsupported operands plus for type
complex and complex what is going on i can't add c2 in c1
c1 is a complex number c2 is a complex number i can't add it
so this is my mistake i hope this works and see here it gave me a complex object
now see here str function game it is telling me something like this
complex number is not showing so whenever we want it to show
things like this i will use str method str
and what i will do here i will remove str from here because i will tell you about types later
and i will return here an f string in which i will give self.r
and after this i will write here self.imaginary
i and after this i this is how complex numbers are represented so i did self.r
and plus self.imaginary i and see here 4 plus 6 i this was 1 plus 2 i, this was 3 plus
4 i, so 3 plus 1 is 4 and 4 plus 2 is 6 i hope you understood this
and same thing we can do for multiply and for multiply we have to find formula in complex number
so what happens in complex number there is a formula for multiply and to derive that formula
i will not be able to derive here i will give a formula for multiplication of two complex numbers formula to multiply
two complex numbers and it will tell me and i will use that formula
now it is telling me that a plus bi and c plus di are two complex numbers
this is the formula now i will say i will not say anything
i will come here what i did my self.r is
my first complex number and my c2.r is my second complex number
so i can say chat gpt just to save some time i will say fix this formula
in the class complex i will give this code
and it will fix this code and this is how you use ai to your advantage i will do same thing step by step
i will do it with ai so see so real part
and imaginary part we have derived here so if i will multiply
if i say print and i say c1 into c2 so it will multiply
see minus 5 plus 10 will come so i can ask
what is 1 plus 2i and 3 plus 4i what is 1 plus 2i multiplied
by 3 plus 4i it will tell me
apply this formula and my answer should be minus 5 plus 10i
it should be so minus 5 plus 10i is coming if you don't know complex number
but again you should know how to do this now we have to make a class vector
representing a vector of n dimensions we have to overload plus operators and star operators
which calculates sum and dot product of the vectors so to sum vectors
you have to sum both and what is dot product formula in dot product
you basically multiply i,j and k values so i will make a file
05 underscore problem 5 dot py and i will make a class a vector
so i have taken a class vector i have initialized it and i have made an add method
which is adding individual elements and for multiplying i have multiplied and sum
and to represent them in a string i have written something like this so let's test its implementation
and see what we have done sum is coming 32 is coming
8,10,12 and 50 amazing let's move on to the next question
now we have to write an str method to print the vector as follows assume vector of dimension 3
for this problem so we will take the same question and i will simply change str method
problem 6 dot py and you can see i have written something like this
but now i will change this f string and by changing this f string here i
then i will put this bracket then here i will write j and here i will write
k and now i will print and show you
we need plus i will write it correctly so that there are no errors
so 5, i, 7, j, 9 you can see something like this and we have assumed vector of dimension 3
here now we have to overwrite len method on vector of problem 5
to display the dimension of the vector so if i do length of any vector
let me show you problem 7 7
7 dot py and i take this
and i made a vector if i take print len of v1 then what will happen
if i do len of v1 then it will ask me what is len, i don't know what is vector len so for this i will define
a len function which will take self and i will
return 3 so here i will
return 3 vector of problem 5 to display the dimension of the vector now here if my variables
if length of my vector was variable then what i would have done is i would have passed a list
like this 1, 2, 3, this is 3 dimension vector so what i will do here
i will take list then i will say self.x, self.y, self.z
is equal to what will happen here self.z is equal to list
let's call this list as l and after doing this our x, y and z will be set
so x, y and z are set here and for length
i will simply say return and here instead of x, y, z
i will set this l self.l is equal to l so that i can get len of l
so i will remove all these methods and i will simply return
len of self.l and let's see if it is printing 3 and yes it is printing 3
so multiple solutions can be a problem i hope you understood this question and you got clarity on how these things work
i hope i could make things easy for you now we will move ahead in this python course
see you there so now we will talk about our project 2 which is the perfect guess
what is this game let me tell you we have to write a program
that generates a random number and ask the user to guess it so a random number will be generated
if you have entered the number you have told it correctly the number of guesses
you have told is good means the less number of guesses you have told is good
and lower number please, higher number please like a number between 1 to 100
you have to guess so you said 72 now if you type 42
it will say higher number please if you type 79 it will say lower number please
until you guess the correct number and the more guesses you do the better
so here what we have to do when the user guesses the correct number the program displays the number of guesses
the player used to arrive at a number then another person will play and the one who has less number of guesses will win
so let's do this so what I will do here, I will make a folder named project 2 so project 2
and here I will simply open with code and here I will make a file named main.py
main.py now here I will show you one thing
now what I will do here, I will say guess a number and I will write
a is equal to int of input and after this I will write guess a number
guess a number is a simple thing after writing guess a number you will guess a number
and I will use random module and I will generate a random integer I will say n is equal to
random dot randint and it will be between 1 to 100
so between 1 to 100 you will guess a number and I will write guess the number
and I will put this thing in while loop while condition will be a is not equal
to n I will fine tune this more but for now I will say
a is equal to int of input guess the number so I will make a equal to minus 1
because my n number will never be minus 1 so this condition will always be true when I will do a is equal to minus 1
now what I am saying while a is not equal to n what we have to do
we have to guess a now what we have to do if a is equal to n
then it will automatically break and what we will do guess is equal to
zero and guess is plus equal to 1 now if
your number if a is greater than n then you will print
lower number please lower number please and what you will do
you will say higher number please higher number
please if a is greater than lower number please
and if a is small higher number please because a is smaller than n
give me a bigger number then what will happen when you guessed the number correctly
then it will say that you guessed the number so here finally what will happen when this loop breaks
then I will print you have guessed the number correctly
in guesses attempt and I will make an f string so here we have guessed
with so many attempts so I will say in guesses attempt now I will run it
guess the number higher number please higher number
higher number higher number higher number
lower number 86 higher number
88 89 higher number
90 93 99
lower number 98 lower number 95 higher number 96
you guessed the correct number in 15 attempts here I will say correct number
and I will write number here I will write n here which is our actual number
so let's play this game again guess the number higher number
lower number lower number lower number
lower number higher number 7 8 9
you have guessed the number 9 correctly in 12 attempts so it is telling me how many attempts I guessed
so in the same way we will do now here you can see in our guesses it will come
a is not equal to n then finally if our guess is correct
let's say we guessed correctly then what it will do it will print something
and make us out of the loop but I don't want that finally when our number is equal
higher number please print so I will say if a is less than n
if a is equal to n then nothing will happen and my guesses plus equals to 1
I will do it at the end one guess will be there so as the loop will go on
the guesses will increase and finally what will happen
your number of guesses will finally be reported now what I will do
the guesses I have written I will put it in the elif because I don't want
guesses value plus equals to 1 in any other case guesses plus equals to 1
guesses plus equals to 1 you have guessed the number n correctly in guesses attempts
I hope you liked this project now you must play this with your friends it's a very simple game
I do 33, 44, 55 I do 66, I do 50 higher number is saying 52
I do 53, I do 54 I do 55, I do 57 I did 57, I did 59
I do 61, 61 lower number is saying 60 and he said you guessed it in 16 attempts so yeah that was about project number 2
now we will move ahead in this course and I will meet you in the next chapter so today we will talk about some of the newly added features in python these are the features that make this course exclusive
I will tell you which features you can use at any time to make your python programs efficient and strong so let's start with the first feature which is valrus operator
so what we will do is I will close this and I will come to chapter 12 so I will make a folder in the name of chapter 12
and I will open this in VS code and our first program will be 01 underscore valrus.py 01 underscore valrus.py
01 underscore valrus.py I will tell you the usage of valrus operator so there is an operator called valrus operator
which was introduced in python 3.8 onwards and it allows you to assign values to variables as part of an expression
your code will be part of an expression and it will do two things one is you can use your expression
plus you can assign it to a variable so I will copy this code and I will paste it here
and I will format it properly for some reason formatting is not done properly and you can see here I will comment it
so we wrote n valrus operator and len of this list is greater than 3 so I can do two things in one statement
I don't need to write n is equal to so I did it in one line and this is basically a convenience method
to do two things in one line of code so if I run this list is too long, five elements expected
so this will be evaluated in true plus the value of n will be assigned to len of this list so this is a very simple operator
modern programs use this a lot type definition is a very important topic and type definition is something you should know
and it makes your work very easy now suppose I make a variable a variable whose name is n
and I write n is equal to 5 now if I write n dot suppose I write n dot is
suppose I write n dot if it is an integer then I will get all methods of integer
so I can explicitly tell python that my n is an integer and if I write n dot then I will get all methods of integer
so I told python that n is an integer was it necessary to write this colon int, no it was not necessary
so basically I marked n as a string if I want to mark n as an integer and if I want to mark name as a string
then I can do something like this I will put colon here and write Harry now what will happen in functions
how can we do this in functions suppose I am writing a function called sum which takes a and b
and it returns an int sum takes a as an integer b as an integer and it returns an integer
so I can write return a plus b so I can do like this and if I do like this
suppose I will write sum and it is telling me to give a an integer b an integer and it will return an integer
so what happens when programmer is coding or your code is being used by someone else then it will be easy to identify
what function is returning and what type of values are being passed so basically you are telling types
so this feature has been recently added in python programming so I have written everything in handbook now we have advanced type hints
which you get from typing module from typing you can import
you can import list you can import advanced like you can import
union type you can import dict and tuple and what is the benefit of importing
you can write list of int like you can see I am doing union
I have done tuple now what happens you can say
these numbers are list of integers so you have written list of integers then you can say
it is tuple of string and int there are two elements one is string and one is integer
then you are making a dictionary then you can use union what is union
it can be integer or string these annotations help in making the code self-documenting and allows developers to understand the data structures
used at a glance you can use union now I will tell you about match case
introduced in python from 3.10 it is similar to switch statement if you have read in C language
I will tell you how it works I will write match case I will bring a simple match case statement
let's do it from the code itself press ctrl c let me show you
how match case works let me format it it is not coming from pdf
let me show you let me format it and here
we have to format it what will happen we are making a function
if the value is 200 it will be executed if it is 500
it will be executed let me print it if I write
http status 200 it will print ok if I write 404
it will print not found if I write 500 it will print internal server error
it will print internal server error let me see I have removed the return
we have to return it will print internal server error if I write a number which is not found
it will print unknown status let's see let me remove the output
I have to return I have removed the return it has written unknown status
I hope you understood how match case works it is straightforward and easy these were some topics
there is one more update you can update you can merge two dictionaries
you can merge two dictionaries you can merge two dictionaries you can merge two dictionaries
you can use multiple context managers you can use multiple context managers you can use multiple context managers
you can use multiple context managers and here and here
and here and here and here
here here here
here here here
here here here
here here here
enter a part
here enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
part enter a
but I don't want zero division error so what I will say here is if b is equal to zero I will not perform division
I will not let the user go ahead I will say raise value error I will say raise zero division error
and I will say hey our program is not meant to divide numbers by zero
zero we can do something like this otherwise we will say else
print this run it put any number
if you put this as the first number then put something else then nothing will happen
it will do the division but where you put the first number and put the second number zero
this is giving you your custom error we raised this error means we can raise error in our program
now why do you want to raise error in your program you want to raise error in your program like you are making a module in python
which people will import so you don't want anyone to make a mistake so you will stop it there
you will give error message there you will crash the program there if you raise then the program will crash
but we talked a while ago that program should not crash but sometimes program should crash
why should it happen if your developer is making a mistake then he should know
that he has made a very critical mistake then his program will crash then only he will fix his code
so that's why we are saying if b value is zero then this is a very critical error
b value should not be zero but it is zero so we will have to raise the error
means you understand something like this that you should not kill any child but if he is doing evil
then kill him so that he improves so that's why we raise errors to improve the developer we will say raise the error
so that the developer knows that he is making a mistake I hope that makes sense
and it does so this was raising exceptions which you can do with the help of raise keyword
now we will talk about try with else or try with finally what is finally
and when do we use else with try so let's talk about try with else so I will do try else here
and suppose I have a block of try catch now what I will do here that my block of try catch is here
so I have a block of try catch try except it's not catch, it's except
except except in python catch in cc plus one
so people get confused they say try catch even catch in javascript
so people get confused so you see try a is equal to this print a
except value error very good now what I will do
I will remove this I accepted now here I will say
else and I will write here I am inside else
and remove this star ok so here I am printing
I am inside else now you see if I run this I will enter a number
and suppose here I did 45 so I am inside else came if try block is successfully run
then it goes inside else otherwise if I wrote anything here and error came
then it doesn't go inside else so if we are using try with else then it will go inside else
only when you run this successfully if all this is successfully run
then it will go inside else otherwise it will not go inside else now we will talk about finally
so you understood about else when it will go inside else sometimes we want to run a piece of code
when try was successful if try was successful then it will run this code
so this tells that try was successful so it will go inside else
when try was successful ok now if it goes in accept
then also it will run if I didn't put else so that's why I had to put else
so that it will run only when some piece of code was successful ok
now understanding finally and understanding finally is little tricky for people
but I will make it easy for you I will make it very easy for you so I take this try catch block
take try accept block and what I will do here I will put finally
and I will say print hey I am inside of finally ok
very good I will run this and as soon as I run this
I wrote 45 hey I am inside of finally so what people say
I have seen instructors explaining like this that finally block works
whether you go in try or accept it works when I learned finally
when I was told that finally block works whether you go in try or accept
so I used to say it works in accept also let's try
if successfully run code is done then also it is working if successfully code is not done
then also it is working so write it like this I used to think like this
then I will tell you why you can't write like this what is the work of finally
I said write like this what is the problem and when I asked
one of my teacher he didn't tell me he couldn't tell me
he said don't ask stupid questions he said like this
but I say ask questions if I don't know
then I will tell that I don't know this every person doesn't know everything
in this world remember finally print
I did this then also it is working and if I do like this
then also this behavior will be same you see if I give a valid number here
then also finally is coming and if I give invalid then also finally is coming
means if I give invalid then also here finally is coming
I will show you here if I give invalid
then also finally is coming so what is the use of finally use of finally comes
in function when you write a function by def
and you call that function and you what you have to do
whether it is try or accept if you return in try ok
if you return in try then finally won't work means if you don't put finally
if you write like this then also this code won't work but if you put finally
then also it will work I will show you if I return this
return this also so see if I
what should I do write a number then see
hey I am inside finally if I write anything here then hey I am inside finally
but if this is not finally if this is not finally
then you see if I am running this program I wrote a valid number
then also it didn't work finally and I wrote an invalid number here
then also it didn't work finally understand so finally means
that it will definitely work even if the function returns see we saw
return means that function returns now the code below this
won't work in function finally overwrites this thing and says
I am finally of this try accept I will definitely run because this is my duty
this is my duty I have to do this so what does finally do
finally's duty is to run and it runs breaking all the rules
remember this thing like this ok so from the example of this function
we saw why do we write finally like this why can't we write directly
finally below so finally block keeps running
whether you go in try or go in accept but you should also understand this thing
you should also understand this thing and I hope I was able to explain to you so you guys try some code
little by little and I will also like when you try out
the code now I would like to talk to you that if name equals to
equals to main what happens in python so to explain this thing
I will make a file and I will name it main.py
now suppose I have a file named module.py and I have made a function in this
and its name is myfunc and it is not doing anything
just a very simple hello world hello world
ok I have done hello world in this now if I import this
and I say from module import and myfunc
ok if I just do this and run this file
what will happen so nothing will happen because I have not run myfunc
in module but if I run myfunc in module
so what will happen basically if I run this
so this hello world will print here ok
now what does this mean it will print this hello world see this hello world
will print because because when I said from my module
myfunc from module import myfunc
means from module import myfunc then it means
that it will take this source code and put it here
literally something like this and my func
will be printed here so what happens is
that in any module if I just print here
underscore underscore name which will print here
and give you what is this now if I write here
print underscore underscore name and I run this so it is giving me
underscore underscore main which means that I have
run this module code from module
file but if I run this
eight underscore main dot py so underscore underscore
name will run module this time which means
it has been imported so its name is telling me
so what does underscore underscore name tell
main tells if you have run this file
which means the file in which you have written
print underscore underscore name underscore underscore if you are running
so you will get underscore underscore main underscore underscore
to see but if you are importing
this code somewhere else so you will get the name of that file
from where you have imported I hope you understood
like if I run zero eight underscore main dot py
so I will get module printed because I have
imported this from module dot py but if I run
module only so underscore underscore name will
print me underscore underscore main
I hope you got it and I hope you understood
now I will tell you here that
what I will do here I will write
if and I will say underscore underscore
name equals to equals to
underscore underscore main so I am saying
that if it means
what it means if this
module if this code
is run by its
own file ok
I will write it better if this
code is executed by
running the file
its present in
means directly I will use
directly word this code is directly executed
then what will happen by running
the file its present in
ok now I will write
directly running this code
and I will cut it and take it here
so basically what I am saying that
if name equals to main means if you
directly called the module then only execute
this code otherwise don't execute this code
ok and I will put this code
here so what will happen
this code will run only when
you are directly running this file
means if you import
8 underscore main then nothing
will happen why nothing happened
because underscore underscore name underscore
underscore main is not there underscore underscore
name underscore underscore is module
because when we import
the file we are importing
its name is underscore underscore
name underscore if we import this code
in any other file then we don't want this code to run
ok we want its functions
to come but we don't want this code to run
that code we write underscore underscore
name equals to underscore underscore main
I hope that makes sense now come here
I have written that evaluates the name of
the module in python from where
the program is run and this
is the global keyword very easy
global keyword now see what happens
that in python programming if you have
made a function I do one thing that I make
a file with the name of
09 underscore global dot py
now suppose you made a function
with the name fun I will
write f here fun
and you said that
a value will be printed
but if you said here a is equal to
89 if you said a is equal to
89 then what will happen
if you print a then what
will happen here 89 will be
printed and here 3 will be
printed if you print this
a then what will happen
a is equal to sorry 3 or
4 then what will happen
in this func a value will be
3 so a is local variable
for whom is local variable
a is local variable
of this function and here
what is global variable global variable
is that which you can use
in any function and can
change this global a
ok so what does global keyword
does global keyword changes
global variables ok so if you
wrote global a then this a will change
but if I didn't write global a
then you see 89
is printing 3 or 89
3 or 3 is not
printing so if you wrote
fun or print a then this
a is equal to 89
will refer because in fun
variables are own
and we didn't reference them
when we wrote print a
because a is equal to
89 is outside so it
will take outside property
in fun which you will understand
in a minute let's say I have a list and
there are some numbers ok
I wrote something like this
what I will do is print these
numbers ok so
let's say I have
a list and there are
some numbers ok
I wrote something like this
let's say I want to print which number is this and I will write here for i in I will write here let us say l and I will write here print
and let's say I will put an f string here and I will write here the item number and I will write here let's say I will write index and I will make it item only, index is
and I will write here item and what will be the value of index I will say index is equal to 0 and maybe I will say index plus equals to 1, because I want to start from 1
or if I want to write its index only, item number index is this then it will be very cumbersome process for me, but my work will be done see I have written here item number 1 is 3, 2 is this, 3 is this
let me do one thing, item number at index, I will do it here because first it will start from 0, then it will start incrementing item number at index, index is item, index plus equals to 1
and 0, 1, 2, 3 will come, you can do this work with enumerate function I will show you how, I will write here this can be simplified using
enumerate function, how it will be simplified using enumerate function, what you can do, you can write here for item in enumerate l
you will write here index comma item is equal to enumerate and you can print it like this so if you print it, you will get the same output, so if you write index comma item
in enumerate i, sorry enumerate l, which is list, so what it will do it will give you index in index and item in item corresponding index, so you can do something like this to access
index and item, it makes sense, good now we will talk about list comprehension, now what is list comprehension it is an elegant way to make a list based on existing list
let me tell you, suppose I have a list let me make a file, in the name of list comp let's make it in the name of list comprehension, suppose I have a list
my list is equal to and I have 1, 2, 5, let's take a big number, let's take small let's take 9
and this is our list now I want to make a squared list so I will do something like this, I will say for item in my list
and I will write squared list dot append and I will say item into item and finally I will print squared list
I have printed squared list and you can see 1, 4, 81, 25, 9, 25 items because I left squared list empty and I said item in my list
1, 2, 9, 5, 3, 5 and I printed 1, 4, 81 25, 9 and 25, I have printed item into item
I could have done this easily in one line using list and I will simply write squared list is equal to and I will say i for i
in my list i into i for i in my list, so what it will do it will run i into i for every item in my list
it will take 1 into 1 and put 1 in squared list it will take 2 into 2 and put 2 in squared list it will assign it in this list, so if I run it and show you
you can see I got the same thing as before so list comprehension is an easy way to do this I hope you understood this, now we will move to chapter 12 practice set
so see you guys in chapter 12 practice set so guys I have made a folder for chapter 12 practice set which I will open in vscode
for first problem I have made a file it says you have 3 files you have to open all 3 and give a message
if none of them exist, so I will say with open and I will write 1.txt and I will open in read mode and I will simply print f.read
I have written it as f and I will do the same thing for 2.txt and 3.txt and if none of them exist
so what I will do is I will write a try except now you can see as soon as I run it I got an error so what I will do is I will wrap it in try
so I will use vscode multi-cursor functionality and write try and I will take it inside and write end and I will press end again
so what I have done here is I have used try now what I will do is I will write except see how I am using multi-cursor functionality
and I will print e so if I run it, it will not give me an error but it is giving me a message and I will write thank you at the end to show you guys that my program
has not crashed, so I have written thank you and it will be printed now let's say I make 2.txt hi I am 2.txt
so I will make 2.txt and see here I got the content of 2.txt and the rest of the files sorry I printed an error message and not an error
I hope that makes sense, now we will move towards problem 2 we have to print 5th and 7th element of a list using enumerate function so for this I will make a program
I will write here l is equal to 1,2,3,4,5,6,7,8 what we have to print
we have to print 3rd, 5th and 7th so I will write here for item index I comma item in item
in enumerate of l if I is equal to equal to now don't make a mistake, 3rd element is 2
and I equals to equals to 4 and I equals to equals to 6 I equals to equals to 2, I equals to equals to 4 and I equals to equals to 6
if I is equal to 3,5,7 if index is 2,4,6 so if index is 2,4,6 then you will print
item, otherwise you will not print item I will run it and you will see 3,5,7, 3rd, 5th and 7th next question is to make list comprehension which will print multiplication table
so this is a very simple problem and at the end I have included this so that you can enjoy
finally when we are wrapping codes then you can enjoy so I will make a list comprehension n is equal to 5 and I will write table is equal to
basically we are making table of 5 for i in range 1 to 11 and I will write n into i for i in range 1 to 11
so 5 into 1, 5 into 2, 5 into 3, all the way till 10 let's print the table and see if it is working and you can see here 5, 10, 15, 20, 21 and let's put some other number
let's put 3 and its table will be printed and the best thing that you can do is you can do int input enter a number and if you do this then you can see here if you put 6
you will get table of 6, if you put 7, you will get table of 7, 9 and so on you can print any table and it will print 9, 10, 90 which is amazing, next question is
write a program to display a by b where a and b are integers, if b is 0 display infinite by handling the zero division error understand how interesting this problem is, so what we have to do
we have already made this program by the way, just if you remember program 4.py, so what I will do here first I will put try and I will write here
a is equal to int of input enter a then I will do the same thing for b, I will say enter b and after this what I will do here, I will say accept
or exception as e and I will not write exception here, I will write value error as v and what I will do here, I will print
infinite, sorry we have to handle zero division error and what I will do here I will print infinite
so if I run this, assume value of a is 4 and value of b is 0 so nothing happened here, there is something wrong so I did not print a by b, so I have to print a by b
in order to produce this error so if I give a valid number whose a by b exists so see the value, 3 by 6 is 1 by 2 which is 0.5
alright, awesome, next question please store the multiplication tables generated in problem 3 in a file called tables.txt, ok let's do that and for this what I will do
I will write an append program see how good program I am going to write to do this so we have generated a table, let's take it first
I am deleting this 2.txt and you can make it as you want now what I will do here, I will store this table
and see what I will do, I will say with open I will make a file, tables.txt and I will open it in append mode
so at the end it will be connected so f.write and after this I will write table, str table, I will convert this list in a string
and after this I will put a backslash n so that a new line will come, now see if I run this so if I run table for 6, see 6's table came
and instead of backslash n I forward slashed it no problem I will fix it and I will put some more tables
and you can see these tables are getting appended I can do one more thing, I will show you when I am writing f.write, I can make it as fstring
and I can say str table and backslash n and I can write table of n
something like this and str table backslash n, so let's say I wrote 5's table I wrote 2's table, I wrote 22's table
so see table of 5, table of 2, table of 22 came here so you can write any number's table here if you don't mind, I can generate a big number's table
if someone asks you table of 3, 3, 2, 3, 4, 2, 3, you can say 3, 3, 2, 3, 4, 2, 3, 2, 3, 1, 3, 3, 2, 3, 4, 2, 3 3, 3, 2, 3, 4, 2, 3, 2, 6, 6, 4, 6, 8, 4, 6
3, 3, 2, 3, 4, 2, 3, 3, 9, 9, 7, 0, 2, 6, 9 you can say like this, we used to say this in our childhood so if someone asks you table of 7, 17, you can say table of 3, 3, 2, 3, 4, 2, 3
so keep the joke aside and relax chapter 12 is over, after this we will talk about chapter 13 and we will see some advanced topics in chapter 13
so see you guys in chapter number 13 intro so guys now we will move towards chapter number 13
and that is advanced python 2, in this I have given you some advanced concepts which are actually simple, advanced means those things which I want to tell you guys additionally, let's talk about virtual environment
first of all, what I will do in chapter 13 I will right click on shift and open with code and here first of all, I will make a file
with the name 01__venv.py now what happens, let's say you have installed a version of any module so if I write pip install pandas
so what will happen, see here pandas will be installed so every package has a version, now see here it is giving me a warning, our python extension
it is saying, you may have installed python packages in your global environment which can cause conflicts between package versions see this carefully, it is written here that there can be conflicts
between packages, do you want to create a virtual environment to isolate your dependencies, now see what it is saying, if I click on create by the way, this warning does not come every time, may be you may not get it
but I will tell you why virtual environment is created, let's say here I am using pandas 2.2.2, now let's say I want to use pandas 1.9, so what will I do, if I write here
pip install pandas equal to equal to 1.9 and hit enter so 1.9 version of pandas should be installed actually 1.9 is not there, it is up to 1.5, if I write 1.5.3
so see here, 1.5.3 will be installed, but in that case 2.2 version of pandas will be gone, so I can
keep only one version of a package, now let's say I want an old version of a package for some reason, so what will I do, I can create a virtual environment, like virtual machine, let's say you have
2007 has been installed in one virtual machine and Microsoft Word, the latest one, has been installed in another machine So what can you do here, you can do the same thing in the virtual environment You make a virtual environment in which all your packages are there and that means it is not from your global interpreter
Means your python installation, the python we have installed, a copy of that installation will be made And in that copy, we will install different versions of the package and the main python of that copy will not make any difference to your computer I hope I was able to explain this thing, we will be able to make an isolated environment
So what do we have to do here, getting requirements to build wheel, it is still running and I guess it is running So I will wait for it, it will be completed or I can cancel it if I want and I will directly do pip install virtualenv virtualenv, so virtualenv is a package that is being installed now, you can see virtualenv has been installed
Now I will write virtualenv here and I will name it env, I can name my environment anything I have kept env here, now it is asking me that you have created a new environment, do you want to select it for this folder I said yes, so it is showing me below that a new virtual environment has been created using 3.1, 2.3
And a folder will be created here, you can see a folder named env has been created In which there is lib, scripts, basically a python isolated environment has been created here Which you can use and you can put any version in it
Now what I will do is, I will come here where these two files have been created, you can see our python file has been created And an env folder has been created in which our environment is there, I will shift right click and open in terminal And you can see what I can do here, I will activate this virtual environment, so if I write here pip install pandas
And I hit enter, you can see the requirement is already satisfied, it is telling me that pandas is already installed in your computer If I do equals to equals to 1.5.2 that I want to install 1.5.2 pandas, then it will download 1.5.2 and install it in my global python installation But now I want to work in 1.5.1, 1.5.1 or any lower version
So what I will do for that, I will install that pandas in this environment, I will show you how we can do this I will let it finish and once it finishes, we will activate this virtual environment So you see here that once we install the virtual environment package and after that if we make a new virtual environment
Then we can activate that virtual environment, I will tell you how to activate that virtual environment What is a virtual environment, an environment which is same as a system interpreter but is isolated from the other python environments on the system So the python you have installed becomes a different copy of your python installation, so let's see if it is finished or not
It is still building the wheel, so guys it really took a while and you can see that it has uninstalled pandas 2.2.2 and is installing 1.5.2 So now what I will do, I will activate this virtual environment and to activate it, I will simply type .slashenv, I will press tab And I will press scripts and then I will type activate.ps1 and I will hit enter
And you can see that env is written in the bracket which means that the environment is activated Now when I am running vscode, it will not come in the env bracket but because I have already done vnv here, so all my packages will be installed in the virtual environment So you have to remember this thing, env does not show vscode here
Now I will come back here and I will install any pip, let's say I have installed pip here and I have written pandas here and hit enter So what will happen is that the latest version of pandas 2.2.2 will be installed here and here you can see 1.5.2 which is being installed in my global will be installed in my global So I am installing it here in this virtual environment and if I import pandas and I print the version of pandas, then you see how I can print it
So I am waiting for it to be installed and if I write python here and I write import pandas and I write pandas dot underscore underscore version underscore underscore So you see 2.2.2 is showing because I am in the virtual environment If I write exit here and run exit and I write exit again which will get me out of this terminal
Now I can write deactivate and activate the virtual environment Like I showed you that we can activate by running env scripts activate.ps1 We can deactivate by writing deactivate
Now if I write python here and I write import pandas Earlier you saw 2.2.2 came but if I write pandas dot underscore underscore version here, then 1.5.2 is coming Which means I can install different versions of pandas in my global python and in this virtual environment
I hope that makes sense Now we will come back here and we will hear about pip freeze command Now if I want to know which packages are installed in a particular python installation, then I will run pip freeze command
If I write pip freeze here, then it will list all the packages Like you can see that my virtual environment is already active Because I want to tell you that whenever you activate the virtual environment in vscode, then your env is not shown in the bracket
Now if I open the terminal here and run pip freeze So you see the version of pandas will be shown here and I have installed a lot of other things Where are you pandas?
I know you can see pandas, but I can't see 1.5.2 or 2.2.2 This is my virtual environment and this is my global python Now if I want to write it in a file, then I can run this command pip freeze requirement dot txt
It's a good practice that we take all our important packages in a file called requirements dot txt So as soon as I hit enter, you see a requirement dot txt Now to recreate this virtual environment, I just have to write pip install minus r requirements dot txt
If I hit enter, then all these packages will be installed one by one in any virtual environment So whenever we make a python project, we run pip freeze command and make the requirements dot txt file The benefit of this is that all the python packages are stored in the requirements dot txt file
And then using that information, we can recreate that environment by doing pip install minus r requirements dot txt We don't need to install these packages one by one pip install minus r requirements dot txt will install all the packages in the requirements file
Now let's see about the lambda function Lambda functions are some functions that we make with the help of lambda keyword I want to show you that if I want to make a function called square, then I can make it in two ways
I make a function called 02 underscore lambda dot py So I can say that y square is a function that takes n and returns n into n And if I print square of 5, then the square of 5 will be printed
But I can also make it like a variable by doing something like this I can say that square is equal to lambda which will take x and return x into x And if I run it, you will see that it will also give 25
So lambda function is a way to create a function with the help of an expression So function is in the form of an expression As you can see, square is equal to lambda x colon x star x
So here we can also make it by giving lambda and multiple arguments So I have a comma b comma c and return a plus b plus c So in a way, this is a shortcut to make functions in a single line and to give functions to a variable
So you can make a variable which is a function So guys, the next topic is the join method which is a very simple thing I will show you how the join method works
So the join method works for strings And what you have to do is, let's say I have a string And I have a string called a is equal to Harry
And let's say I have a list I have Harry and I have Rohan And let's put one more, Shubham
Now if I want to join these three with a dash So I will write here, final is equal to I will write dash and I will write dot join and I will write a
And if I print final, then what will happen is that these three names will join with dash Now if I want them to join with double colon instead of dash So I will write double colon here and they will join with double colon like this
That is, double colon will come in between them and they will join and make a new final string So this was the join Now I will move to the next topic and I will tell you about the format
Look, now the f string has come, so this much of format is not used But in some old source codes, you will get to see the syntax of the format For which you should learn the format, how it works
So what do you do to learn the format Simply I will show you something like this I will copy and show you how it works
So let's make a new program format.py So let's say I have a string, I made a string like this
And I will say .format and I will say argument1, argument2 So if I assign a string to it and print this string So what will happen is that where I have given the first bracket, Harry will come
And where I have given the second bracket, boy will come So Harry is a good boy will be its output Now if I want my boy to come here and my Harry to come here, then I can do numbering here
By default, this numbering looks something like this That zeroth argument means Harry will come here And the first argument will come here
Okay, but if I want my Harry to come here and my boy to come here So I can do this And if I run it, then boy is a good Harry will be its output
This was used earlier, the format function is now used very little Since the f string has come, Python 3.6 onwards I will move towards the next topic, which is map, filter and reduce
The first function is map What does map do? It applies a particular function to all the items in an input list Suppose I have a function, a lambda function
Which is squaring, suppose So I will show you how map works So first of all, I make a file named filter reduce.py
So suppose I have a list in which there are some numbers 1, 2, 3, 4, 5 And I want to make its square list So first of all, I will make a function named square
And I will say this is a lambda function which takes x And returns x into x And after this, what we will do
Square is equal to lambda x, x into x Now here we will use the map function to get the squared list So here you see, how you can do it
You write map function here and give input list So if I write here sq list is equal to map And I give square here
And I give l here And after that I print my sq list sq list
So what will happen, I will get the squared list Here map object at is getting like this And if I want to get its values
Then I have to convert it into a list So if I want to print it Then I have to print it like this
List and sq list have to be done like this So that it can be converted into a list So now see, it has been converted into a squared list
By default, if you want to iterate its elements This is an iterable, you can iterate it with for loop For item in
And after that, by writing sq list And your work will be done I hope you understood how map is used
So here I write map example So we use map If we have to run a function for all elements
Then we can run it What does filter do? Filter basically filters
Which is its name Now we will see the example of filter Example of filter is very simple
Here we will make a function And here we will write even And this even function
If n is divided by 2 Then what will it do? If n is divided by 2
Means if n is divided by 2 and remainder is 0 Then we will do return true Otherwise we will do return false
And here we don't need to put else Just telling you Because if it goes inside if
Then it will return anyways And if it doesn't go inside if Then it will be false
So this is already false Now what I want here That I get even numbers from a list
So what I will do I will write print list And let's say here
I am making a list named only even Only even is equal to I will write filter
And filter takes the first argument function Which will be even for me And after that it takes the second argument list
So from my list I will get only even numbers Which are 2 and 4
I hope that makes sense Now we are going to see Reduce function example
And to give an example of reduce function What I will do I will make a function which is sum
And what will it do? It will sum a and b And what I will do here
I will return a plus b I could have made its lambda function But I chose to make this
Now what I will do I will write print reduce And I will write here
L Now you have to import reduce function From func tool module
So what we will do here We will import reduce from func tools import reduce
and here the first argument of reduce is function with which you want to reduce, second argument is list so here I will write sum, l
so as soon as I will write sum, l so what will happen, this sum function will run on l
by doing 2 by 2, I will tell that it will run by doing 2 by 2 like it will run on 1 and 2 first, then the result
will run on that and 3 then the result will run on that and 4 then the result will run on that and 5
for example here we are talking about sum which sums a and b so here we will run on 1 and 2
so 3 will come, then 3 and 3 will become 6 then 6 and 4 will become 10
and 10 and 5 will become 15 so the output of this program will be 15 and you can see here 15 is the output of reduce
to explain this I will take you to this diagram, like let's say we have a list 1, 2, 3, 4
1, 2, 3, 4 we have numbers first sum will run on 1 and 2 and this list will reduce to
3, 3 and 4 then it will run on 3 and 3 which will be 6 and 4
then it will run on 6 and 4 and it will become 10 if it would have been multiply 2 and 1 would have been multiply
then it would have been multiply by 3 then it would have been multiply by 4 so 2 into 3 would have been 6
6 into 4 would have been 24 so if I multiply it if I make a multiply function
multiply is equal to lambda x comma y
and return x multiply by y and if I give here mul instead of sum
which is multiply then we will get 120 why is it 120? because it has 1, 2, 3, 4, 5
2 into 1 is 2 2 into 3 is 6, 4 into 5 is 120
24 into 5 is 120 I hope that makes sense and this wraps up chapter number 13 and I will see you
in chapter number 13 practice set so guys here for chapter 13 practice set
I have made a folder and I will solve the first problem here
infact the first problem is very simple, we have to make virtual environments so what I will do here is
pip install virtualenv and after that we have to make 2 virtual environments so to solve this problem
I will open my terminal here because in vscode many things are managed by python extension
so I try to do very raw things and what I will do here I will make virtualenv env1
so my env1 is made and I will make env2 so these are 2 environments which I have made
now to activate any of these what I will do, I will write env1 I will tap
then I will write scripts .ps1 so that my env1 will get activated
and what I will do here, I will pip install the question is what you have to do
you have to install some packages in the first virtual environment so what I will do, I will pip install pandas
and after that I will pip install pyjoke and after that I can pip install more things
but I keep only 2, I keep pandas and I keep pyjoke once pandas is installed, I will pip install pyjoke
so I am waiting for pandas to get installed so here our pandas is installed now I will
pip install pyjoke and my pyjoke is also installed now what I will do, I will write pip freeze
and in this way I will generate requirements.txt file so that my requirements.txt file
will be created here after this I will activate my second environment and for that I have to deactivate
my env1 I will enter, env1 is deactivated to deactivate
env2 I have to write activate.ps1
and in env2 I have to write pip install-r
requirements.txt so that all these modules which were installed here
in environment 1, they will also come in environment 2 so now see in environment 2 all my packages
are getting installed pandas is getting installed, pyjoke is also getting installed so our first question
is done so I will also check once by importing those packages so that you know
that it is actually working so I will write python here in apple import pandas
and you see import pandas is working which means that our pandas
is installed in env2 now second question is write a program to input name, marks
and phone number of a student and format it using format function like this so name, marks and phone number
I have to take these three things so I will write apple02 underscore problem2.py
and I will name is equal to input enter name
name marks is equal to int input enter marks and after this
I will do that input
so I have imported these three things and I will print f string but not f string, we have to use
format function, so I will do s is equal to and I will copy this string I have got a habit of f string
that means things become so easy with f string but again now we have to use format function
so we will obey the instructions of the question so what I will do here
name of the student is name will come here then his marks are
will come here then phone number will come here and I will write here
.format, I don't want to write 0 here I pressed it by mistake .format and I will write here
name, marks and phone and after this I will print it to see
whether it is done correctly or not run it and after running I will write
Harry in name, 100 in marks and in phone number I will write
random number don't call it, name of the student is Harry his marks are 100, phone number is
2, 2, 4, 2, 6, 2, 8, 2, 10 number is made don't call it, it is a random number
now what we will do next question list contains a multiplication table of 7
program to convert it to string of same numbers so multiplication table is of 7
we have to make it a string so what I will do here 03
underscore problem3.py so I will write here
table is equal to 7 into i, 7 into
into i for i in range range
1 to 11 and it will become a table with the help of list comprehension
and what I have to do I have to write a program to convert it to a string so what I will do
I will write this and I will write backslash n and I will write .format because I have to make a vertical string
and I will write table and it will be s is equal to
and I will write print s so you can see sorry not .format
I have to write .join I am sorry about that so I will join and you can see
as soon as I will do this it will say int found we need a string
so what we will do here we will make our table a string because your
your join function it needs a string
and you have to do it so if you join so you
have to convert it to a string so I will run it and you can see a vertical table is made here of 7
7,14,21,28,35 and so on now the fourth problem is that you have to filter
list of numbers which are divisible by 5 so what I will do here
I will make a method I will make a method and I will give an n
and what I will do here I will say if
your n is percent 5, if n percent 5 is equal to 0
return true otherwise we will return false
and I will write here a is equal to 1,2 and here
I will write here and I will write anything here
and here 55 I have taken some numbers now I will say f is equal to
list filter and our first function will be divisible by 5
and a and if I print f then in f only those items will be divisible by 5
because I have made a function which returns true when n is divisible by 5 and after that
what I did, I ran filter function and gave this list so we saw how filter function works
next question is write a program which takes maximum of numbers in a list so what I will do
05 underscore problem 5 dot py and I will do one thing
I will use this list I have put some big items in it so this
is correct so what I will do 1,2 and
65 and 111 and the biggest item
will tell me so what I will do I will make a greater
and I will say if a is greater than b, return
a or return b, I have made a simple function now what I will do
I will make it l I will write print reduce
and reduce I will have to import as I have already told you from func tools
import reduce and the first argument of reduce is function
so I will give greater and then l so what it will do
it will tell what is the greater so first it will compare them
1 1 1 1 1 1 1 1 1 and from 635
will be 65 so now you see it will give the greater number
so it will make them fight the winner will fight with this the winner will fight with this
the winner will fight with this the winner will fight with this the winner will fight with this
the strongest one will win so this is the ninja technique I hope you understood
if I make this greater it will give me 5553 as you can see
next question run pip freeze for the system interpreter take the contents and create a similar virtual environment
so what we have to do we have to run pip freeze so what I will do
I will come to this folder and open in terminal where did you go terminal
where did you go terminal open in terminal and I will write pip freeze
requirements.txt and here a requirements.txt will be created in chapter 13 practice set
so if I show you the requirements.txt file is created now what I will do
I will create a new virtual environment so I will write virtualenv and I will name it harry
and after creating harryenv I will write .harryenv
.slash harryenv .slash harryenv and then scripts
then activate.ps1 so it is activated harryenv is written in bracket
then what I will do after activating pip install-r requirements.txt
and all my requirements will be installed one by one it is installing
so it will be installed see it is installing and finally it will succeed
now what I will do I will explore flask module and create a web server
like question number 7 so question number 7 question number 6
problem6.py so I will write the commands which I have run
so it will be easy the command which I have run I will do one thing
I will copy and paste it and I think it should be useful here I have written something like this
and you can see I will write two commands I think that should be good enough
so I will write like this and you can run the rest in your computer
so these are the two commands which you have to run and I will comment them to show that this is not a python program
and see it is still installing and it has installed all my packages
in the environment and I have already done this n1, n2, harry n
all these I have created in virtual environment now in virtual environment there are many files if you inspect it
you will find many things basically there is a copy of python so now what I will do
I will explore flask module and for this I have to go to internet so what I will do
I will write flask module and you can see welcome to flask so first of all I have to do pip install flask
pip install flask and flask will be automatically installed
and here what I will do 07 underscore problem
7.py and here I will create a basic server of flask
I will click on a minimal application there is a minimal application in quick start I will copy it
and after copying I will read the instructions to run it
there are instructions to run it you have to run it and after doing this
your work will be done now you see this is app.py there is another way to run it
I will tell you you can find it from documentation you can run it
you can run app.run I have created an app and you can do like this, app.run
and it will run after running this program now see, we will run this program so this app has been created and you see, if you click on control on this, then hello world
a basic python server has been created now what is flask, basically it is used to create websites and APIs you can explore this further
and that wraps chapter 13 practice set, I am so happy we have come this far Alrighty guys, the project we are going to make today is very close to my heart and I can call it mega project from my heart because it has the qualities of a mega project with that said, I will make a folder here and I will name it mega project 1 and this is Jarvis and this Jarvis is not a normal Jarvis
let me tell you what I mean this is a project in which we, if you have seen Alexa, if you have seen Google home, if you have seen such devices, then we are going to make something like this for our system so I will make this main.py and after making main.py, I will install some packages
but because this is a mega project, all the packages we have are going to be kept in a virtual environment so to keep it in a virtual environment, I will click on select interpreter, you can see up here, plus create virtual environment or you will see select interpreter at the bottom right, you guys click on plus create virtual environment and select vn
and a virtual environment of python 3.1.12 is being created here, you can see below, creating virtual environment, something like this so this takes a little time to create a virtual environment and the virtual environment is finally created, this is done, my virtual environment is done now let me tell you one thing, whatever pip install I will do here, it is also giving me a warning, it is also telling that python virtual environment was successfully activated
it will not come in venv bracket, but it means that the terminal will work on your virtual environment only, good, amazing so I will write pip install here and we will install speech recognition and py audio first so you can install different packages by giving space, like I have given here speech recognition, space, py audio, so it will install both
so I hit enter and first install this much, now here you can see it is installing all these packages, it installed it very quickly after this, one more thing I am going to install, pip install, I am going to do setup tools, setup tools and I will bring this terminal here just so that you guys can see, if I drag powershell here, it will become a tab and you see, by writing pip install setup tools, it will be installed in setup tools
and it will be installed in virtual environment, which is a very good thing, now in my main.py, I will write here if __name == __main I told you guys what is this, it is a good practice that you guys put this in your program, so first of all I will do speech recognition as sr now what is this as keyword, when I am doing import speech recognition, then I will have to write again and again, after import speech recognition
speech recognition dot something, speech recognition, so instead of not writing this big, I will just write sr, so to make it small, I have made it as sr so if you are using as keyword, then you will write sr instead of speech recognition, it is such a simple thing, so we did this now what we are going to do is, we will install one more module, which is called web browser, so we will write pip install, now I will do don't show again so that it doesn't come again and again
pip install and I will write here web browser, it is saying could not find a version, so if I write here import web browser so web browser ok, so interface is for, so web browser is a built-in module I guess, is it, let me check, web browser module, yes this is a built-in module so this is a built-in module, which means that I don't need to install it, amazing, that's good actually, that's good, so what we will do is, we will open browser from web browser
so if we want to open anything in browser, and we will give voice commands, and we will open from web browser whatever we want to open, one more thing I am going to do here, install pittsx, pittsx3 what is this, this is a package of text to speech, which will help you to do text to speech, so I will install pip here, pittsx3 and by doing pittsx3, you can see here, I am waiting for it to be installed, it is installed, so if we import pittsx3 so what we can do is, we can do text to speech, if we want to call anything from jarvis, we can call it, so far so good, I am really happy, we have done a lot, now our first task is, we have to make a recognizer object, and what that recognizer object will do is, whenever we will say something, it will recognize it, so I will simply write here
recognizer is equal to sr.recognizer which is a class, what does recognizer class do, it helps you to take speech recognition functionality, and now we will initialize ttsx engine, so we will write ttsx is equal to pittsx, so we will write pittsx.init and by this our pittsx will be initialized now we will make a function which will be a speak function, which will take a text and then will speak it, now how to make this speak function, for this we will use this, first we have to init and after that we have to simply say engine.say, so you can see here, if I will do engine.say and I will make engine here, in fact I will do it exactly like this, so what will happen is, it will be easy for me, so I have done engine.say, now if I will run the speak function, if I will write speak, hey sir how may I help you, so I hope you are listening to the desktop audio, it didn't work, let me see why, so I run it, it didn't work, so I will speak this text, I will text it, I run the speak function but it is not working, let me check why,
so here we have done everything, why it is not working, we have to run run and wait, engine.run and wait, because otherwise it will exit, hey sir how may I help you, so it is working, it is working, so our jarvis, I call it initializing jarvis, because I don't want it to keep running, I want it to listen for wake word, I call it jarvis, it will say tiding and then I will say do it like this, so it will do it, understand why we are doing this, otherwise what we are doing normally jarvis will keep listening and it will be annoying, so this is not a reason to do this, so come on, I will tell you how to do all this, so what I will do here, I will go to speech recognition, I will go to speech recognition documentation, speech recognition and in speech recognition package, Python has just come, so I will go to speech recognition and see its code samples, so I am seeing that I will get code samples somewhere, because you can't remember code samples, here you ask chat gpt and if you are not asking chat gpt then you will have to pick it up from somewhere, so here I am seeing the project speech recognition readme is also there,
so here I am just seeing if they have given any code, like examples directory, see they have given examples, so here if you see there is a file microphone recognition, so I will pick some code from this microphone recognition file, so they have taken r is equal to, first we will take audio from the microphone, after taking audio what we will do, we will see what has been said, so from here to here the code will help me to find out what has been said, so let's go, I am very excited, so the first thing I am going to do, I will write a comment, listen for the wake word Jarvis, so if I have said Jarvis, then only something should happen, and if I have not said Jarvis, then nothing should happen, it is a simple thing, and what we are going to do, we are going to do it in a while loop, so I will do while true, so I will do while true, and I will say this thing should keep happening, it will listen for the wake word, and finally what will happen, here say something is written, I will make it listening, so this is listening, r.listen source, and finally what will happen, that if r.recognizephoenix, we can also use recognize google function, so I will do one thing, I will write command is equal to this,
and print the command, so let's do this for now, and it is very important to keep this in the try, because if we do not keep it in the try, then we can get an error, and it is very important to correct the indentation, so let's run this, Jarvis, Jarvis, so it is saying that you have not set up the phoenix module correctly, so let's do one thing, let's see what is this error, because you cannot do everything with your brain, you have to use stack overflow, you have to install everything that is going to be used, so here pip install pockets phoenix, let me try this, let me try pip install pockets phoenix, which I have done, I have basically fired this command, pip install pockets phoenix, and let me do one thing, whatever commands I am using, I will export requirements.txt to you, but again I keep putting it at the top, I have run this command,
and now let's see, hey Jarvis, hey Jarvis, Jarvis, Jarvis, now see it is saying John Lewis, .if, so it is not able to hear Jarvis properly, so that's why I use google, I don't use recognize phoenix, I use recognize google, I think that function is very good, so we are not going to use this, we are going to use recognize, see there are many of recognize, google cloud, roundify, ibs, phoenix, tensorflow, whisper, I don't know what is this, so let me do one thing, let me do recognize google, where did you go google, recognize google was a function, recognize google cloud was also not recognize google, so let me do one thing, how are you Jarvis, he is saying give me your google cloud connection, so recognize google is a function if I am not wrong, Jarvis how are you, Jarvis, now see there is a problem from here, it is taking a lot of time to recognize, so let me do one thing, let me print recognizing,
so that we know how much time it is taking to recognize and how much time it is taking to listen, so I will run it again, see I did it like this, now I will run it, listening, Jarvis, it is taking a lot of time, see what happens, I will show you one thing, the listen function takes two things, one takes your timeout parameter, one takes face time limit parameter, so if I do timeout is equal to 2 seconds, timeout is equal to 2 seconds, so I will show you what will happen, so by doing timeout is equal to 2 seconds, it will listen for only 2 seconds, and after that what it will do, after listening for 2 seconds, it will timeout again, so I will show you. Initializing Jarvis, Jarvis, why is it saying Jarvis, I have written the spelling wrong, I am saying what is Jarvis doing, initializing Jarvis, yes Phoenix could not understand the audio, so I will do one thing, accept exception as e,
I will do exception as e, and I will write could not, I will write an error here, we are not using Phoenix yet, Jarvis, I will run it again, initializing Jarvis, see it is listening, I want it to keep listening again and again, see it is listening, I want it to keep listening again and again, so what I will do here, I have done timeout is equal to 2, so there are two parameters, one is timeout, that is, maximum number of seconds that this will wait for a face to start before giving up and throwing in a speech recognition.waitTimeoutException, and what is this faceTimeLimit parameter, maximum number of seconds that will allow a face to continue before stopping and returning the part of the face, for example, you said something, after that how long can you wait to continue, so I will do it here for 1 second, so I will do faceTimeLimit for 1 second here, and see now,
initializing Jarvis, so here it threw a waitTimeout error, and here you see that r.listen, so I will take this thing in try, because I don't want to get an error, so oops, I will take it in try, what will happen to it, that audio is equal to this, now I will run it, initializing Jarvis, Jarvis, Jarvis, Jarvis, Jarvis, yes, see now it has become very fast, it is going to listen, Jarvis, now tell wake word, now we only have to do the job of wake word. now we have to do only the wake word, so once we have listened for command, so I will say here, if this command is Jarvis if command.lower, we have to convert in lower case too say ya, and after that we will keep our conversation
and I will show you here, if command.lower is equal to equal to Jarvis so we said ya and after that what we will do, listen for command listen for command, till now we have listened for wake word
means we have listened for Jarvis word we took it from audio microphone and then finally what we did, we said that
look bro, listen if Jarvis has been said then do this, so say ya and after that we will listen for command so what we will do, with sr.microphone as source
what we will do here is, listen for command and whatever will be our command, whatever will be our command this time I have done print listening here
and audio is equal to this and our command will be r.recognize google audio, so this will be our command and I call this word, I call this word
because now we are listening for only one word so word.lower is equal to equal to Jarvis so what we will do, speak Jarvis will say ya
then whatever you want to say, I will write Jarvis active here Jarvis active and after that you have to listen
and r.recognize google audio will be the command then after this here, we will make a function process command, so our command will be process
and we will make a function, which I have not made yet so let me do one thing, I will make it and it will process our command, so I will write here c
and for now I will write pass here, so this is a function which will work now let's test this initializing Jarvis
Jarvis, Jarvis, ya now Jarvis is active for now, I will print the process command, c
and once Jarvis is active, what will happen command will be processed, ok let me run it again because I have made changes
so our Jarvis is active, it is working, it is not saying anything to us it is working quietly, it is working, it is working Jarvis, ya, how are you
it says c is not defined, let me see what is the problem so I have made c in process command and c in print what is the problem, I don't understand
c, ok here I have to give full command Jarvis
Jarvis, ya, how are you ok, I have to run it again because I have changed the program initializing Jarvis, Jarvis, ya, how are you
so it has recognized how are you, now we can process it the first feature which I want to add in this is open google so here I will say if c, if c.lower
equals to, equals to open google here I will say if open google in c.lower in c.lower
in c.lower, if open google in c.lower then what is it, if open google is in c.lower then what you have to do, webbrowser.open
https://google.com and we have to do the same thing with open facebook we have to do the same thing, here I will say elif
we have to do the same thing with whom, with facebook with linkedin, so here write facebook ok, facebook, open youtube, open youtube
so now your Jarvis has become a virtual assistant what will it do, if you say open youtube then it will open youtube, if you say open facebook
it will open facebook, if you say let us say open linkedin linkedin, it will open linkedin, let us try this first so here I have taken all these
initializing Jarvis, ok if I say self open youtube so will it react or not, Jarvis yes, open google
see it has opened google it is fun, it is fun, it will be more fun just watch the trailer, the movie is still remaining my friend
so now we will stop all this and I will show you what else we can add so Jarvis is still listening, if I want I can say again to Jarvis yes, open youtube
it has opened facebook because I have messed up the code so I will do youtube instead of youtube, now it will work so this is a program which you can run and it will work
you keep doing your work, ok and whenever you want to talk to it, just call its name and it will do your work Jarvis, yes, open youtube
why should I open youtube, Jarvis will open it why should I open it, I can make it I am capable enough to make a virtual assistant for myself
so why should I open it from Jarvis, why should I do it myself, it is fun it is fun till now, Jarvis Jarvis, yes
open linkedin bro, bro, bro Jarvis, yes, open youtube
oh wow so I will do one thing, I will write songs here which songs, we play songs
we play songs of NCS, non copyright songs and I will sing you a song, stealth stealth youtube music
now it will come, youtube music, it was stealth Akash Gandhi, now it will come this, this music
now I will show you one thing now I will show you one thing, we will make a music library here we will make a global variable, our music library
now music library, or we will do one thing we will make music library in another file, we will make music library.py
and we will import music library, we will import it and we will write music is equal to a dictionary
we will say this is the url of stealth song this is the url of stealth song then we will say, there are many other songs
I will name this song as march you name your favorite songs this is done, and I will name this song as skyfall
skyfall I will take the link of skyfall, copy link address and what else we can do, I will keep only three songs
I will keep one more, I will I will write youtube music library I will write youtube music library
and I got youtube music library and I got youtube music library let's see
I will keep wolf now see what I want to do, I did import music library what I will do, else if
else if, there is an elif in this play, or let's do one thing, we will say if c.lower is starting
.starts with play then what you have to do, you have to search .play, then what you have to do, you have to search music
music library.music and what you have to play, music library.music and here will come song, now what will be our song
so see, if there is play in our command, then what we will do we will say song is equal to c.lower.split by space of one, now see what I did
it is very important for you to understand, if you said play skyfall, then what you have to do, first c.lower, which is play skyfall, .split
by space, which will convert it into a list one will be play, one will be skyfall then we will take one of this list, 0 will start with 1
so our song will be skyfall, which we will be able to play, understood very good, now see, I did this and now my song's link will come here
link is equal to this, then what we have to do, we have to open this link which we can do by opening web browser so let's try, let's tell Jarvis
let's tell Jarvis to play Jarvis's song, and now I am saying that Jarvis is not reacting, but when I will take his name, then he will react Jarvis, yeah
play skyfall oh bro, means ad came, otherwise skyfall would have been played are you understanding, very good
and now I will tell him to play some other song so now I will show you, Jarvis yeah, play wolf
did you see, so any song, so what you will do to your music library here you will come to musiclibrary.py, and in musiclibrary.py you will increase your music library, see we all listen to 20-25 songs
our favorites, and we can listen to any of those favorite songs like I want to listen to march, so see my Jarvis is listening but he is not reacting, but when I will take his name, then he will react
like you see, Jarvis Jarvis, yeah play march
Jarvis Jarvis, yeah, play stealth ok, so there is a problem here, there is no problem here
Jarvis, yeah play stealth accuracy wise, there is not much accuracy
we have to tune some parameters, like we have to tune these values but we have a basic idea, now see what should be the value of timeout what should be the value of face time limit, which recognizer should we use
like I am using recognize google, so if we tune all these then it will be perfect like our Alexa, so this thing you have to see, very good, now see he is listening
continuously, and then there are other things that can optimize it but for now we will not talk about them, now we will just add functionality so what have we added till now, this is capable of opening a music
this is capable of opening a link, this is capable of playing a music on youtube, and what else we can add in this now I can say this, if
lf c.lower.startswithnews if this news is in this, so what we can do
or we can do something like this, if news in else if news news in c.lower.startswithnews
if it is news, then what it will do, it will play news now how will we get news, I will tell you, so there is a news API which gives you news, so you can
take your api here by signing up, I will not show you my api but you can take your api, and then what you can do like this, all articles about tesla from last month
all articles mentioning apple from yesterday sorted by popular publishers first or if you want headlines, I will go to documentation
what I want, I want headlines, and what we will do here we will see where we will get headlines, top headlines how you can get top headlines, I will do countries equal to India
so what I will do here, I will put my api key I will log in first and put my api key, I think I have password I will take out my api key and come back
so I brought my api key, this is my api key, so I will do one thing I will write news api key is equal to I will write news api key is equal to
news api is equal to and this is my api key, guys please don't copy this api key, I am requesting you don't do this, generate your own api key, otherwise I get error
if you exhaust my api key, but I trust you which I shouldn't do, because someone exhausts my api key the experience is not good, but no problem, if news comes
in c.lower, then what we will do, we will take this content in this url, so what we will do is we will use a library called request, we will say
ours is equal to request.get so what happens with request.get, whatever is in this data
and what we will do here, we will request.get and now i will go to chat gpt and what i will say to chat gpt babu, chat gpt babu, see i am a person who uses AI i will say to chat gpt babu, help me fetch headlines from this
so it will tell me what is the format in news api, you can request.geturldata is equal to request.json and after doing this you will get json and after this you will get articles in articles, so i will show you how it works, so i put my api key here first
and see it becomes a little easy by using AI, see all our articles are in articles and i need all the titles from articles, so i will do one thing, i will say that there are i need all the titles in a list, see i can do this myself by using for loop
but what i will do, i will say that this is how it looks like, this is the format and i will not get the data from chat gpt, i just want to get the format, so i will do one thing i will reduce this a little and i will do like this and i will reduce this a little and i will say that
this is the format, i just want titles from this, i just want titles from this, i said this and see here it will tell me that if you want to get all the titles from this json response then take your api key, after taking your api key what you have to do is run this code
so see the first thing i am going to do is that i need at least the response i need all the articles, see in fact it has already done it, it has already said the titles of the article so i think i am good, i think i am good, i didn't even need to do what i did
so what i will do, i will come here and all the articles, all the articles data is equal to this i will correct the indentation, my response is r, r is equal to r.json data.getArticles and i will do speak, not print, i will do speakArticles, so i will do one thing, i will do language is equal to in
and although it's not a good idea but i am hard coding my api key here, okay i am hard coding my api key here, hard code means i am writing as it is, i am not bringing it from variables but you can bring it from variables, you can bring it from variables by using news api
but let me do the same thing, i will write news api, what is going on, so i did f here, my news api is here okay, i will run it and you see i will say tell me news Jarvis tell, yeah tell me the news
we seek save your mothers and sisters, Harbhajan slams 8ml for remarks on Arshdeep, Xpac cricketer apologizes, news 18, stock market live, markets end flat, nifty below 23,300, nifty mid cap 100 up 440 points, the financial express, Supriyasu will take stig at Pune awesome, so he is telling me the news, so he gave me google, facebook, youtube, linkedin, played music and he also told me the news, awesome okay guys, so what are we going to do now, we will do integration with open ai, which means that along with the use of ai, we will power our assistant with ai
so now what we will do, in else, suppose there is no news in our request, this is also not done, open google, open linkedin, so what we will do, finally we will say, now go to open ai and let open ai handle the request okay, awesome, and here what we will do, simply here, I will come to my open ai account, so see what happens in open ai account, when you go to your open ai account, if you simply write open ai api on google, open ai api so what will happen, open ai has an api, you can use it in python, you can use it in different languages, like I will show you a quick start guide for it, what you have to do is simply like this, first of all what you have to do is, I will show you, you have to make this client, so I will copy this code
and first of all what we will do is, we will make a new client, I will make it with the name client.py and I will write a pass here, otherwise this code is troubling me, and I will simply install the open ai package, so I will write pip install open ai, which I should write here, and finally I should make a client with my open ai api key, so I will use this client, I will not use this client because my environment variable does not have my api key, so I will put my api key here, you guys keep your api key hidden, people don't want to see your open ai api key, but for now I will put my open ai api key here, and after that what we will do is, we will use their completion api, so how to use completion api, you have to do something like this, and I will show you how to do it, so what I will simply do is, first of all, let's put it up a little bit like this, and let's remove it, and here we make a new secret of ours, I will name it Jarvis, and I will delete it later, for now you will see that it will not work, this key is not going to work for you, so don't even try, and if you try, it will not work, because I will delete this key while making this video, but you keep your api key safe from people, because people exhaust it and you will not even know, now you can see that we have done this, now I will simply copy this code, as I told you, I will copy the code from here to here, and after copying it, I will paste it here,
so what we did first is, we told him who he is, so I have written here, he has written, you are a poetic assistant skilled in explaining complex programming concepts with creative flair, now see here, compose a poem that explains the concept of recursion in programming, so let's see what he gives, here I do one thing, you are a virtual assistant, virtual assistant named Jarvis, skilled in general tasks like Alexa and Google Cloud, now role user or content, compose a poem that explains the concept of recursion in programming, I have written it like this, I will not ask him, I will ask him what is coding, I will run it, and hopefully it will work, let's see if it works or not, and here he told me, content is equal to, and here he told me in choices.message, what is coding, and here see coding, and its .content, I will have to do message.content too, maybe, if I am not wrong,
and here we see, chat completion example, I will write just one strength of a model, explaining recursion is available on something, okay, now I did .message, so I got a message here, but I just have to see that what he is showing me like this, role is equal to assistant, so this chat completion message object he has given me, okay, I want its content, so if I print its .content, so will I get it, let me tell you that OpenAI's API is paid, yes I got the content, OpenAI's API is paid, okay, I am telling you in advance, paid means that you will have to pay money, only then you will be able to integrate AI, and if you have not added in your OpenAI account, I am not saying that you have to do it, but I am just telling you that if you want to take your Jarvis to the next level, just add $5 to your OpenAI account, let me tell you that this $5 is going to be worth it for you, I am not selling anything of OpenAI, but I am telling you that if you do this, then you will take your projects to the next level, and if you do $5 once, then I will show you the usage, how little it is used, it is used very little, I mean I put $5 once, and my usage has never been around $1 from this account, I mean I keep playing around a little, see we have used less than $0.01, we have requested only 2, and we have used so little,
so you can understand, so come on, let's start the work, so what I will do first is that I will pull my OpenAI documentation, and I have already shown you how this work can be done in OpenAI documentation, so we had already made a file whose name was client.py, so what we are going to do this time is that we are going to make it a function, whose name we will keep OpenAI, so come on, let's bring it here, and its name comes here, so we will write here, OpenAI process, and here will come command, and we will put all these things in it, in which we will make a client, and after making a client, we will do all this of OpenAI, we will put completion API, so we will do all these things in it, so let's take imports too, we copied it, and here completion is equal to this, message is equal to this, and here what will happen, our command is going to come, instead of whatiscoding, our command is going to come, so I will write command here, and what we will do in return, we will return this message, we are going to return this, otherwise what will happen, if nothing is matched, then what we will do is, we will use AI process, and we will hold command C, and we will write output is equal to this, and finally we will speak output, so come on, let's ask something from this, we will ask from this,
Jarvis, who is Hrithik Roshan? Hrithik Roshan is a popular Indian actor who works in Bollywood films. He is known for his versatile acting skills and has starred in several successful movies such as Kehona, PR Hai, Koi, Mil Gaya, Jhoom 2, and the Kareesh series. So this AI will only work for you when you will use OpenAI and API, now I will ask Jarvis again, and this time I will ask Jarvis, who is Donald Trump? Jarvis, Jarvis, Jarvis, who is Donald Trump?
Before wrapping this video, I want to show you one more thing, and that is how you can use GTTS, so what is GTTS, it is Google's text-to-speech, simply you have to install PIP, and it is a little better than Microsoft's free text-to-speech, but let me tell you, it runs for a while for free, otherwise you have to take the subscription of Google Cloud, so if you are doing this project on a large level, then you have to use GTTS only, so how will GTTS work? These are three lines of code, so I will do one thing here, that my speak function, I will make it speak old, and make a new speak function, and I will write speak text here, and I will take this import at the top, and did I do something wrong? No, from GTTS import GTTS, and here I will do its change all occurrences, and I think I will do it like this, yes fine, GTTS is equal to save, and here its name is hello.mp3, I will make its name temp.mp3, and in GTTS I will give text, and what will it do? It will make the text's mp3, and after that I will do one more thing here,
give short responses, and now I have to play this mp3, so to play mp3, I will go to chat gpt, I will not take much time, how to play mp3 in pygame, so pygame is a library through which you can play mp3 files, so first of all we will do pip install pygame, so that we don't have any problem, pip install pygame is done, and as soon as you install pygame, it is installed here, so now what we will do is, we will put the method to play music in this function, so what we will say is, play temp.mp3, initialize it, and here our temp.mp3 will be played, and the import of pygame will go at the top, it's not a good idea to put the import in between, now see what I will do, I will run it, and I will ask it this time, what is, what is force, initializing Jarvis, Jarvis, Jarvis,
ok, so it is saying permission denied temp.mp3, now that's a problem, let me see what the problem is, permission denied temp.mp3, why, why, why, why, why, why, why, I have to see this, why is temp.mp3 permission denied, permission denied, error 13, permission denied temp.mp3, I will ask charge gpt, it has given this code, so I will ask it, why is it coming, so actually what is this, my python file is not able to access this file, which is a problem, but why is this happening, let me, let me see what the problem is, I will run it again, initializing Jarvis, Jarvis, Jarvis, ok, so permission denied, it is not able to write, why is this happening, initializing Jarvis,
For that, we will use os.remove So, it is not able to replace it So, I will import os
and os.remove.mp3 I hope this is the correct function Remove function removes
Okay, let's try this Come on Initializing Javas
Okay, so it is saying that It cannot access the file because it is being used by another process So, I will try by unloading it
I have simply unloaded it instead of load Initializing Javas Okay, it is saying that it takes no argument
Okay, so unload does not have to give any argument Okay, okay Initializing Javas
Javas Yeah What is force?
Force is a push or pull that can cause an object to move Stop or change its direction or shape It is typically measured in units such as Newtons
So, now see what is the benefit of using GTTS In GTTS, you can control speed and many other things You can change the voice
By the way, you can change the voice in this too In PyTTSX too But if you want clarity, you want professional sound
Then you can use 11Levs 11Levs is an option They also have an API
So, you can do something like this Now, loading and unloading the music is a bit difficult for me So, I will ask chatgpt
Can I announce GTTS voice directly without mp3? I think there should be some way You can use to generate speech
Okay So, here it is saying something like this You can do GTTS write to fp
Audio data I don't know It seems a bit complicated to me
But again, I think there is no direct functionality in GTTS To speak Is there any speak function?
Is there any speak function in GTTS? Because you will have to write it And then there will be a problem after that
So, they are saying something like this Do it in the temp file Like I did
But again, I think this works The work is done So, it's fun
Your PyTTSX will always be fast Because PyTTSX is Microsoft Windows You guys understand
By the way, save function works very well in Apple I have seen one thing Their pronunciation is a bit better
So, if you have a Mac or something Things will be a bit better With that said
Our first project is over Add more functionalities to it Don't use my API
And I will move on to chapter Not chapter Megaproject 2 now
So, this was Megaproject 1 Jarvis Now, I will move to Megaproject 2
Which is an awesome project You will also enjoy You will also say
What have you made Let's go to the project So, guys
This is a WhatsApp account Which I will use now To create a bot
Which will reply to messages Let's see if we can do this Using Python
So, the first thing I am going to do Is that I will open Megaproject 2 here
And to open Megaproject 2 I will simply go to the desired folder And after this
I will simply make a folder here Megaproject 2 Okay
And Megaproject 2 here I have made it in Ultimate Python Codes Now I will simply open it in VS Code
What is this? Who is messaging? No problem, brother
I am making a bot now I won't need to reply Come on, come on
So, I have opened Megaproject 2 I will make a main.py here And I am going to use
A module called pyautogui So, I will write here install pyautogui
Okay And As soon as you install pyautogui
Pyautogui will be installed Okay So, I have installed pyautogui
Now what I will do simply I will go to the pyautogui documentation So, we will simply write here
Pyautogui docs Okay And I will tell you
How to write pyautogui docs And I am going to use chat gpt Okay
So, see here What does pyautogui do? It tells you
Where is your cursor So, first of all, what we will do We will import
I won't close Megaproject 1 Because I am going to take some code from there Pyautogui
Okay And what I will do here I will write while true
And I will write Pyautogui Dot
And there was a function here Which used to tell us That
What is xy coordinate? So, what was that function? I think
Get coordinate something Let me check Let me check
Let me check I think Pyautogui dot
So, let me do one thing Why not ask chat gpt? I
If I want, I can see it from top to bottom I will get that function too General functions
If we go to cheat sheet Then maybe we get something I
Yes, position Here it is Pyautogui dot position
This function used to be Which tells us The position of our cursor
Okay So, what will I do? I will say
Look, first of all, what do we have to do? Pyautogui dot position We have to find this
Okay So, I will simply Run this program
And What will it do? Simply
In our This Page
It will drag from here to here And It will copy all this conversation
Then finally, I will give this conversation to OpenAI Which will give me a response to it So, my first
The work is Drag from here to here Okay
So, what do I do? I do one thing What will I do first?
I will take the position of all icons below So, what will we do one by one? First of all, we will come here
We will run this program And you see If I talk about this
If I talk about this Which is my Chrome icon Where is it?
In which position is it? Actually, I am not printing I should print it
A is equal to A And I should print A
I think then it will give me the position Is it giving me the position? Yes
So, as soon as my mouse moves It will give me the position So, if I talk here
16, 39, 14, 12 16, 39 16, 39
14, 12 Okay, it has given me this position Okay
Now, what will I do? That I will make it a little smaller
And I will take the position here This is 975 I will take the position here
Here Where my mouse is 1003
237 1003 237
1003 237 So, where do we have to go?
While tracking We have to go here Now, what is this position?
To see this I will move it aside And I will see what position is this
Oops! I clicked by mistake I have to keep the mouse here 2187
1258 2187 1258
2187 1258 Okay
I have to go here Now, what will I do? Simply
I I will come to chat gpt I will stop this program
I will tell chat gpt Write a py auto gui script Which will
First click Click On an icon at
This position And then Drag from
This position to this position To select a text And then copy it
To the clipboard Then take it In a variable
Okay, let's see what it does So, it is telling me That you have to take it from here to here
We will install PiperClip I will tell you what is PiperClip So, PiperClip install
What is PiperClip? I will tell you PiperClip
PiperClip is basically a Program which helps you See, I already have this installed
Which helps you To take content from clipboard Okay
So, if you want to take content from clipboard You need PiperClip Okay, now
See, it will tell us how we have to work So, I I will copy this whole program
And then I will start from there See, it has told me that How
What is happening With comments Come on, let's do one thing
Here we Let's make a new program.py So that our learnings remain
I don't want to delete anything We started from here Then we came here
So, see what it is doing Let's do one by one What is it doing?
It has imported required modules Then what is it doing after this? First, it is clicking on the icon
Which is our chrome icon I will write here Chrome icon at this coordinates
Time.sleep It is waiting for a second It is waiting for a second
Click is registered Then after this it is moving from This
To this Now it is dragging to this position And after that it is pressing ctrl c
So that you copy Okay, text Then this text came to us
And finally this text came to us So what we will do We will print this text
Let's see if this script is working or not Let's run this program Okay
Okay Dude, what happened? While dragging
While dragging Why text was not selected? If I drag here like this
So text should be selected Okay, I got the I got the point
This I I have to do like this I have to bring it down
So what I have to do I have to come up to here Up to here
I have to drag up to here Okay I have to drag from here to here
So I will do one thing That I will do a change here
So from 1003 We were coming up to 237 Then we were going to 2187
And up to 1258 Okay So I will run this program again
And I will try to see What are the coordinates here
Here What are the coordinates here Or if we come here
So what are the coordinates If I drag from here like this Or if I drag from here like this
So what are the coordinates here Okay, let's see And the coordinates here are
14991263 14991263 1499
I just make it 1499 I make it 1268 Now let's see what's the scene
Oops Something went wrong Let's run it again
Okay From here to here It didn't happen
I will reduce it a little more I will reduce it a little more I will make 1268 to 268
Now let's see I mean, our whole chat should be copied basically Oh, what happened?
So it's all wrong This is all wrong So I have to drag
I will take it again I have to start from here I have to drag
Why shouldn't I drag from here I drag from here But you know what's the problem
If we drag like this In fact, if we are dragging like this Then there is a problem
We have to drag from here All the way till here Let's try it one more time
So if I run this program It's not working It's not working from where we are starting
I will change the coordinates again But I am going to You know, get this done anyways
992207 First of all, I write 992207 992
207 And after this we will go all the way till 2208
2208 1285 2208
1285 2208 1285
1286 Let's do it I will stop this
And let's see I will stop this And let's see
Whether our work is happening or not Okay We got it
We got it We got the chat We got the chat
Now see here Is it coming? Yes
See this I have put my name in this dummy account as Naruto Now see here
What did Naruto say? What did Rohan Das say? Then Naruto said something
Rohan Das said something Now what I will do is I will speak from the API of OpenAI
API of OpenAI If you don't know about the API of OpenAI Let me tell you something
That the API of OpenAI You can go Open
AI You will search API You will get the overview of API
If you will log in Then you will get your API key Of OpenAI
It is paid I have bought it Okay
So I am able to do all this If you have not bought it So
First of all, I will recommend you You buy it It is a recharge of 5 dollars
It is a matter of 400 rupees By eating a pizza less You for a whole year
At least If you are in the first year of college Then you will make projects for free for 4 years
It takes so little expense of usage Once you should have an API key Okay
Don't steal my API key Because I will delete my API key I am telling you directly
So see here We have used this code And when we have made a client
Then what we have to do That in this way You have to give API key in OpenAI
Okay So I have already written this program for you So I will copy this program
Client.py I will make client.py here also Just so that you have
Client.py Now you see here The text is selected
It is selected I want there to be a click here That this text is deselected
So what I will do I will come to my mega project 2 I will close mega project 1
What will I do in mega project 2 I will say Brother see
Once you have done ctrl c Then what to do Click on keypyautogui.
Okay Register a click Let's see if it works or not
So yes it was selected There should be a click There should be a click please
There was not a click I don't know why There should have been a click
A click But maybe I have to tell it Where to click
So I click here And After clicking here
I hope it will deselect So see I did this here
And by clicking It should be deselected If it clicks
Okay It is not deselected It is deselected by clicking here
So I will have to do one thing I have to click here 21081286
So we started from the top We came down Then we came here
Clicked Click a little up Or how about I click here
Here So what is this You guys see
I renamed it I named it getcursors.py
I named it openai.py I named it
300sqlbot.py I will install pip OpenAI
Okay OpenAI This will install the OpenAI package
Let's install it But I come back here On the bot
And where do I have to click I have to click on such a place Where by clicking
Where by clicking All text will be deselected So I copy this and see
I'm just trying I just want Everything is fine
Okay So now what we will do here That we will run this program again
And see what happens So it came from above It selected
Then finally What did he do So he minimized it all
I work I run this This
I run this code And it is telling me that It is telling me that
Which I am at the point So I will click here
995 I will click on 994281 we will click on 994281
994 281 yes you have to do some trial and error
then only it will work for you, otherwise it will not work now it is deselected, we clicked on the right place, it is deselected now what we have to do is, once it is deselected
we got the text, now what I will do is I will say to my openEI account that you are harry you are a
person named harry, I will give it full person named harry named harry who speaks
hindi as well as english he is from india and is a coder
you analyze chat history and pretend to be harry, yes speak like harry, otherwise he will say pretending is wrong
and he will give moral knowledge chat history and talk like harry and respond
like harry now here we have to give a command so here what we will do, what we will give in command
generate, we will say generate I will do one thing you are a real person named harry
ok, analyze chat history and respond like harry let's see if it works first of all, so here I will do command
and I will write command is equal to command is equal to, I will give this chat history and I will see that by giving chat history
will it respond, so I run it let's see what response it gives me so see here, it gave, rohan seems to be a fan of
sharing music videos in indian and english it's always nice to discover new songs in different languages so here what it did
it analyzed the chat and gave something now assume, let's use it there is no need to assume
so I will do one thing, first of all I will import this I will import this line in my bot and after this what I will do
after importing this, simply I will do this completion and here
we will print this text, after printing here we have to make a client, which we have made above with our api key, so this is our openai client
now what we are doing here what is our content going to be, our content is going to be text and this chat history, if I name it chat history
chat history chat underscore history let's call it chat history
chat history and you are a person named Hari who speaks indian as well as english, she is from india
now what is this going to be this is going to be our response this is going to be our response
I will say chat gpt click at, I will see the location and paste the text
and press enter now I have to see where is this chat box I have to click here and paste the text and press enter
so what I will do, I will run this get cursor again and I will find this cursor position 18081328
18081328 18081328 18081328
so I am telling this and I am saying you have to do this, so we have to simply click here on these coordinates, it will already generate everything
it has generated the whole code, I will only take that part I will take the part of step 5 and we have to do this ctrl v and press enter the rest of the steps it has generated
I will copy that here and what we have to paste we have to paste the content of clipboard
infact I don't have to paste the content of clipboard I have to paste a given text so how will I do that
maybe I will do one thing we have done we have to copy
we have to copy this I will say this I need to paste
the content of a variable in chatbox, how will we do that so it is telling that if you want to do this then use piperclip
and it will give me the functions of piperclip so here what we have to do piperclip.copy text, so we can do this
and I will stop this piperclip.copy response so we can do something like this
so I will run this and what it has done, it has copied the chat history it has generated the response from openai
it has done something wrong naruto seems to be enjoying the mix of so we have to simply reply to the chat
and here if we get any response then we will do it but for now let's leave it like this
and let's fine tune it again because according to me we have to work on this so I told it, you are a person named Hari who speaks
I have made a mistake here, in naruto who speaks Hindi as well as English I have made a mistake here
you are from India and you are a coder and you are a coder you analyze chat history and respond like
naruto output should be the next chat response as naruto
now I will delete this message I will delete this, I will delete for me and now I will run this
and I don't want anyone to know that I am a bot it has copied, deselected now it has taken the response from openai
the song is also good I also liked it, thanks for sharing Rohan brother, brother, are you watching
now Rohan is confused, he doesn't know that I am writing all this so what I will do, I will run my script again and you see
automatically generated again it will generate from openai again and yes, I liked this mix song
the blend of Hindi and English was good I will definitely listen to it and put it in my playlist you gave me a new experience, thank you
ok, ok, let them talk it will be fun, ok I have to do one more thing I have to do this, if there is a Rohan Das colon
if there is a Rohan Das colon in its ending it means Rohan Das is talking means the last message is Rohan Das
then only you have to generate the response otherwise you don't have to generate the response I will check this now, but for now I want to enjoy it
naruto or boruto, now he understood he is checking there is no such bot
so it can't be anyone, it can be my character dedication is of a different level there is boruto
but naruto will always be number one and I will run it again I don't have to run it manually, so I will write the code quickly
before Rohan goes offline here I will run it in while true here I will run it in while true
here I will run it in while true here I will run it in while true and keep doing it
this program will continue but we will complete it if there is a Rohan Das in the last line of chat history
so what I will do here I will use chat gpt I will use chat gpt
I will say I want to check write a function to check if the last message is
from Rohan Das here a function will be written which will check this lock
if the last message is from Rohan Das here I have made a function you can see
isLastMessageFromRohan if isLastMessageFromSender because we have to change the sender
isLastMessageFromSender sender name is equal to Rohan Das if Rohan Das is coming
true or false I will say if the last message is from Rohan Das I will reply to Rohan Das
so here he copied he said send 10 crores for this ok
ok actually it is in while true so
if it is in while true it will minimize it we have to take care of this
we have to do this only once we have to do this only once ok
instead of sending 10 crores give me some coding projects I will enjoy more
now I have to take time.sleep ok now
drag to copy now what we will do
isLastMessageFromRohan sender name is Rohan Das chat history
if isLastMessageFromRohan otherwise we will not do
we will do this only when last message is from Rohan let's go chat bot is ready to talk
ok what is happening it is selecting
ok if last message is from Rohan why it is selecting
why it is not generating completion I will have to check it I will have to check it
and one more thing we will increase the wait let's see what it has done
chat history reverse message parts is equal to this
timestamp one more thing last message from Rohan Das
print isLastMessageFromRohanDas let's see
ok is it true or false last message is not from Rohan Das
why it is happening last line I got it
multiline ok I got it
I have to split it I have to split it I will split it
I will split it this message is useless it is not useless
I have taken all messages I will split it I have split it
I will say give me last message if messages.
if Rohan Das if sender name in messages return true
or else return false let's run this program ok
ctrl c ctrl c ok
I got it last message is not from Rohan Das Rohan
Rohan Rohan Rohan
Rohan Rohan and this is the response
and you can apply delay if you apply 5 second delay you can see
message said send done and now reply will come mistake
it should not have sent naruto 216 it made a mistake
till now it was sending correct but it made a mistake now it will understand
that it is a bot ok there is a problem
it is not selecting it is dragging from top to bottom He's not doing it. Why so?
I'll try it manually. Okay. Complete the project with your new Jutsu. Wait for this scene. Believe it.
Do the scene. Keep waiting for this scene. Believe it.
What? I? I want to make a good way to copy this.
I think this is a decent way, but we can make a good way to copy this. So this text was selected from top to bottom. He's replying to something.
Now see what he will do. Now see what he will do. Now he will give a response to my bot. What is he doing? He is writing Naruto again and again.
This is a mess. He will have to fix it. He will have to fix it. Naruto Naruto don't start.
I'll write. He just output should be the new chat response. Message only.
Text message only. Let's see if it works properly. Okay, okay, he has copied.
Okay, he is giving a response. Hey, what is he doing? Link colon link. Where is the link?
Hey, he's getting me caught now. Oh brother. Now the response will come from the front.
Now the response will come from the front. He is coming. He is typing. He understood. He understood.
No fun. I am Naruto. What is the scene of Bodinga Ninja?
Need some help. Close the bot. He understood.
He is always ready. Did you see friends? This bot is also helping.
Bot is also helping. Did you see friends? Bot is also helping.
Ding ding ding. Okay, guys. To make sure that I am tracking from the right place.
I will start from here. I will start from here. In fact, which always works.
I will end here. I will end right here. If I am ending here too, then there should be no problem.
Maybe. If I am dragging. And I start from here.
And I am ending here. Then there should be no problem. According to me.
Let's see. Okay. So what will I do?
I will run my same script. Get cursor one. And I will see.
What are the coordinates of these two places? First of all, I would like to start from here. I would like to start from here.
Wait a minute. Let's put it aside. Let's put it.
Where should I put it? Come on, brother. Yes.
Let's put it here. Let's start from here. 1026244
1026244 From 1026244 Where do we have to go?
We have to go to 11291323. 132 It's wrong.
What was it? Wait a minute. We have to bring it here.
11311321 1131 1131
I do it like this. Okay. Now let's see how the bot works.
Now the bot should be fine. And I am here. 1026244
1026244 1026244 1026244
1026244 1026244 1026244
1026244 1026244 1026244
1026244 1026244 Okay
Copied Okay The response will be generated.
Response is generated. If you want, let me know. The Ninja of Coding is always ready for you.
Now let's see what he will do. He will reply. Let's have some fun with Akash.
This is what the coding ninja said. I don't like it. But okay, it will work.
We can fine-tune this prompt. We can show it a little chat history. We can fine-tune.
So he's a little more.. Look, he's replying. He's replying from the front.
Let's see what happens. Oh, man, he sent it again with a template. The solution to every problem with Coding Ninja
I'll tell him. I'll put a prompt. I'll put a prompt.
The solution to every problem with Coding Ninja can be. Let's do this.
Okay, he's typing. Oh, man. Okay, that was it.
So, man, this is how you can reply. You can write a lot of good prompts. I hope you guys liked this project.
So guys, I'm so happy that we guys learned everything in this course. We learned new things that have come in Python.
Plus we made four projects. Two of which were amazing projects. You guys must have enjoyed it too.
Now I'll recommend you guys to make a GitHub profile. And at least push these four projects in it. If you don't know how to do it,
I've made a video in which I told you how you can push your code on GitHub in 10 minutes. If you want more projects like those four projects,
let me know. I'll make more videos of the Python project. Do let me know in the comment section
if you want more Python projects. Now I want to give you tips related to job search. First of all, you have to be active on LinkedIn.
You have to make your profile very good. Your profile will shout and say that you know Python. If you want to hire such a Python developer.
It should not be mixed in your profile that you are doing Python, HTML, JavaScript, React. That's not recommended at all.
Send four emails daily. Don't send bulk emails. Otherwise, your emails will go to spam.
And you won't get replies. So if you are collecting emails, if you have collected 50 emails in a day,
then you only send four emails for the job. What will happen with this is that your emails will not land in spam.
If you are using any kind of platform and writing emails, then your emails will be in spam.
If you have more than 50 emails, more than 10 emails, basically send an email in bulk,
then you will be marked as a spammer by that email service. Whether it's Gmail, Outlook, whatever it is.
Okay. You have to keep this in mind. Now if you want to go into data science,
then I have made a roadmap for data science. You must see it. In total, it is a great book
whose name is Python for Data Science. You must buy it. And if you want to go to machine learning,
then I have also made a roadmap for machine learning. You must watch that video. And there is a great book related to machine learning
whose name is Hands on Machine Learning with Scikit-Learn and Tensorflow. I will highly recommend it to you.
It's a great book. It will change your life if you want to make your ML career.
So, I am very emotional that I have completed such a big course. Emotions are pouring.
How do I tell you? I never thought that what I thought,
I will be able to convert it into a video. I mean, there are a lot of ideas in the mind, but there are very few ideas
that can be executed in a video. And I am so glad, I am so happy that
this video was made and this idea was executed. Now, from here,
you should go to good packages. See MediaPipe. If you want to do image processing,
then see OpenCV. See Django and Flask. If you want to make APIs,
then there is a package called Streamlit. You should explore all those things. Now your Python has a very strong grip.
So, from here, you can divert your career in any direction. So, I hope you guys liked this video.
Thank you so much for watching this video. And I will see you next time.
The course starts by teaching fundamental programming concepts such as variables, data types (integers, floats, strings, booleans, None), control flow with if-else statements, loops (while and for), and defining functions including recursion. These basics lay a strong foundation for new programmers to understand Python's core syntax and logic.
It covers classes and objects with a focus on attributes and methods, explains encapsulation and abstraction using instance variables and properties, and delves into inheritance types like single, multiple, and multilevel inheritance using the super() method. Advanced OOP features such as static and class methods, property decorators, operator overloading, and special methods like str and len are also included for deeper understanding.
The course introduces recent language features including the walrus operator for inline assignments, type hints for clearer code annotations, match-case statements for cleaner multi-conditional logic, enhanced exception handling with try-except-else-finally blocks, custom exceptions, and managing project dependencies through virtual environments and requirements files.
Yes, the course includes several hands-on projects such as building a Jarvis Virtual Assistant with voice recognition and API integration, developing a 'Guess the Number' interactive game to practice control flow, and creating a WhatsApp Auto-Responder Bot using PyAutoGUI alongside AI-powered replies through the OpenAI API. These projects reinforce concepts with real-world applications.
It emphasizes hands-on coding exercises after each chapter to reinforce learning, recommends effective use of AI tools like ChatGPT for programming assistance, encourages documenting work with notebooks and cheat sheets, and provides advice on job search strategies including optimizing LinkedIn profiles and targeted applications to prepare learners for careers.
Yes, it thoroughly covers lists, tuples, dictionaries, and sets including their creation, indexing, mutability, and common methods like append, insert, update, and set operations. String manipulation techniques such as slicing, concatenation, and useful methods like .startswith() and .replace() are also detailed to help manage and transform data effectively.
For those interested in expanding skills, the course recommends related materials like a comprehensive AI course covering machine learning and deep learning, an algorithms and data structures overview to strengthen problem-solving skills, a CS50 course for web development expertise, and a guide to Python Pandas for advanced data inspection and cleaning techniques, providing a well-rounded learning path.
Heads up!
This summary and transcript were automatically generated using AI with the Free YouTube Transcript Summary Tool by LunaNotes.
Generate a summary for freeRelated Summaries
Comprehensive Artificial Intelligence Course: AI, ML, Deep Learning & NLP
Explore a full Artificial Intelligence course covering AI history, machine learning types and algorithms, deep learning concepts, and natural language processing with practical Python demos. Learn key AI applications, programming languages, and advanced techniques like reinforcement learning and convolutional neural networks. Perfect for beginners and aspiring machine learning engineers.
Comprehensive CS50 Course Review: From Basics to Web Development
Explore the full journey of Harvard's CS50 course, covering foundational programming concepts, data structures, Python, SQL, web development with HTML, CSS, JavaScript, and cybersecurity essentials. Gain insights into practical applications, debugging, and best practices for secure coding and user interface design.
Unlocking the Power of Go: A Comprehensive Programming Course for Beginners
Learn Go programming with our comprehensive course for beginners. Master the fundamentals and build real-world projects!
Understanding Introduction to Deep Learning: Foundations, Techniques, and Applications
Explore the exciting world of deep learning, its techniques, applications, and foundations covered in MIT's course.
Master Tableau: Comprehensive Guide to Data Visualization & Dashboards
This extensive Tableau course covers everything from basics to advanced topics, including data modeling, calculations, chart types, dashboards, and real-world project implementation. Learn to create dynamic, interactive visualizations and dashboards with over 60 functions and 63 chart types, optimized for business intelligence and data analysis.
Most Viewed Summaries
Kolonyalismo at Imperyalismo: Ang Kasaysayan ng Pagsakop sa Pilipinas
Tuklasin ang kasaysayan ng kolonyalismo at imperyalismo sa Pilipinas sa pamamagitan ni Ferdinand Magellan.
A Comprehensive Guide to Using Stable Diffusion Forge UI
Explore the Stable Diffusion Forge UI, customizable settings, models, and more to enhance your image generation experience.
Pamamaraan at Patakarang Kolonyal ng mga Espanyol sa Pilipinas
Tuklasin ang mga pamamaraan at patakaran ng mga Espanyol sa Pilipinas, at ang epekto nito sa mga Pilipino.
Mastering Inpainting with Stable Diffusion: Fix Mistakes and Enhance Your Images
Learn to fix mistakes and enhance images with Stable Diffusion's inpainting features effectively.
Pamaraan at Patakarang Kolonyal ng mga Espanyol sa Pilipinas
Tuklasin ang mga pamamaraan at patakarang kolonyal ng mga Espanyol sa Pilipinas at ang mga epekto nito sa mga Pilipino.

