Python Variables and Data Types

Python Simplified
13 Apr 202014:12

Summary

TLDRThis Python tutorial covers the basics of variables, data types, and their applications. It introduces variables as essential building blocks, explaining their components: symbolic names, assignment operators, and values. The lesson delves into numeric data types, including integers and floats, with practical examples like calculating the probability of COVID-19 infection in British Columbia. It then explores strings, demonstrating how to manipulate and concatenate them using various methods. The tutorial also touches on boolean values and their role in conditional statements, setting the stage for future discussions on if statements and loops. The video promises more on advanced data types and variable naming conventions in the next installment.

Takeaways

  • ๐Ÿ Variables in Python are fundamental building blocks used to store data for easy access in code.
  • ๐Ÿ”ข Variables can hold different data types, including integers, floats, strings, and booleans.
  • ๐Ÿ“ˆ Python automatically converts integers to floats when performing division, resulting in decimal values.
  • ๐Ÿ“ Strings are sequences of characters enclosed in quotation marks and can include letters, numbers, and symbols.
  • ๐Ÿ” The `type()` function can be used to check the data type of a variable in Python.
  • ๐Ÿงฎ Basic mathematical operations like addition, subtraction, multiplication, division, and exponentiation can be performed on numeric data types.
  • ๐Ÿ”— String concatenation can be done using the `+` operator or within print statements using commas.
  • ๐Ÿ“‘ The `format` method offers a more organized way to create formatted strings with variables.
  • ๐Ÿ” Strings can be repeated using the multiplication operator (`*`) to repeat a string a specified number of times.
  • ๐Ÿ“Š Boolean values (`True` or `False`) are used in conditional statements and are integral to control flow in Python.
  • ๐Ÿ“˜ Advanced data types, which can contain more than one value, will be covered in later lessons.

Q & A

  • What are the three elements that make up a variable in Python?

    -A variable in Python consists of a symbolic name, an assignment operator (usually the equality sign), and the value that is being stored.

  • How many basic data types are there in Python?

    -There are eight basic data types in Python.

  • What is an integer in Python and what does it represent?

    -An integer in Python represents whole numbers, which can be positive or negative.

  • How do you represent floating-point numbers in Python?

    -Floating-point numbers, or floats, in Python represent decimals and can also be positive or negative.

  • What is the purpose of the 'type' function in Python?

    -The 'type' function in Python is used to check the data type of a variable when you are unsure of it.

  • What happens to the data type when you divide two integers in Python?

    -When you divide two integers in Python, the result is a float, even if the division results in a whole number.

  • What is a string in Python?

    -A string in Python is a set of characters enclosed in quotation marks, which can include letters, numbers, punctuation, and spaces.

  • How can you concatenate strings in Python?

    -You can concatenate strings in Python using the plus operator, the comma operator within print statements, or the format method.

  • What are the two arithmetic operations that can be performed on strings?

    -The two arithmetic operations that can be performed on strings are concatenation using the plus operator and multiplication using the asterisk operator.

  • What is a boolean data type in Python and what values can it take?

    -A boolean data type in Python can only take two values: 'True' or 'False', represented with a capital 'T' and 'F' respectively.

  • How do you check for equality in Python?

    -To check for equality in Python, you use the double equality sign (==) instead of the single equality sign, which is used for assignment.

Outlines

00:00

๐Ÿ Introduction to Variables and Data Types

This paragraph introduces the concept of variables in Python, explaining them as fundamental building blocks used to store data for easy access in code. It covers the three elements of variables: a symbolic name, an assignment operator (usually the equality sign), and the value to be stored. The paragraph also categorizes variables into different data types, such as numbers, words, and lists, and mentions that there are eight basic data types in Python. It starts with numeric data types, specifically integers (whole numbers) and floating-point numbers (decimals), and provides examples of how to declare and manipulate these types. The speaker also demonstrates how to perform basic arithmetic operations with these numeric types.

05:03

๐Ÿ“ Exploring Strings and Concatenation

The second paragraph delves into strings, which are sequences of characters enclosed in quotation marks. It explains that strings can include letters, numbers, punctuation, and spaces. The paragraph provides examples of declaring string variables and demonstrates how to print multiple variables in a single statement. It introduces string concatenation, showing how to connect strings using the plus operator and the comma operator within print statements. The speaker also explains how to use the format method for more organized string concatenation, filling in placeholders with variable values. Additionally, the paragraph touches on the limitations of mathematical operations with strings compared to integers and floats.

