Python Tutorial for Absolute Beginners #1 - What Are Variables?

CS Dojo
26 Dec 201724:38

Summary

TLDRIn this introductory Python tutorial for absolute beginners, YK, the founder of CS Dojo and a former Google software developer, guides viewers through the basics of Python, its applications, and how to install it using Anaconda. The video covers setting up a Jupyter Notebook, understanding variables, and demonstrates swapping values between them without repetition. YK also provides a practice problem to reinforce learning, encouraging viewers to engage with the material and subscribe for future tutorials.

Takeaways

  • 😀 YK, the founder of the CS Dojo YouTube channel and a former Google software developer, introduces a new Python tutorial series for absolute beginners.
  • 🌐 The series is aimed at those with no programming experience as well as those who want to expand their knowledge of Python, regardless of their background.
  • 🐍 Python is highlighted as one of the most popular programming languages, used by companies of all sizes, including Google, and in many university computer science courses.
  • 💡 Python's simplicity and versatility are emphasized, with applications ranging from web development to data analysis and scientific research.
  • 💻 The tutorial focuses on installing Python and setting up a development environment using Jupyter Notebook, a tool popular in the scientific community.
  • 📘 YK explains how to use Jupyter Notebook through Anaconda, a package manager that simplifies the installation of Python and related libraries.
  • 🔧 The video demonstrates creating a new Jupyter Notebook, navigating the interface, and writing basic Python code, including the use of the 'print' function.
  • 🔑 Variables in Python are introduced as references to values, rather than containers, which is a key concept for beginners to grasp.
  • 🔄 The tutorial includes a practical exercise on swapping the values of two variables without repeating string values, illustrating the concept of variable assignment.
  • 📝 YK provides an outline of the video content in the comments and encourages viewers to subscribe to the newsletter for updates on future tutorials.

Q & A

  • Who is the speaker in the video and what is their background?

    -The speaker is YK, the founder of the YouTube channel CS Dojo and a former software developer at Google.

  • What is the purpose of the video series?

    -The series is designed for absolute beginners in programming as well as those who have some programming experience but want to learn more about Python.

  • What is the main topic of the first video in the series?

    -The first video is about 'What are variables?' in Python.

  • Why is Python considered easy to learn for beginners?

    -Python is considered easy to learn due to its simple syntax.

  • What are some applications of Python mentioned in the video?

    -Python is used for websites back-end code, data analysis, scientific research, and is commonly the introductory programming language in computer science courses at universities.

  • What is an IDE and why is PyCharm Community Edition recommended in the video?

    -An IDE is an Integrated Development Environment that includes everything needed to develop a program. PyCharm Community Edition is recommended because it is a popular IDE for Python development.

  • Why is Jupyter Notebook used in the series instead of a traditional IDE?

    -Jupyter Notebook is used because it is simple, easy to install, and popular for quick writing and testing of programs, especially in scientific and data analysis communities.

  • How is Jupyter Notebook launched and what does the server represent?

    -Jupyter Notebook is launched through an application like Anaconda Navigator. The server represents the core of Jupyter Notebook and should not be closed to ensure the notebook continues to function.

  • What is Anaconda and why is it used to install Python and Jupyter Notebook?

    -Anaconda is a package manager used for installing math and science libraries. It is used because it comes with Python and Jupyter Notebook, eliminating the need for separate installations.

  • What is the significance of the 'print' function in Python as demonstrated in the video?

    -The 'print' function is used to output data to the console. It is demonstrated to show how to display strings and numbers in Python.

  • How are variables defined and used in Python according to the video?

    -Variables in Python are defined using the '=' operator to assign values. They are used to store and refer to data, and their values can be reassigned or changed throughout the program.

  • What is the correct way to swap the values of two variables in Python without repeating the values?

    -A common pattern is to use a single temporary variable to hold one of the values during the swap, then reassign the variables to each other's original values.

  • What is the error that occurs when trying to print a variable that hasn't been defined?

    -A 'NameError' occurs, indicating that the name of the variable is not defined yet.

  • How can you assign the value of one variable to another without changing the original value?

    -You can assign the value of one variable to another by directly using the variable name on the right side of the assignment, for example, 'f = a'.

  • What is the main takeaway from the practice problem on swapping two variables?

    -The main takeaway is understanding the concept of variable assignment and how to swap values without repeating strings or losing data.

