Python For Kids (Part 27: Variables)

Python For Kids (Part 27: Variables)

For a complete table of contents of all the lessons please click below as it will give you a brief of each lesson in addition to the topics it will cover. https://github.com/mytechnotalent/Python-For-Kids

We have up to this point discussed a good deal of framework for CPython and now we can begin to discuss variables and how we can utilize them to achieve a great deal of functionality in our development.

Python is what we refer to as a dynamically-typed language. This means that you do not have to explicitly define what kind of variable you are creating like an int or str. You can literally create a variable as follows.

Let's open up our Mu IDE. Let's start with the following.

name = 'Kevin'


print(name)

Let's save and run.

We see the following in the REPL.

Kevin
>>>

We have very simply taken the string 'Kevin' and stored it into a variable called name.

Strings are what we call immutable as you are not able to change individual elements or characters of a specific string. For example if you try this.

name[0] = 'L'

As you see we get the following error:

Traceback (most recent call last):

File "c:\users\kevin\mu_code\main.py", line 5, in <module>

name[0] = 'L'

TypeError: 'str' object does not support item assignment

>>> 

We see that a string in this case is not able to change one of it's elements individually. We can however change the entire string by doing this.

name = 'Keith'


print(name)

Here we see 'Keith' has been reassigned successfully into the name variable.

We can also see that this variable is in fact a str.

print(type(name))

This will yield the following result.

<class 'str'>

>>> 

Finally we can also reassign the variable to any other data type as well. Let's try to make name an integer. This is not something we would do normally as the name would be confusing but for educational purposes I wanted to demonstrate for you so you can see the power and flexibility of variables in Python.

name = 42

print(name)
print(type(name))

We can see the following result.

42
<class 'int'>

As we can see we changed the name variable to an int and assigned it the number 42.

We can also create variables for any of our other primitive data types that we have discussed in the last several lectures.

HOMEWORK:

I would like to you try a few more examples and re-run them so you get a better idea of how this works.

I would like you go to back and create variables for each of the primitive datatypes and print them out. I would also like you to specifically try to change the individual elements of each to see if they are mutable (changeable) or immutable (unchangeable) to get a better idea of the underlying mechanics.

I would then like you to print out the type of each variable like we did above for the string so you can experiment with ways to figure out how to check what a particular variable really is under the hood.

This is the FUN part of learning when we apply our new knowledge and experiment on our own it REALLY helps us learn these concepts so please take your time and try a number of different things on your own.

Taking the time and making examples of all the datatypes will really help you to prepare for the more advanced topic so I really encourage you to try these out.

If you have any questions please as always leave them in the comments below as I would LOVE to help you with any issues.

The most important thing is you are learning and you are comfortable, confident and having FUN!

In the next lesson we discuss mathematical operations.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了