10:04

๐Ÿ”ข Arithmetic with Strings and Boolean Basics

This paragraph discusses the arithmetic operations that can be performed on strings, such as concatenation using the plus operator and multiplication with an integer or float using the asterisk operator. It then provides a real-life example of how strings can be used to personalize messages, similar to email newsletters. The paragraph concludes with an introduction to boolean data types, which can only be true or false. It explains the use of booleans in conditional statements and comparison operators, emphasizing their importance in upcoming lessons on control structures like if statements and while loops. The speaker also hints at the existence of more complex data types that will be covered later in the course.

Mindmap

Keywords

๐Ÿ’กVariables

Variables in the context of the video are the fundamental building blocks in Python programming used to store data. They consist of a symbolic name, an assignment operator (often the equal sign), and a value. Variables allow programmers to store and manipulate data easily throughout their code. For example, the video uses 'dragons' as a variable to represent the number of dragons the presenter has.

๐Ÿ’กData Types

Data types refer to the various kinds of values that can be stored in variables. The video introduces eight basic data types in Python, including integers, floating-point numbers, strings, and booleans. Understanding data types is crucial for knowing how to manipulate and use data within a program.

๐Ÿ’กIntegers

Integers are a data type representing whole numbers, both positive and negative. In the video, integers are used to demonstrate basic arithmetic operations and to track the number of dragons. For instance, 'dragons' is an integer variable set to three, and 'dead_dragons' is another integer set to two.

๐Ÿ’กFloating-Point Numbers

Floating-point numbers, or floats, represent real numbers and can include decimals. They are used when precision to a specific number of decimal places is necessary. In the video, a floating-point number is used to calculate the probability of getting sick with COVID-19 in British Columbia.

๐Ÿ’กType Function

The 'type' function in Python is used to determine the data type of a variable. This is demonstrated in the video when the presenter checks if the variable 'prob' is a float, which it is because the result of the division operation resulted in a decimal value.

๐Ÿ’กStrings

Strings are sets of characters enclosed in quotation marks, which can include letters, numbers, and punctuation. They are used to represent text in Python. The video provides examples of string variables such as 'pet', 'pet_name', and 'pet_age', and shows how to concatenate them to form sentences.

๐Ÿ’กConcatenation

Concatenation is the process of joining two or more strings together. The video explains how to concatenate strings using the plus operator and the comma operator within print statements. It also introduces the 'format' method as a more organized way to concatenate strings with variables.

๐Ÿ’กBoolean

Booleans are a data type that can only be one of two values: True or False. They are used in conditional statements to evaluate whether a certain condition is met. The video uses a boolean variable 'my_boolean' to demonstrate a conditional statement where Python evaluates if 3 is less than 6.

๐Ÿ’กConditional Statements

Conditional statements are used to perform different actions based on whether a condition is true or false. The video briefly mentions that boolean values are integral to if statements and while loops, which will be covered in future lessons. An example given is checking if 3 is less than 6.

๐Ÿ’กArithmetic Operations

Arithmetic operations are mathematical calculations performed using numbers. The video demonstrates basic arithmetic operations such as addition, subtraction, multiplication, division, and exponentiation using integers and floating-point numbers.

๐Ÿ’กAssignment Operator

The assignment operator, often the equal sign (=), is used to assign values to variables. It is a fundamental concept in programming for storing data. The video explains that variables are created with a name, an assignment operator, and a value.

Highlights

Variables are basic building blocks in Python used to store data.

Variables consist of a symbolic name, an assignment operator, and a value.

Python has eight basic data types for different kinds of data storage.

Integers represent whole numbers, both positive and negative.

Floating-point numbers or floats represent decimals.

An example of using integers to calculate the number of dragons remaining.

A real-life scenario using integers and floats to track COVID-19 cases.

Variables can be checked for their data type using the 'type' function.

Strings are sets of characters inside quotation marks.

Concatenation of strings can be done using the plus operator.

The format method is a favorite way to concatenate strings for its organization.

Boolean data type can only be true or false.