Outlines

00:00

😀 Introduction to Python Tutorial Series

YK, the founder of CS Dojo and a former Google software developer, introduces a new Python tutorial series aimed at absolute beginners and those wanting to learn more about Python. The series will cover Python basics, installation, setting up a development environment with Jupyter Notebook, using the print function, and understanding variables. YK also provides an outline in the comments and offers a newsletter subscription for tutorial updates.

05:04

🛠️ Installing Python and Setting Up Jupyter Notebook

The script explains how to install Python and Jupyter Notebook using Anaconda, a package manager for math and science libraries that includes Python. It guides users through the installation process, choosing Python 3, and launching Jupyter Notebook via Anaconda Navigator. The tutorial also covers the basics of Jupyter Notebook's interface and how to create and run a new Python 3 notebook.

10:07

📝 Understanding Variables in Python

The video delves into the concept of variables in Python, demonstrating how to assign values to variables and how to print those values. It explains the difference between variables in Python and in other languages, emphasizing that in Python, a variable is more like a name tag referring to a value rather than a box containing the value. The tutorial also shows how to assign different data types, such as numbers and strings, to variables.

15:08

🔄 Reassigning and Referencing Variables

This section discusses the ability to reassign variables to new values and how multiple variables can reference the same value. It also explores the concept of variable referencing, where one variable can refer to the value of another, and the implications of reassigning those values. The importance of careful variable management to avoid unintended changes in value references is highlighted.

20:11

🔄 Swapping Variable Values and Conclusion

The final part of the script presents a practice problem on swapping the values of two variables without repeating string values. It explains common mistakes and introduces a solution using a temporary variable to swap values correctly. The tutorial concludes with instructions on how to download the files used in the video and how to subscribe to the newsletter for future updates, inviting viewers to share their thoughts in the comments.

Mindmap

Keywords

💡Python

Python is a high-level, interpreted programming language known for its readability and efficiency. In the video, it is described as one of the most popular programming languages, used by companies of all sizes, including Google, and in many university computer science courses. The script emphasizes Python's ease of learning due to its simple syntax and its versatility in applications such as web development, back-end coding, and scientific research.

💡Variables

Variables in programming are used to store data values. The video explains variables as a fundamental concept in Python, where they act as 'name tags' referring to different values. The script uses examples like 'a = 1' to illustrate how variables can be assigned values and how they can be reassigned to different values, highlighting the dynamic nature of variables in Python.

💡Jupyter Notebook

Jupyter Notebook is an open-source web application that allows for interactive computing. It is used in the video as the development environment for Python. The script explains that Jupyter Notebook is popular in the scientific community for data analysis and is chosen for its simplicity and ease of installation through Anaconda. It is used to write and test Python code interactively.

💡Anaconda

Anaconda is a package manager and distribution of the Python and R programming languages for scientific computing. In the video, it is used to install Python and Jupyter Notebook. The script walks through the process of downloading and installing Anaconda, which includes Python 3 and Jupyter Notebook, simplifying the setup process for beginners.

💡IDE (Integrated Development Environment)

An Integrated Development Environment is a software application that provides comprehensive facilities to computer programmers for software development. The video mentions IDEs in the context of software development tools, recommending PyCharm Community Edition, but opts for Jupyter Notebook for its simplicity and interactive capabilities.

💡Print Function

The 'print' function in Python is used to output data to the screen. The video introduces the 'print' function as a way to display the contents of variables, strings, and numbers. It demonstrates the function with examples like 'print("hello world")' to show how to print strings and 'print(3)' to print numbers.

💡String

A string in Python is a sequence of characters. The video explains strings as a type of data that can be stored in variables, using examples like 'c = "hello there"'. It highlights that strings can be enclosed in either double or single quotes and are used to represent text.

💡Assignment

Assignment in Python is the process of binding a value to a variable name. The video script uses the '=' operator to assign values to variables, as in 'a = 1', which assigns the integer value 1 to the variable 'a'. Assignment is a core concept in programming, allowing for the manipulation and storage of data.

💡Reassignment

