© Distribution of this video is restricted by its owner
Transcript ×
Auto highlight
Font-size
00:08 Welcome to the video component of the two. For this course here we're

00:17 cover the very very basics of python switch to some slides. So the

00:30 today is python basics. As we at the beginning of this course,

00:40 is the language we are selected to instructions to a computer. So let's

00:47 started with that. So today we'll what is what is the python program

00:54 values and variables that are the real . The next thing is assignments and

01:02 , and then we'll talk a little about input and output in python.

01:07 very basics, so for writing a in python, we need to know

01:18 few things that really don't have anything do with the language. But just

01:24 reminder of some of the things we have already discussed. A python program

01:30 plain text. Um it's not binary any such thing. Most real world

01:38 programming is done in something called an development environment. So the idea is

01:45 instead of writing a program in a file or something and trying to run

01:51 , you get some assistance in developing program running it. And these things

01:57 I. D. S. These slides and some examples of those for

02:03 con include peachum and spider and several , but also the the book that

02:11 using that has a Bill written python environment. So you can actually write

02:19 run python programs in the book itself that is what we will primarily be

02:25 in this course, at least for for the first couple of weeks.

02:29 you don't have to go outside to a python environment, everything is there

02:35 the room stone text that you So the um uh it allows,

02:47 not a sophisticated so called I D but it does allow simple things like

02:51 by step execution, which is helpful um um program in in understanding how

02:59 program is running. So moving on few more basics just so that you

03:09 , a python program will have the containing a python program will have these

03:17 py in the end. So so know which one is a python program

03:22 with something else? Um computer programs case sensitive. Just remember that uppercase

03:29 is not the same as lowercase They mean could mean different things.

03:33 finally outside of the main program you have comments that look like this.

03:39 just for your visual clarity. Those comments, those are not part of

03:45 core program. They're meant for human and another little detail you'll see

03:54 we haven't told you where you need but you need courts. And in

03:58 interestingly double court and single court mean same thing except that a single court

04:06 to be matched with a single court a double court has to be matched

04:09 a those are some little background I wanted to get out of the

04:14 before we start talking about real So getting to the real stuff of

04:23 , the first thing is very Something I call a value, values

04:30 things like the number 14, the 3.9 the number of the string,

04:39 , basically any quantity, an element python manipulates computes with creates and so

04:49 is usually something that we say is value and you hear the term object

04:56 value for now in python at least now just think of it at the

05:02 whether somebody says an object or value matter. We are referring to something

05:08 this for the number or string or like that. Every value has a

05:16 , a data type. So this also a very accept but many words

05:24 used to explain the same thing For . At least, data type is

05:30 as type is same as what's called class. So they're all the same

05:36 least for now, at least in score. So we don't want to

05:41 into details that are irrelevant right Now, what are these types?

05:47 there say the number 14 is an makes sense. 3.9 is a decimal

05:56 and a decimal number is referred to a float and alpha is a string

06:03 we're like technically it'll be str so n t f l O a t

06:07 s d are the official terms we to um uh for for these um

06:16 types. Okay, just gonna switch here. So once again this is

06:26 a summary of what we have covered integer. What we think of as

06:31 is python, type I. T. And examples of 1 23

06:35 1 23 0. A real value five. And some examples of two

06:43 something something. And you can also things like E to the power something

06:49 such and strings are any text. enclosed between court marks. So here

06:56 some examples of those. Let's move . Um python allows us to find

07:09 type something else is and it might right now but it's still useful and

07:16 will make more sense. So you know what the type of something

07:20 Use this function called type. And with that function said tell me what

07:27 something is. So here let's think this one at a time. When

07:33 say type, What type is Clearly this is a string. So

07:39 should return str look at 43. is we know it's an integer.

07:47 It should return I. N. . Um 4.3 seems like a float

07:54 uh real number which python calls it float. So that should be

08:01 And uh the Let's look at It's 43 but it's enclosed in