Conditional statements are used to evaluate true or false values.

Comparison operators are used to compare values in Python.

Boolean values are integral to if statements and while loops.

Advanced data types can contain more than one value.

The video will be continued in the next part focusing on variable naming rules.

Transcripts

play00:00

[Music]

play00:04

hi people welcome to Python lesson

play00:07

number three where we'll talk about

play00:09

variables and all the different types of

play00:11

data we can store inside them variables

play00:14

are very basic building blocks in Python

play00:17

we use them to store pieces of data that

play00:20

we'd like to easily access throughout

play00:22

our code variables consists of three

play00:25

different elements a symbolic name an

play00:28

assignment operator which in most cases

play00:31

will be the Equality sign and lastly the

play00:34

value are looking to store and I've

play00:36

included some examples for you guys so

play00:38

check them out before you continue

play00:41

variables are also being categorized

play00:43

into different data types representing

play00:46

numbers words and even lists of items

play00:49

currently there are eight basic data

play00:52

types in Python and let's actually take

play00:55

a look at them so it can get more

play00:56

familiar we'll start from the numeric

play00:59

data types and our first one would be

play01:01

integers integers represent whole

play01:04

numbers positive or negative and you can

play01:06

see some examples while floating-point

play01:09

numbers or floats represent decimals

play01:11

positive or negative as well and it's

play01:15

the perfect time to start in your

play01:17

notebook and Jupiter or call out

play01:19

whichever you like and we'll take a look

play01:21

at some coding examples so let's say I

play01:23

have three dragons so we'll type dragons

play01:26

equals three and randomly with no

play01:30

particular reason let's say the two of

play01:32

my dragons died so we'll type dead

play01:35

underscore dragons equals two you know

play01:39

the only thing left to do is to type a

play01:42

simple formula and find out how many

play01:44

dragons I actually have we'll type total

play01:47

underscore dragons equals dragons - that

play01:53

dragons kind of makes sense and make

play01:56

sure you print the results so we'll do

play01:58

print total underscore dragons and we

play02:03

can run the cell with shift-enter

play02:04

and we got one perfect well okay that

play02:09

was way too easy and extremely unusual

play02:11

so

play02:12

see a real-life scenario where we can

play02:14

use integers or floats so I live in

play02:17

British Columbia Canada and lately I've

play02:20

been keeping track of all the covert 19

play02:22

cases in the province so I'm going to

play02:24

use the up to date numbers from the

play02:26

government website of the cases in BC

play02:29

and we'll compare it to how many people

play02:31

in total live here our first variable

play02:34

will be named Kovac underscore BC and

play02:37

it's equals to 1200 and 3 as we've seen

play02:41

in the previous page while population

play02:43

underscore BC is actually equals to 5

play02:46

million and 71 thousand people that's a

play02:50

lot and for now keep your variable names

play02:53

identical to mine and we'll talk about

play02:55

the naming rules later on it's time for

play02:58

the formula so my probability of getting

play03:01

sick we'll call it prob equals to Colvin

play03:05

underscore BC / or / population

play03:11

underscore BC and we'll print these and

play03:15

shift-enter to run the cell and as you

play03:20

see my chances of getting infected are

play03:22

pretty low at least from this

play03:24

perspective I actually want to point

play03:27

your attention and do something

play03:28

interesting that happened here we can

play03:30

see that even though the variable prop

play03:32

was created using two integers it ends

play03:35

up falling under the float category

play03:37

because we get a decimal there's

play03:39

actually a very simple way of checking

play03:41

the data type of a variable if you're

play03:43

unsure we'll simply use the type

play03:46

function so we do it by typing type and

play03:49

inside round brackets we'll put our

play03:52

variable prob and we'll wrap all of this

play03:56

in a print statement so we can see the

play03:58

results run the cell with shift-enter

play04:01

and we can see that python interprets

play04:06

the variable probe as a float let's

play04:10

actually check the type of population BC

play04:13

now we're expecting to see that it's an

play04:15

integer but you never know it's always

play04:17

good to check and we see that we got it

play04:20

right perfect

play04:21

in our last numeric example i'll show

play04:25

the mathematical operations you can do

play04:27

with basic Python and yes I mean the

play04:30

absolute most basic Python so let's say

play04:33