Reassignment in the context of the video refers to changing the value that a variable refers to after it has been initially assigned. The script demonstrates this with examples such as 'b = 2' followed by 'b = 1', showing that 'b' initially refers to 2 but is later reassigned to refer to 1.

💡Temporary Variable

A temporary variable is used to hold a value for a short period during a computation. In the video, the concept is introduced as part of a solution to swap the values of two variables without repeating the values. The script uses a single temporary variable to hold one of the values during the swap process, illustrating a common programming pattern.

Highlights

Introduction to a new Python tutorial series for absolute beginners by YK, founder of CS Dojo.

YK's background as a former software developer at Google.

The series is suitable for complete beginners and those with prior programming experience looking to learn Python.

Explanation of what Python is and its applications in various industries and academic fields.

Advantages of Python, including its ease of learning due to simple syntax.

Instructions on how to install Python on a computer using Anaconda.

Introduction to Jupyter Notebook as the development environment for the series.

How to set up and use Jupyter Notebook through Anaconda.

Overview of the Jupyter Notebook interface and its components.

Creating the first Python program using Jupyter Notebook.

Understanding the 'print' function and how to use it to display output.

Introduction to variables in Python and how they differ from variables in other programming languages.

Demonstration of assigning values to variables and the concept of variable referencing.

Explanation of how to reassign variables to new values and change their references.

The concept of variables pointing to the same value and the implications of reassignment.

A practice problem to swap the values of two variables without repeating strings.

Solutions to the practice problem using temporary variables and the concept of variable reassignment.

Emphasis on the importance of understanding variable assignment and reassignment for programmers.

Conclusion and invitation to subscribe to CS Dojo's newsletter for future tutorial updates.

Transcripts

play00:00

Hey everyone, my name is YK. And i'm the founder of this YouTube channel CS

play00:04

Dojo, and i'm also a former software developer at

play00:08

Google and this is my, new

play00:09

series python

play00:11

Tutorial for absolute beginners and this is my video number 1: what are variables?

play00:16

So this series is gonna be good for complete beginners, who have never done any programming before as

play00:22

Well as someone who's done some programming before whether it's python or any other language but

play00:28

Wants to learn more about python. So in this video i'm going to go through a number of different things

play00:33

First of all what is python and what can

play00:35

you, do with it and then i'm gonna explain how. To install python on your computer and then set up a development environment.

play00:43

We're gonna use something called jupyter notebook throughout, this series and then i'm gonna explain how. To use the print function

play00:49

And what are variables and how to use them and by the end of this video if you have two variables

play00:56

Storing different things, you'll be able to swap the content within those two variables with

play01:01

Each other, and i'm gonna put on

play01:03

an outline of this video in the comment section below

play01:05

So you, don't have to watch the whole thing if you, don't want to. Oh and before we get started if you?

play01:11

Want to make sure that you don't, miss my future tutorial videos sign up to my, newsletter which is available in the description?

play01:17

Below so just quickly, what is python and what can?

play01:21

You, do with it it's one of the most popular programming languages out there and it's used at smaller companies as

play01:27

well as larger companies including google and many

play01:31

Universities use python as the introductory programming language in their computer science courses and one advantage of python

play01:38

Is that it's fairly easy to learn because it has fairly simple syntax

play01:43

It's used for many different applications

play01:46

Including websites back-end code which is the code that runs on your server as

play01:51

Opposed to the code that runs on your device whether it's a phone or a Laptop and it's

play01:56

also popular for their analysis and scientific research purposes, okay?

play02:01

So let's now go ahead and install python on your computer usually

play02:05

When people develop a program using python or any other language for that matter they usually

play02:11

Use something called ide which stands for integrated development environment. It's an environment that includes

play02:18

Everything you need pretty much to develop a program. It has a nice text editor in which you can

play02:24

Write your code and then the ability to run your program as, well as something called a debugger

play02:30

Which allows you to quickly get rid of all the bugs in your program and if you, want to use an IDE to follow

play02:36

My series i personally recommend park charms community edition so in this series instead of using a traditional

play02:43

IDE i decided to use something called jupyter notebook

play02:47

So jupyter notebook is an environment for writing and testing your program quickly and

play02:52