08:11 Anything enclosed in quotes is a So this should be string. It

08:17 matter that the actual content of string like a number. So but don't

08:26 my word for it. Go to python and profess in the book,

08:31 these statements on and see what you get. And let me know if

08:35 get something other than what's here. we'll talk a little bit about type

08:46 . Sometimes it makes sense to switch one type to another. Okay,

08:53 um now look at this statement So float is one of the you

09:04 use, it's the same term that use for the type. You can

09:07 use it as a function to convert to float something uh to convert to

09:14 . So the number two, you to convert it to float two looks

09:20 an integer. But of course you be a real value to. So

09:24 you do this, you should get like 2.2 point zero. Is the

09:31 or python's way of saying that I to but I'm thinking of it as

09:36 real number or um not not as integer. Okay, Let's see.

09:45 . Now it says in 2.9, is another of those that is a

09:52 but it can be used as a to convert something to an integer.

09:58 it's actually unclear what what does it to uh convert 2.92? An

10:08 You could say that maybe you should at the closest integer to the

10:14 That would be three. And but is an alternate where where you forget

10:20 the fractional part and that's what python . So if you do this it

10:26 it's when you can what it uh to an integer in python it forgets

10:32 the part of the decimal so you get to Now here moving on,

10:41 a a string and the content of string is 48, Converting that to

10:50 integer intuitively makes sense because after all just a number, it just happens

10:55 be in the strength form and that's Python does. The answer you'll get

11:00 would be 48. No If we 4.8 into a string that's okay to

11:12 string can be anything. You have float 4.8 and that can be converted

11:17 a string. So intuitively thinking of as a string, that would be

11:24 this. But actually when you print string, although when you enter a

11:30 you have to put coats. When print a string um there are no

11:35 . So actually when you print it get like this but just four

11:39 But it's just printing it as a . Don't worry if that confuses you

11:43 now but the basic idea is that float has been converted to a string

11:49 then it has been printed. Now at this for a minute, there

11:55 a string called number N u m R and you want to convert it

12:02 an integer. This makes no You can't have a little string even

12:11 it looks like you know something pneumatic its meaning to be converted to a

12:17 . So this is meaningless, you get an error. So the story

12:24 that these conversions only work when they sense. So that's the best way

12:30 think of it right now. Instead you know detailed rules for everything,

12:34 is where this statement comes from. now we'll just use common sense in

12:41 out what can or cannot be converted within that common sense condition you can

12:50 conversions between these types. Alright, so far we've looked at values,

13:05 going to introduce Two of the most concepts in any language which is variables

13:15 assignments um in the next couple of . So variable is a name that

13:23 to a value. So we had 4.345 whatever instead of the actual

13:30 we can give it a name and uh you can imagine that makes life

13:36 in many ways. And the way variable is created is with an assignment

13:42 . So a statement like this month it takes this value eight and assigns

13:50 to variable month. Okay, It a value. Month is a

13:55 Now, month is attached to it radius equals 5.0 the only difference is

14:01 now the value is a flawed and says title equals introduced Python. Now

14:06 value is strength, but essentially all these are variables and we can assign

14:15 to them and by what's called an statement. Okay. Should be pretty

14:24 . The way to think of Is that a variable is a

14:35 So month radius title, they're all , anything can go inside them.

14:43 ? And when we executed those assignment , this is what happened That those

14:52 those empty box called month. It's filled with it. The box radius

14:58 the value of 5.0 and the box has introduced Python. So it's it's

15:05 to start thinking variables right way. and uh the the way to think

15:12 be slightly different in different computer but this is the best way to

15:17 in python. So, um remember you may have uh used or you

15:26 have used variables. In algebra. always this X in algebra that you're

15:31 to find the value of. They related but not the same concept.

15:37 algebra variable is something that you don't the value of your trying to find

15:42 value in python. It's a name you give to a value.