a equals 12 and B equals 3 we can find

play04:38

out their sum by using plus we can find

play04:42

out their difference by using minus we

play04:47

can multiply using the asterisk we can

play04:51

divide by using the slash and lastly we

play04:56

can even do a square operation with two

play04:58

asterisks the next datatype we'll talk

play05:02

about is strings a string is any set of

play05:06

characters inside quotation marks now

play05:08

these characters can include letters

play05:10

numbers or even punctuation symbols and

play05:13

spaces basically as long as something is

play05:17

wrapped inside quotation marks Python

play05:19

will interpret it as a string let's see

play05:22

some examples so let's get a few basic

play05:25

details about our pets I'll type pet

play05:28

equals and in my case that would be cat

play05:32

inside quotation marks now the quotation

play05:34

marks can be either single or double as

play05:37

long as it's a matching pair now we'll

play05:40

continue to cut underscore name which in

play05:44

my case equals to me oh yes you can tell

play05:47

I was very impressed with the matrix and

play05:49

lastly pet underscore age in my case

play05:53

equals two quotation marks for now here

play05:57

I want you guys to note that even though

play05:59

four is a whole number once we place it

play06:01

inside quotation marks it does not count

play06:04

as an integer anymore it counts as a

play06:06

string automatically an integer would

play06:09

have looked like this so if the value of

play06:11

your variable is green that means you

play06:14

got yourself a float or an integer and

play06:16

if the value of your variable is

play06:18

burgundy that means that it's a string

play06:21

well at least in Jupiter

play06:23

now let's print all three variables in

play06:26

the exact same statement will type pet

play06:29

coma pet name comma again and Pat age

play06:35

shift enter and Wow

play06:38

we desperately need a few connection

play06:41

words luckily you don't really need to

play06:43

create separate variables for every

play06:45

piece of string you want to include so

play06:48

let's start with some context inside a

play06:50

print statement will start with

play06:51

quotation marks and the words I have a

play06:55

then will include the variable Pat using

play06:59

a comma of course and then we'll write

play07:02

his name is inside of quotation marks

play07:05

then we'll include the Pat underscore

play07:08

name we'll include the words and he is

play07:17

following a comma and the variable Pet

play07:20

underscore age and lastly finally coma

play07:25

and the string years old now let's use

play07:29

shift enter to run the cell now what

play07:34

we've just done here is called string

play07:36

concatenation which is fancy words for

play07:38

connecting strings

play07:40

note that concatenated using the coma

play07:43

operator can only be done inside print

play07:45

statements using comma anywhere else

play07:48

will result in a completely different

play07:49

action so don't do it just yet we'll get

play07:52

to it when we'll talk about tuples

play07:54

another thing we can do inside our print

play07:56

statement is dividing it into lines

play07:59

we'll use the new line literal which is

play08:02

backslash N and while it looks a bit

play08:05

crazy at first our results will now be

play08:07

arranged a bit differently we now have

play08:10

three lines instead of one and let's

play08:12

actually delete our first print

play08:14

statement we don't need it anymore

play08:16

and we can also see that the new lines

play08:18

begin exactly where we placed our

play08:21

backslash and literal perfect in our

play08:25

second example we'll start our

play08:28

concatenated string inside a variable

play08:30

we'll name it my underscore pet and

play08:33

we'll use the plus operator to can

play08:35

cata needed to the variable Pat and so

play08:38

on and again using a coma-like in the

play08:41

print statement above will not work here

play08:44

coma as part of a variable value means a

play08:47

completely different thing and now I'm

play08:50

just adding a little bit of context

play08:52

mostly to demonstrate we can use

play08:54

multiple plus operators in the exact

play08:57

same concatenation

play08:58

so let's print our variable my

play09:00

underscore pet and wow it works great

play09:05

another cool way we can concatenate

play09:07

strings is by using the format method

play09:10

and this is probably my favorite way

play09:13

because it's so much more organized will

play09:15

type my underscore pet to equals I have

play09:23

a curly brackets his name is curly

play09:29

brackets and he is you guessed it right

play09:33

curly brackets years old and right after

play09:37

the closing quotation marks with no

play09:39

spaces in between we type dot format and

play09:44

inside the round brackets we're filling

play09:47

in the blanks sequentially so first