It's actually, popular with the scientific community and for data analysis purposes but i've decided to use jupiter notebook throughout

play02:59

This series because it's very simple and easy to install

play03:03

Jupyter notebook through something called anaconda and it's fairly easy to use as

play03:08

Well so here's the way, jupyter notebook, works there are two components to jupyter notebook

play03:15

First of all when you launch jupyter notebook on your computer you might see a command line interface like this

play03:22

This, represents the jupyter notebook server

play03:25

You can, think of it sort of like the core of jupyter notebook and you don't have to worry that much about how

play03:31

it works exactly

play03:32

But just remember that if you close this window. Maybe accidentally jupyter notebook might stop working so just be careful about that

play03:41

Now, when you launch jupyter on your computer you might?

play03:44

also See a browser window or tab showing up whether it's Chrome, Firefox, Safari, or anything else and

play03:51

It'll probably be at the url

play03:54

Localhost call eight eight eight eight or something like that and it'll be connected to the jupyter notebook server

play04:01

this

play04:01

Browser window or tab is basically the user interface for jupyter notebook and you can write and execute your code

play04:08

Here but it's actually executed on the jupyter notebook server, again. You don't have to worry that much about, how

play04:15

It works exactly but this is just an overview

play04:19

Now to install python and jupyter notebook we're, going to use something called anaconda

play04:24

Anaconda is something called a package manager

play04:27

which allows you to install many programs at once

play04:30

This, particular package manager' anaconda is actually used for installing math and science libraries but

play04:38

You, don't have to worry, about that the only thing you need to know

play04:40

Is that when you install anaconda it comes with python and jupyter notebook so you

play04:46

Don't have to install python or jupyter notebook separately on your computer

play04:50

Let's now install python and jupyter through anaconda go to anaconda org, and then click download

play04:58

anaconda and

play04:59

Select whatever platform you're using whether it's windows mac or linux

play05:04

I'm using mac here so i'm gonna click mac os here and there are two options here

play05:10

Python, three-point-something or python 2 point something, make sure to use python 3 point something because we're

play05:17

going to use python 3 instead of python 2 throughout

play05:20

This, course so click download and then save this file wherever

play05:24

You, want to save it and once this file is downloaded just open it and then click continue continue continue

play05:33

Agree with everything and then select

play05:37

Install for me only or install on a specific disk it doesn't matter which one you choose and then click install and

play05:44

This, process will probably take a, while for you

play05:47

Once this process is done you should see something like the installation, was completed successfully

play05:53

Once you see that just click close and you're all set to launch jupyter you need to launch an application called

play06:01

Anaconda navigator

play06:03

Just launch that in the usual way you launch any other application, and then find jupyter notebook and click launch

play06:14

Like i said before you see a browser tab or window. Opening up and it should be at the url

play06:21

Localhost colon 8080 8th 8th or something like that

play06:25

Ok, let's now. Create our first program

play06:28

First go to whatever the folder you, want to create. Your first program, in i'm going to go to desktop

play06:36

And once you click it you can see that my current location is desktop because it's at the top right here

play06:43

Before you create. Your first program here i personally recommend that you create a

play06:48

New folder so i just went to my desktop i clicked right click and then, new

play06:54

Folder i'm gonna call this folder

play06:56

python, tutorial 1 and

play07:00

once you create that folder you should see in the jupyter notebook interface to click that and you see that the current location is

play07:08

Python, tutorial one within desktop right here at the top to create a, new, file or a, new?

play07:15

notebook, file as i say just click new at the top right hand corner and then click python 3

play07:23

So a new notebook has been created, here change the name of this notebook from untitled to

play07:29

Let's say, what are our?

play07:31

variables click rename and

play07:34

Once you, do that if you go back to desktop and the folder that

play07:38

we just created python tutorial 1 you'll see a, new

play07:41

File what our variables dot i pi and b and that i pi and b is just an extension for algebra notebook?

play07:49

Now in jupyter notebook there's something called a

play07:52

Cell each cell represents a set of code and the box you're looking at right now is one cell

play07:59

So you can, type in any python code here for example print

play08:04

Parentheses double quotes hello world and when you run, this? Cell

play08:09

Using this button at the top it executes all the code within the. Cell, so let's run, this. Cell and