15:48 so a valuable has a name. , just this is some basically housekeeping

15:58 . What can be a variable, write the program and you name the

16:03 in your program. Can any, know, any word be available more

16:10 less. Yes. So variables are composed of uh letters. They can

16:16 uppercase or lowercase. It'll be Something with an uppercase, something with

16:20 lower case. They can have digits they can have underscore. So that

16:26 that's what you have to use a of those two. Put together the

16:31 of a variable with the condition that first character must be a letter.

16:39 the first character cannot be a And technically the first character can be

16:48 , but for this course you're strongly to not use to start all your

16:55 with the latter not underscore because sometimes underscores has special meaning and it's not

17:02 it to take that risk. So don't use them and you won't accidentally

17:06 into trouble. So for for now character has to be a letter.

17:14 only other condition is that there are words that have a special meaning in

17:21 . You can imagine if else if and we don't know the meanings

17:26 But you can imagine they probably have specific meanings and it would be very

17:32 if you said if equals two. you're not you're not allowed to do

17:38 . Um You can look in the the book, the list of these

17:44 words and generally just get a rough of what it is. So that

17:49 don't accidentally use them that's about So those are the rules of what

17:57 allows you to do? Which is wide and but then there are the

18:02 of what is a good idea. basically you want variable names to be

18:08 and intuitive right? Because you want be to remember and be able to

18:15 what is the significance what a valuable for. So if if you use

18:22 tax, total cost then it's intuitively what it stands for. There was

18:27 reason to call them these. As as python is concerned, you could

18:32 said, you know the first one X. One. Second one is

18:35 . To the third one is Three and python is happy but you

18:40 be happy. And if you are with another person on your program they

18:46 be happy because you know there's there's no useful information anymore in the

18:52 of the variable settle. No. already talked about the assignment statement and

19:04 said that a variable is created with assignment statement when we said something like

19:11 equals for prices. The variable name the price equals for is the assignment

19:18 . So those who are actually special of an assignment statement in general.

19:25 assignment statement has a variable name on left hand side and an expression on

19:32 right hand side. These are examples uh general assignment statements um There's a

19:41 on one side and a bunch of that are being uh kind of processed

19:50 the other side. Okay. Um what does what does python do when

19:58 sees a statement like that? First it does is evaluate the expression on

20:04 right. Forget about what's on the in this area, you evaluate

20:10 so you multiply by 3.14 by five five and see whatever value you come

20:16 with and then once you are you store the resulting value in the

20:23 name on the left. So now area gets this value whatever. 55

20:28 3 75 something that gets that So it's a two step process compute

20:34 expression on the right. What our . It computes to store the results

20:39 the on the in the variable on left. Okay. Um yeah.

20:49 if you already noticed that the expression the right can have values, it

20:55 also have other variables. And if the case, whatever is the value

21:00 that variable at that point would be for computing the expression. So a

21:09 bit more on Expression for the next minutes, expressions consists of two

21:18 operators and operations. So operators are that uh manipulations or how what what

21:32 used to make computations. Things like , subtraction, multiplication, division.

21:38 all operators and operations is what you those to. These are numbers

21:46 other valuables and and stuff. so let's look at an example it

21:52 45 plus nine. Now plus this the operator, some is the operator

22:01 nine and 45. Our operations here have the expression two multiplied by

22:09 multiplied by r. The multiplication are operators And two and Pi and R

22:21 the operations. Okay. So same you have area slash 2.0 areas and

22:28 . And to point out as an and slash or division is the

22:32 And here you've got this operator which multiplication, that acts on two sub

22:42 which themselves have operator minus here and here and the operations are A and

22:49 and a and four here. So is this is nothing just we want

22:56 make sure we have a vocabulary You've seen enough of that in your

23:01 courses. Um Most of the operators python of intuitive multiplication is multiplication,

23:12 , is addition, subtraction, Uh There is some issue with division

