Python Course guide |variable assignment |

In this blog, we’re going to be discussing variable assignments. Now we just saw how to work with numbers. But what do these numbers represent? We had integers and floating point numbers

But you have a variable name assigned to them. So it’d be nice if we can assign these particular data types a variable name to easily reference them later on in our code. For example,

I could say a variable name my underscore dogs is equal to two because I have two dogs. Now there are a couple of rules for choosing a variable name in Python. And these rules are that names cannot start for a number. There can also be no spaces in the variable name so you should use an underscore instead. And you also can have any of these symbols in a name. And if you forget this list of symbols if you were to type one of these symbols out in a variable name Python would quickly complain and you’d have an error. So you don’t need to worry about memorizing all these. You’d get the air as you’re typing along with a few more rules about variable names.

python source code.

It’s generally considered best practice according to pep eight that names are lowercase. Now there are situations when you become a more advanced programmer. Where you are going to want to have a kind of global variable names in all caps that are used to write your code. But right now, in general, want to keep our names lowercase, and we also want to avoid words that have a special meaning in Python, and these are built in keywords like a list or as for string. You may be wondering well how the heck is I supposed to know what are the appropriate built-in keywords. Luckily any development environment that’s designed to work with Python will have syntax highlighting that will alert you that using a built-in keyword by highlighting a different colour and we’ll see an example of that in just a little bit. Before

we jump to the Jupiter book though I want to mention that Python uses dynamics exciting and this means you can reassign variables to different data types and this makes Python very flexible in assigning data types. And that’s different than many other programming languages that are statically typed.

So let me show you an example of what I mean by this in python something like this is OK. Here I’ve assigned my dogs variable name equal to two and then later on in my code I went ahead and reassigned the same variable name my dogs to a completely different data type list. Sammy and Frankie. Now that’s OK in Python but in other languages that would produce an error. And that’s because these other languages are statically typed meaning in the other language such as C plus you’d have to say I.A. for integer and then say my dog is equal to whatever integer value want such as 1 and then later on your code

You would not be able to assign a different data type. You would not be able to say My dog is equal to Sammie because it’s no longer an integer. Now to result in an error. So there are some pros and cons to dynamic typing in Python. The prose is not having to write out the actual data type. Saves you a lot of time and makes it easy to produce Python code quickly and it also makes your code very readable because you’re just reading that variable name. Now, this kind of a double-edged sword here because the cons are that this may result in bugs for unexpected data type because you’re not having these restrictions of data types especially when you’re dealing with user input. You may have unexpected data type show up, and that can cause problems later on in your operations.

So you should be aware of the data types as you’re coding and you can use a special type function that’s built into Python to check the type of any variable quickly and will show you how to use that. And just a little bit. All right let’s explore all these concepts by jumping to a different book. OK now that we’ve seen how to use numbers in Python as a calculator let’s see how we can assign names and create variables.

We’re first going to create a very simple variable called A and set it equal to

5. And now that I’ve run that anywhere in my code when I call a. It now assigns the variable 5, and I can reassign it simply by saying A is then equal to something else like 10. And now if I check it has ten there, and I can also add now objects together, I could say a plus A., And that’s going to result in 20 because ten plus 10 is equal to 20. And Python also allows you to do reassignments concerning the same object. Let me show you what I mean by that. I could say A which is still equal to 10 I could reassign it to be say something like A is equal to a plus A. So what that is saying is take the current value of A which is 10 and reassign it to a plus A., So that’s ten plus

10. So after I run this is now going to be equal to 20. And keep in mind if I were to run this cell a second time. So I noticed the in operator here it’s going to go from 40 to 42. If I rerun it’s 40 now, and you can keep doing this again and again, and you’ll keep seeing it essentially double each time. So keep that in mind. This is a little different than in a scripting environment if you’re running a great script you don’t see that effect because you’ll have that line once in a cell environment.

You’d have to run that cell over and over again. OK. So let’s imagine that we don’t know what type is a where you can do is use the built-in type function, so that’s type have open in close parentheses, and we’ll learn how to create our functions later on. But passing the variable there do shift enter, and you’ll get back Python’s built-in keyword for what the type is. And in this case, it’s I.A. because it’s an integer. Let’s reassign it to be a floating point number. So we’ll say thirty points one. Let’s check the type of that type of a, and it returns that float. So these are the same keywords that we saw when we discuss that table of basic data types. Now as you previously mentioned you want to avoid using built-in Python keywords as variable names and the way you could know if that’s happening or not is let’s say I wanted to start assigning I.A. equal to 4.

So notice what’s happening here. I have syntax highlighting on I.A., and I didn’t get that before with a. So that means that I and there is a special built-in keyword and you shouldn’t use it for something like this. So if you ever see that your variable name is having some special highlighting that a normal variable name it doesn’t have then you should avoid using this. So definitely don’t ever run that.

And if you run that as you were following along. Or maybe you made some other re-assignment mistake. You can always come here to the kernel and select restart the kernel and that will restart the kernel and it will kind of delete all the variables so all variables will be lost. If you ever have some weird kind of error happening because you reassign something like list or I.A.

you can hit restart here. It will restart the kernel, and then you’ll need to rerun the cells. If you’ve wanted to find anything because if we say here, they’ll say hey I do not define Sin-Eater rerun the cells, and then you have five again. OK, so the last thing I want to know is a simple example use variable names. So I will say my income is equal to 100 and then in the cell, I will say my tax rate is let’s say I have a 10 per cent tax rate to 0.1 and I want to figure out what my total taxes paid are.

I will say my taxes is equal to my income. Times my tax rate so I have that, and Elish checks what my taxes are. How much do I owe? I’ll check my taxes. And there we have 10.0. So now I can perform logic with variable names and this is a lot more readable than just using integers or floating point numbers.