play08:17

You, see that the string hello, world, is printed so this text hello?

play08:22

World is something called a string it's usually enclosed by either double quotes or single

play08:27

Quotes and it's basically just a set of characters so

play08:31

Assuming that this is your first time coding in your life i'm going to explain

play08:35

this, line a little bit more carefully so this says print whatever is in the parentheses these two parentheses and

play08:43

that happens to be

play08:45

Hello, world a string and when you run it again it's printed right below. This

play08:50

Cell how the world and the important thing to note here

play08:52

Is that if you

play08:53

Don't type in the exact set of characters that you see on the screen it might

play08:58

Not go right so for example if you forget to close the parentheses and run the

play09:03

Cell you'll get an error and if you use

play09:06

for example

play09:08

Curly, brackets instead of regular

play09:11

Parentheses you, also get an error and if you forget to close the

play09:15

Quotation, marks it's the same thing so

play09:18

Let's try, using this print function. A little bit more in

play09:21

This, cell that you see right here in the green, box you can, type in print double quotes more string and

play09:30

Actually instead of using double quotes you can, also use single quotes in python and then you can run this. Cell and

play09:38

The string more string is printed

play09:41

So you can, use single quotes and double quotes pretty much interchangeably in python and then you can

play09:47

Also, print not just a string but, also a number so you can, say print parenthesis 3 and then when you run

play09:55

The, cell the number 3 is printed and you could, also have multiple lines within the same cell as, well

play10:02

So in this. Cell you can, write print

play10:06

let's use double quotes here more string and

play10:10

then print 3

play10:12

So when you run, this. Cell these two lines are executed one by, one

play10:18

so you see more string and

play10:21

Three print it just one after another

play10:24

So let's now dive into our main topic here namely, what are variables

play10:29

Before i explain, what our variables exactly in python i'm going to show. You some sample code you can write a

play10:36

Equals 1. And what this means?

play10:39

Is that assign this value 1 this number to the variable named a

play10:45

You can, think of it it sort of like the variable a

play10:49

Containing the value 1 that's not 100%

play10:52

Technically accurate, as i'll explain later but that's one way to think, about it so when you run this

play10:57

Cell you, won't see anything printed this time but the variable a now

play11:02

Contains the value 1 or a more technically accurate, way of thinking about it is that the bar a

play11:10

Refers to the value one and you can. Check, what's inside the variable a

play11:15

by

play11:15

Printing the variable with print parentheses a and note here that there are no double quotes or single quotes around

play11:23

This character. A, and that's because a is not a string it's a variable and when you run, the?

play11:29

Cell you see the value a is referring to which is one and you can

play11:34

Do the same thing with different variables so you

play11:37

Can write b equals two so the convention here is you write b space and then

play11:44

Equal space and to

play11:46

This, line says, assign the value 2 to the variable b and when you run, the

play11:52

cell again the variable b refers to the value 2 now so if you haven't for example c in this code and

play12:00

If you, want to know, what's inside the variable or what the variable refers to you, can, write just like before print

play12:07

Parentheses be no single quotes or double quotes

play12:10

Run this. Cell with this button right here and then you'll see the value b is referring to

play12:17

Just like before, we can, write multiple lines within a single cell right here

play12:21

By, writing print parentheses a

play12:24

print

play12:25

Parentheses b and when you run, this, cell

play12:29

You'll see one and two so one is what a is referring to and two is what b is referring to?

play12:37

So a variable doesn't necessarily refer to a, number it could refer to a

play12:43

String so if you write for example c equals either double quotes or single quotes hello

play12:50

there

play12:51

And when you run the, cell?

play12:54

C now, we first see the value or the string hello there and when you print c?

play13:00

You should see how low. They're printed

play13:04

So i just ran this. Cell and the string hello, there has been printed, so let's now

play13:09

Quickly, talk about

play13:09

What variables are in python when you write a equals one in a different language for example c or c++

play13:17

The correct, way to think, about it might be as a, box so you have a, box a

play13:24

Containing the value one but in python this is not the accurate, way to think about it the more accurate way to think

play13:30

About it is that a is more like a name tag and this can refer to any value you

play13:36

Want and when you say a equals one you're saying a refers to the value one now. These two