23:19 the division takes multiple forms. The slash is decimal division. So if

23:26 decide 30 by eight you get 3.75 you calculate hopefully that is correct.

23:34 , um there is also the concept integer division, integral division means you

23:41 30 by eight and you only the is the caution without the decimal

23:50 So that one is uh the the for that is double slash. So

23:56 how you distinguish real multiplication and integer . And finally if you may also

24:08 you have the quotient and the remainder um integer division. If you want

24:14 remainder, you use this percentage sign your operator. So you You're looking

24:21 remember when you divide 11 x three you calculate remember should be too and

24:27 what it returns. So this kind completes the list of basic operation.

24:32 are into their plus minus multiplication and on. Okay. Uh so um

24:47 , once you have a bunch of and operators, what is the order

24:54 which they should be calculated? For you have a plus B multiplied by

25:01 . Do you do the multiplication first added to the multiply B plus

25:09 b n C and added to Or you add a plus B and

25:13 multiply to see, you can see these will return very different answers.

25:22 python follows what is well known mathematical , you should already be aware of

25:29 and you can you know, check anywhere which just means cost, you

25:34 exponential pension and negation. Next is and division next to the addition and

25:41 . And when everything is the you have a bunch of addition and

25:46 . You don't know because they all the same priority. You go left

25:50 right except exponentially ation. You go other way. These are the rules

25:56 you you're probably familiar with them So now based on these rules,

26:03 plus B multiplied by C. Is this with parenthesis. With be multiplication

26:11 see happening first and that is represented the parenthesis. And here you have

26:19 exponentially ation. So the exponentially ation first and then the multiplication and then

26:26 . Now note that both these statements the left and right are legal python

26:34 . So one is uh they're equally as far as python is concerned but

26:44 always a good idea to write python like this so you don't have to

26:50 and remember all the President's rules and if the even if your intended to

26:57 the president's you're still free to put corinth Asus and then it's clear what

27:03 of operations you intend in your So that's um that's about it for

27:15 presidents and in general expressions I wanted put a few more examples of string

27:24 type because that's something which we're not to as much in mathematics. So

27:30 python strings are any characters between between quotation March, Right. So my

27:40 is a string. Another string example another string. The and there are

27:45 blanks count. They're part of the to just to get more familiar.

27:52 can have strength. That is A C lower case abc uppercase and blank

27:59 blank C. But they are all different strengths. Small, lower case

28:05 . And upper case. Er just characters that have nothing to do with

28:09 other And space is also a These two are different because upper and

28:16 is different. These two are different there are spaces and uh you can

28:22 letters, digits, special characters. everything within a string and it doesn't

28:29 . It's just uh carried whatever way looks, they don't have it within

28:35 string concept. They don't have any meaning. It's just everything is a

28:39 whether it's a letter or a digit and some special characters. So wrapping

28:51 with pipes, if you can think her type as a set of values

29:00 a set of operations that you can on them. So we've talked about

29:06 float and string as the type so . So here are some examples of

29:11 there's an infinite list of integers and of these but these are the set

29:17 values that can be integer plus minus uh anything between you know plus and

29:24 infinity. Uh These are examples of these are examples of strength. So

29:31 aspect of a type is what values be that type and I think looking

29:36 this, it should be intuitively clear the second component is of a type

29:42 what operations you can perform on Okay, so we've seen most of

29:49 , we know most of these intuitively integers. The operations we can perform

29:55 addition, subtraction, multiplication, Uh that we can do real type

30:03 , integer type divisions. You can um you know very minor. That

30:07 just means you can say minus four percent we saw is a remainder and

30:13 is exponential station. This is intuitive floor is the same except for things

30:20 don't make any sense which would be doesn't make any sense. Integer division

30:26 make any sense in a float. those are not allowed. And finally

30:32 also have some operations on them that represented by plus sign and start the

30:39 sign. It is not obvious what what they should mean. So python