Because now I have this nice almost English sentence that says my taxes equal to my income times my tax rate. OK, so we’ve learned some basic numbers in Python. We’ve learned how to deal with the tick, and we’ve wrapped it up by learning how to do variable assignment in Python. Up next we’re going to learn about strings.

PART – 2

Now there are situations when you become a more advanced programmer. Where you are going to want to have a kind of global variable names in all caps that are used to write your code. But right now, in general, want to keep our names lowercase,

and we also want to avoid words that have a special meaning in Python, and these are built in keywords like a list or as for string. You may be wondering well how the heck is I supposed to know what are the appropriate built-in keywords.

Luckily any development environment that’s designed to work with Python will have syntax highlighting that will alert you that using a built-in keyword by highlighting a different colour and we’ll see an example of that in just a little bit. Before we jump to the Jupiter book though I want to mention that Python uses dynamics exciting and this means you can reassign variables to different data types and this makes Python very flexible in assigning data types. And that’s different than many other programming languages that are statically typed.

So let me show you an example of what I mean by this in python something like this is OK. Here I’ve assigned my dogs variable name equal to two and then later on in my code I went ahead and reassigned the same variable name my dogs to a completely different data type list. Sammy and Frankie. Now that’s OK in Python but in other languages that would produce an error.

And that’s because these other languages are statically typed meaning in the other language such as C plus you’d have to say I.A. for integer and then say my dog is equal to whatever integer value want such as 1 and then later on your code. You would not be able to assign a different data type. You would not be able to say My dog is equal to Samie because it’s no longer an integer. Now to result in an error

So there are some pros and cons to dynamic typing in Python. The prose is not having to write out the actual data type. for tool which use in python you can visit Techstack which provide free tool for students .Saves you a lot of time and makes it easy to produce Python code quickly and it also makes your code very readable because you’re just reading that variable name. Now, this kind of a double-edged sword here because the cons are that this may result in bugs for unexpected data type because you’re not having these restrictions of data types especially when you’re dealing with user input.

You may have unexpected data type show up, and that can cause problems later on in your operations. So you should be aware of the data types as you’re coding and you can use a special type function that’s built into Python to check the type of any variable quickly and will show you how to use that. And just a little bit. All right let’s explore all these concepts by jumping to a different book. OK now that we’ve seen how to use numbers in

Python as a calculator let’s see how we can assign names and create variables. We’re first going to create a very simple variable called A and set

it equal to 5. And now that I’ve run that anywhere in my code when I call a. It now assigns the variable 5, and I can reassign it simply by saying A is then equal to something else like 10. And now if I check it has ten there, and

I can also add now objects together, I could say a plus A., And that’s going to result in 20 because ten plus 10 is equal to 20. And Python also allows you to do reassignments concerning the same object. Let me show you what I mean by that. I could say A which is still equal to 10 I could reassign it to be say something like A is equal to a plus A. So what that is saying is take the current value of A which is 10 and reassign it to a plus A., So that’s ten plus 10.

So after I run this is now going to be equal to 20. And keep in mind if I were to run this cell a second time. So I noticed the in operator here it’s going to go from 40 to 42. If I rerun it’s 40 now, and you can keep doing this again and again, and you’ll keep seeing it essentially double each time. So keep that in mind. This is a little different than in a scripting environment if

you’re running a great script you don’t see that effect because you’ll have that line once in a cell environment. You’d have to run that cell over and over again. OK. So let’s imagine that we don’t know what type is a where

PART -3

you can do is use the built-in type function, so that’s type have open in close parentheses, and we’ll learn how to create our functions later on. But passing the variable there do shift enter, and you’ll get back Python’s built-in keyword for what the type is. And in this case, it’s I.A. because it’s an integer.

Let’s reassign it to be a floating point number. So we’ll say thirty points one. Let’s check the type of that type of a, and it returns that float. So these are the same keywords that we saw when we discuss that table of basic data types.

Now as you previously mentioned you want to avoid using built-in Python keywords as variable names and the way you could know if that’s happening or not is let’s say I wanted to start assigning I.A. equal to 4. So notice what’s happening here. I have syntax highlighting on I.A., and I didn’t get that

So that means that I and there is a special built-in keyword and you shouldn’t use it for something like this. So if you ever see that your variable name is having some special highlighting that a normal variable name it doesn’t have then you should avoid using this.and you can visit python course in delhi ncr So definitely don’t ever run that. And if you run that as you were following along. Or maybe you made some other re-assignment mistake. You can always come here to the kernel and select restart the kernel and that will restart the kernel and it will kind of delete all the variables so all variables will be lost. If you ever have some weird kind of error happening because you reassign something like list or I.A.

you can hit restart here. It will restart the kernel, and then you’ll need to rerun the cells. If you’ve wanted to find anything because if we say here, they’ll say hey I do not define Sin-Eater rerun the cells, and then you have five again. OK, so the last thing I want to know is a simple example use variable names.

So I will say my income is equal to 100 and then in the cell, I will say my tax rate is let’s say I have a 10 per cent tax rate to 0.1 and I want to figure out what my total taxes paid are. I will say my taxes is equal to my income. Times my tax rate so I have that, and Elish checks what my taxes are. How much do I owe? I’ll check my taxes. And there we have 10.0. So now I can perform logic with variable names and this is a lot more readable than just using integers or floating point numbers.

Because now I have this nice almost English sentence that says my taxes equal to my income times my tax rate. OK, so we’ve learned some basic numbers in Python. We’ve learned how to deal with the tick, and we’ve wrapped it up by learning how to do variable assignment in Python. Up next we’re going to learn about strings. I’ll see you there.

Leave a reply:

Your email address will not be published.