play09:49

we'll have pet then we'll have pet

play09:52

underscore name and lastly we'll have

play09:55

pet underscore age and we're separating

play09:59

all of these with commas so don't forget

play10:01

that now let's print the variable my

play10:03

underscore Pat - and yes it works once

play10:09

again so far we've talked about all

play10:12

sorts of ways of combining these strings

play10:15

but what else can we do with them

play10:17

can we use some mathematical operations

play10:19

like we did with integers and floats

play10:21

let's have a look there are actually two

play10:24

arithmetic operations we can use with

play10:26

strings the first we're already familiar

play10:29

with that would be the summation of two

play10:31

strings by using the plus operator again

play10:35

it's in the examples above and the

play10:37

second is the multiplication of a string

play10:40

by any integer or float so as you can

play10:45

see in the current examples the plus

play10:47

symbols

play10:48

asterisks have completely different

play10:50

meanings when handling strings but

play10:53

what's the point in all this where can

play10:55

we apply strings in real life you can

play10:58

see that once we change a value in one

play11:01

or two of our pet ID variables our print

play11:04

statements are being modified

play11:05

accordingly you can basically tailor

play11:08

specific words inside your print

play11:10

statement to make your message more

play11:12

personal similarly to how every email

play11:15

newsletter these days contains your name

play11:17

somewhere on it rather than just a

play11:19

general hello user greeting so there's

play11:22

your real life example the last data

play11:24

type we'll talk about today is boolean

play11:27

and it's the easiest of them all because

play11:29

it can only be one of two values it can

play11:32

either be true with a capital T or false

play11:35

with a capital F and let me explain with

play11:38

an example so our variable my underscore

play11:42

boolean is equal to and now it's time

play11:45

for the conditional statement which in

play11:48

our case will be 3 is less than 6

play11:51

now Python is going to evaluate this

play11:54

conditional statement and let you know

play11:56

whether if it's true or false actually

play12:00

we can wrap our condition inside round

play12:02

brackets so it looks a bit more

play12:04

organized we'll try the same but this

play12:06

time we'll flip the side of the operator

play12:08

so 3 is greater than 6 let's see what

play12:13

python has to say about that and as you

play12:16

can see 3 is indeed not greater than 6

play12:21

here's a tricky question what happens if

play12:24

we want to check for the Equality

play12:26

instead do we still use the equal

play12:28

operator and the answer is absolutely

play12:31

not the equality symbol is an assignment

play12:35

operator it's basically the glue that

play12:37

holds a variable name in its value and

play12:40

therefore we cannot use it to make

play12:43

comparisons instead we'll use the double

play12:46

equality sign which is the go-to symbol

play12:49

in our case moreover there are plenty of

play12:53

other comparison operators you can use

play12:56

please take a look at the following list

play12:58

and try to experiment with a few of

play13:00

these and

play13:01

as you experiment you may be wondering

play13:04

what exactly can we do with this true or

play13:06

false thing where does it come handy so

play13:09

in the near future we'll learn all about

play13:11

if statements and while loops where

play13:13

boolean z' are an integral part of the

play13:16

scheme so far we've only talked to about

play13:19

four simple datatypes what about the

play13:22

rest of them

play13:22

the advanced datatypes contain more than

play13:26

just one value as you can probably see

play13:28

in the examples we can recognize some

play13:30

strings and integers inside them and yet

play13:32

they fall under completely different

play13:34

categories so let's save these for a

play13:37

later point in our course and we'll talk

play13:39

about them after we master all the cool

play13:41

things we can do with integers strings

play13:43

floats and boolean and by things

play13:45

I mean methods so stay tuned in the next

play13:49

video we'll talk about the rules of

play13:51

nathan in your variables just as I

play13:53

promised earlier I just had to split

play13:55

this lesson into two parts to keep it

play13:57

slightly shorter and slightly more

play14:00

bearable if you guys found this video

play14:03

helpful please give it a thumbs up and I

play14:06

will see you in less than three point

play14:07

five very soon bye bye

Rate This
โ˜…
โ˜…
โ˜…
โ˜…
โ˜…

5.0 / 5 (0 votes)

Related Tags
Python BasicsData TypesVariablesIntegersFloatsStringsBooleansCoding TutorialPython 101Programming