30:47 made an adult decision that a plus strength and concatenation. So A B

30:54 plus C. D. That those are strength is just A B c

30:59 D E. And multiplication is duplicating strength. So if you say two

31:07 by string A B, you'll get B A B. And that's about

31:15 the operations you can do on these Again, types means a set of

31:22 and what operations you can do on . And this table pretty much covers

31:28 uh you know, that define completes definition of a type or the types

31:34 we will be interested in. So some situation python will has to decide

31:52 type of a result. For Here you're dividing one by 21 is

32:01 integer. Two is an integer now the symbol you're using is real

32:11 So python here does what Common sense it returns the value of .5 which

32:21 course is a float. So you get you do it this kind of

32:26 conversion or you see this kind of conversion when you're calculating an expression.

32:33 result is a flirt even though the operations oriented now look at this one

32:43 a second. So the X value the same as 10.5 point five is

32:50 by two. So the value will one. But the question is is

32:56 now afloat or an integer python says a float. So in general when

33:05 have an operation between an integer and floor. The result is a

33:09 So yeah this is just just something know and most of the time this

33:17 intuitively what you would expect. So to variables assignments and that we already

33:29 and a variable can be assigned different at different points in a program.

33:37 in the discussion so far we gave variable one value and didn't didn't worry

33:42 it after here this is a perfectly uh cord the you say I.

33:51 . Equals court. Do not know . Just ignores like difference between the

33:57 and end court and then you say . D equal Susan then you say

34:01 . D. Equals 47 nor that is an interesting. Okay so this

34:06 perfectly legal. Remember a variable is container, you can put one thing

34:12 then you can throw that out and can put different thing and then you

34:15 throw it out and put a different . So the way to think of

34:20 happens here is that after the first you have um this situation here you

34:30 the variable I. D. Remember . D. Is a container.

34:33 the container has the strength. Do know yet. Right? So and

34:39 you go and execute the statement saying . D. Equal Susan at that

34:45 . This connection is broken I. . Does not stand for, do

34:50 know yet anymore. Now it is to this value Susan. So

34:56 D. Is now Susan and when say I. D. Equals

35:01 Once again this connection is broken and this point the I. D.

35:07 variable I. D. Has a content in it. 47.

35:13 So uh basically a variable is a . You can take one thing out

35:18 put a new thing in and the you do it is with repeated assignment

35:24 with the same variable on the left side. Hopefully this is clear.

35:31 also I think this is really intuitive of course multiple variables can have the

35:39 value. So you can have item equals 4.5. Total price equals 4.5

35:45 something else. And uh one way think of it is when you said

35:52 price you got the value 4.5. uh the total price can also be

36:02 the same and you can think of um as another variable but that has

36:10 same content in its uh it's containing same value of pointing to the same

36:29 . Okay um A minute on a very common pattern of reassigning a variable

36:40 python in any other language. Is one? Count equals count plus

36:46 Okay now if this was algebra this not make any sense. Right count

36:51 available. Is that it cannot be count and countless one cannot be equal

36:59 this equal is not really equal in ? It's uh it's an assignment statement

37:06 ? So now what happens here is what we expect if we follow the

37:14 of executing an assignment statement? So we said the rules of the

37:20 just say account was 10 before the was executed. Said there was count

37:27 stand before it. The rules for or evaluating an assignment, Our first

37:36 the right hand side. So when evaluate the right hand side is count

37:40 one. So this is gonna be plus one equals 11. After

37:45 once this is complete, you say the value to the variable name on

37:50 left. Now the valuable count becomes . Yeah so the end result of

38:00 county calls count plus one. Is the value of count would be one

38:05 than before. Um So if it 10, it will be 11,

38:09 was 100 will be one on one so on. So it's perfectly

38:13 perfectly legitimate python statement which is extremely . And as we said, this

38:20 another way to think about how variables algebra and programming languages are different.