play13:43

Ways of thinking, about it they might not seem that different right now but it's

play13:47

Going, to be more important later and in python you can create a, new variable, by writing as

play13:52

We saw b equals 2 and this says the name b or the variable b now

play13:59

We first see the value 2 and just like you saw

play14:03

A variable can refer to a string as, well so if you write c

play14:07

Equals hello, there you're, saying the variable c refers to the string. Hello

play14:12

There once you master that basic concept you'll be able to move on to a slightly more advanced topic

play14:19

Let's say you run, these lines of code and then you, want to run more code for example d equals 2

play14:26

Then what happens is the variable d of course refers to the value to

play14:32

Which the variable b also refers to

play14:35

so it's totally possible for two or more variables in

play14:39

Python to point to the same value in this case 2 and then it's

play14:44

Also possible for you to reassign an existing variable to another value so if you

play14:50

write b equals 1 after

play14:54

executing this line of code b equals 2 after this line b refers to two of course but, after writing b equals 1

play15:02

B refers to 1, which a, also refers to and you can

play15:07

Even reassign an existing variable for example b which once referred to a number

play15:14

One right here to something else for example a string so if you, write for example b equals double quotes ah

play15:22

The string the variable b now refers to

play15:26

The string ah

play15:28

So let's see how, this actually works out in code

play15:31

Okay, we're gonna continue on the code, we had earlier earlier, we had b equals

play15:38

so if you print be

play15:40

the variable

play15:41

We get the value to and you can reassign

play15:45

This variable to another value for example 1. By writing b equals 1 and then when you run the. Cell, and

play15:53

print b again

play15:56

you get

play15:58

One printed the new value and what happens if we try to print a variable that doesn't exist yet

play16:04

So if you try to print print

play16:07

Parenthesis, a what happens let's run, this

play16:10

Cell and see what happens it'll actually give you an error because he doesn't exist yet and the error says

play16:16

Name error name e is not defined yet so that makes sense let's fix that. By writing e

play16:23

equals this is a string and

play16:26

This, way the value this is a string is assigned to the variable e and the variable e exists in the system

play16:34

So if you print e with print parenthesis e and when you run this

play16:39

Cell you, don't get the error anymore and let's address another question

play16:43

You might have here is it possible for us to assign a variable

play16:48

to

play16:49

Another one the answer is yes so here if you write

play16:53

Equals 1 a of course refers to 1. And then c equals hello, world c refers to

play17:00

hello world

play17:01

What happens if you write f equals a?

play17:05

What happens then is this, means the variable f the name f refers to?

play17:12

The value a is referring to so f refers to one now and so f?

play17:20

doesn't refer to the variable a

play17:23

Instead it refers to the value a is referring to and this is important for example when you do a

play17:31

Equals two if you write that a now refers to

play17:35

the value 2 instead of 1 but f

play17:40

Stays at the value 1 and this might not be the case if f was referring to the variable a

play17:46

Let's take a look at another example here if you write g iko see the variable g will refer to

play17:54

Whatever the value c refers to which is how it world and then once you write

play18:00

C equals, hello c will refer to the new value

play18:05

Hello, right here

play18:07

But the variable g will, stay at the value hello world

play18:12

Okay, let's go back into the demo earlier in the demo, we had a

play18:17

equals one and

play18:19

Then c equals hello there

play18:23

So once we print a and c. With print parenthesis a, and print parenthesis c

play18:29

We should see one for a and hello there for see what happens if we write f?

play18:36

equals a

play18:38

F should now refer to whatever a refers to

play18:42

So if we print f?

play18:45

We should see the number one printed and?

play18:49

What happens if we assign a to two?

play18:52

then

play18:53

The value of a should, now be two and then the value of f should, stay as i said earlier

play19:02

at one

play19:03

Okay, now i'm gonna give you a quick practice problem to work on in order to solidify your understanding of variables and to do that

play19:10

Go, back to the previous tab of jupyter notebook the navigation

play19:14

Menu and if you already, close the tab

play19:17

Just open localhost colon 8080 eight eight or just relaunched

play19:21

jupyter notebook

play19:22

And then go back to the same folder as before we had python tutorial one click new in the top right corner click

play19:30

python 3, again and