38:29 makes no sense in algebra, but a good common way of using variables

38:36 assignments in python. So just a more minutes using the not using,

38:49 about input and output. How do get data in into a python program

38:54 computation? How do you throw it ? Um And we're not gonna cover

39:04 possible way that python can get input create output but just enough that uh

39:12 can get started. So python has built in function called input. So

39:19 happens when you say input and python running a program? Uh This is

39:27 input from the keyboard. Everything stops you can't move forward until we've got

39:36 import from the user. So the is given a prompt saying that you

39:44 to enter some input here and when user presses returns or enters the import

39:49 says okay I have my input Here's the input. Let's start

39:53 The program resumes and keeps going and the user enters is considered a

40:01 So I think this will become more in the next minute or so.

40:07 if you had um this statement um , what is your name? It

40:16 two components. One is that this you have in this string is uh

40:25 uh part of the prompt. So got printed on the on the user

40:30 so they know that you're looking for name. So if you are a

40:35 you will see this, what's your ? So let's say the user entered

40:39 genius here and let's so then at point the name gets the value the

40:49 , clever genius. So if you print name after that clever genius will

40:53 printed. Okay, so it's very . You say import. When you

40:59 import you can put any string in and that will get presented to the

41:04 who's giving the import and whatever input user gives. That will be the

41:12 of the variable here as a So now the that begs the question

41:23 if you actually want an integer or if you actually want somebody to enter

41:28 float? Because your program you is looking for say the you know

41:34 length of a table and that is float in python um types um you

41:45 enter the import as a strength. you will have this statement, you

41:52 G P A equals flowed input. is so the import statement is still

41:59 similar. Okay. And what comes is a strength. So when somebody

42:06 3.9 then that is comes in as string. But this function float,

42:13 it to a float. And then you type after that the type is

42:18 . This is Python's way of saying . It is, it is

42:22 Okay, so basically all input has be entered as a string but that's

42:29 a serious limitation. You can enter integer, read it as a string

42:34 convert it to an integer. Similar float. You have inter type conversion

42:39 you get an integer or you take as a string and convert it to

42:44 float. If you don't do it will stay as a strength.

42:47 is important to remember, especially for programmers, sometimes you forget you think

42:53 I'm going to enter too and it be a number but if you don't

42:58 it to an integer, you try say divide by two and you'll get

43:03 error because you cannot divide by uh your strength. So this is a

43:10 detail. That's good to remember. for output we are only going to

43:17 the print function which we've already used times just in this lecture is just

43:24 , whatever. The only thing we're to add here is so far everything

43:30 like print and then there was value variable. In reality you can have

43:37 values. For example, this is legitimate print statement. It has first

43:45 is a string. There are then . There's a comma. Then there's

43:50 second value. 34 multiplied by Then there's a comma and then there's

43:55 third value which is another string which um seconds and 34 minutes. And

44:04 the the final uh chancellor is that's , is there 2040 seconds and 34

44:15 . So that makes it easier to out when you're when you're trying to

44:20 out something meaningful that includes multiple You can print in one shot versus

44:26 multiple prints treatments. So this is Oh yeah, so another thing is

44:39 variables and expressions which is implicit when say 34 multiplied by 60 as you've

44:46 here. It is calculated before being . So you don't print 34 and

44:54 multiplication than than 60. It's an . So the whole expression is evaluated

45:00 you saw and it's 2040. However in In this case seconds in 34

45:08 everything was a string. There was computation. So it got printed as

45:16 so this is the end of the summary is once again, we are

45:24 into computer programs, a computer program an implementation of an algorithm and um

45:34 talked about variables, variables, world that can be accessed by our

45:39 we have assignment statements that assign values variable and we have numerical data that

45:46 be represented in python using in and data types and uh our text is

45:54 , string data type and there are basic um input and output functions.

46:02 , that is the end of this lecture. Um and uh we will

46:11 you in class soon.

-
+