play19:32

Let's rename this notebook. By clicking untitled and by changing it to

play19:38

swapping two variables and

play19:42

Here's the problem you're given two variables let's say

play19:47

V1 equals first string in double quotes and v2

play19:51

Equals second string, again in double quotes how

play19:54

Can you, swap the values within, these two variables with

play19:57

Each other the easiest way to solve this problem is just to write v1 equals second string and

play20:04

Then b2 equals

play20:06

First string, but this might not be the best solution

play20:11

Because if you had a much longer string it would be kind of cumbersome to repeat these strings and

play20:17

Then if one of the strings changes so if first string becomes first two strings

play20:23

That you need to rewrite your code right here to

play20:27

Match that to write first two strings again

play20:30

So ideally you, want to be able to solve this problem without repeating the strings think

play20:36

About it for a second and then i'm gonna show

play20:38

You, what a good solution might look, like, when you see this problem you might say, well it's actually pretty simple

play20:44

Once we have b1 equal to first string and v2 equal to

play20:50

Second string, so that's v1 referring to this string and v2 referring to this string, we can just write

play20:58

V1 equals v2 and then v2 equals v1 and we're done but actually this solution is wrong

play21:05

Because when we execute this line v1 equals v2

play21:10

V1 now refers to whatever v2 refers to of course, which is second string and then when?

play21:16

We write v2 equals v1?

play21:19

V2 will now refer to what v1 refers to now

play21:22

Which is second string so at the end of these lines of code?

play21:27

Will have v1 and v2 both refer to the second string which is not what, we want?

play21:33

Let's now see what a? Good solution might look, like

play21:36

I'm going to give you a few different options here the first option is to use two temporary variables we're

play21:43

Gonna call them temp 1 and temp

play21:45

2 temp 1 will be equal to

play21:48

What v1 refers to so that's first string and then temp, 2 will be equal to what v2 refers to

play21:56

Which is of course second string and then we'll just need to swap them together

play22:02

So v1 will be equal to temp 2, which refers to second string so v1 will now refer to

play22:10

Second string

play22:11

After this line right here and then v2 will be equal to temp 1 which of course first string

play22:19

So v2 will refer to

play22:22

First string, after these lines so this is one potential solution but actually

play22:27

We don't have to use two temporary variables

play22:30

Okay, here's a solution that only uses one temporary variable instead of two the first thing we're gonna do is we're gonna set a

play22:37

New, temporary variable called temp - what v1 refers to

play22:42

so temp

play22:44

will be this string right here and

play22:48

After that we'll set v1 to v2

play22:51

So v1 will no longer refer to first string. And then it will refer to second string instead and at

play22:58

This point note that the first string this string is not lost yet because

play23:03

We have a variable pointing to that value so after that all you need to do is

play23:08

Refer v2 back to whatever temp refers to which, is first string so v2

play23:15

Will refer to first string, after these lines of code and the v1 will refer to second string and

play23:22

this, little pattern of using a single variable to switch the values of two variables is

play23:29

Actually really important to know because it's a very common pattern to use for any programmer

play23:34

Okay, let's now test that pattern with our python code so in the second sale we're going to write our solution

play23:40

We're gonna create a, new variable called temp, which refers to whatever v1 refers to which

play23:46

Is first string, and then we'll set v1 to be v2

play23:51

So v1 at this point is second string and then we'll set v2 to whatever

play23:57

Temporary first - which is first string so at this point

play24:00

Once we print v1, we should be able to print second string?

play24:05

We do and then if we print v2 which would see first string and?

play24:11

We do okay, to download the files i created, throughout, this video just go to cs. Dojo io

play24:18

Python 1 or if you just, want to subscribe to my, newsletter so that you don't

play24:23

Miss my future tutorial videos just go to cs. Dojo dot io

play24:27

News and as always let, me know in comment below, when you thought of this video i'm? Yk, from cs

play24:34

Dojo, and i'll see you in the next video?

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
Python TutorialCoding BasicsSoftware DevelopmentGoogle DeveloperJupyter NotebookVariable AssignmentData AnalysisScientific ResearchAnaconda InstallationIDE OverviewProgramming Beginner
Benötigen Sie eine Zusammenfassung auf Englisch?