What is List :
In python, list is a collection of any data type which is mutable, i.e means we can change the value of list
list Creation
we can create list in two ways
a) Empty List : L=[]
b) With some values : L1=[1,2,3,4,5,6]
After creation of empty list we can add some number with the help of append and extend function.
for accessing list element we can use the index of the element. i.e if we want to access 3 from above mentioned list(L1), then we have to write, x= L1[2], where 2 is the index of 3.
if you want to access all the element, then you can use range function as follows:
for i in range (0,len(L1)):
print(L1[i])
Here, i represents the index of individual element.
Lists are pretty useful, but only if you can get at the things inside them. You can already go through the elements of a list in order, but what if you want, say, the fifth element? You need to know how to access the elements of a list. Here’s how you would access the first element of a list:
animals = ['bear', 'tiger', 'penguin', 'zebra']
bear = animals[0]
How do you make a two- dimensional (2D)
list?
That’s a list in a list like this:
[[1,2,3],[4,5,6]].
Aren’t lists and arrays the same thing?
It depends on the language and the implementation. In classic terms, lists are very different from arrays because of how they’re implemented. In Ruby, lists are referred to as arrays. In Python, they’re referred to as lists. Just call these lists for now, since that’s what Python calls them.
How come a for- loop can use variables that aren’t defined yet?
It defines that variable, initializing it to the current element of the loop iteration, each time through.
Why does for i in range(1, 3): only loop two times instead of three times?
The range() function only does numbers from the first to the last, not including the last. So itstops at two, not three, in the above. This turns out to be the most common way to do this kind of loop.
What does elements.append() do?
It simply appends to the end of the list. Open up the Python shell and try a few examples with a list you make. Any time you run into things like this, always try to play with them interactively in the Python shell.
What is While loop
A while- loop will keep executing the code block under it as long as a boolean expression is True. they do is simply do a test like an if- statement, but instead of running the code block once, they jump back to the “top” where the while is and repeat. It keeps doing this until the expression is False. Here’s the problem with while- loops: Sometimes they do not stop. This is great if your intention is to just keep looping until the end of the universe. Otherwise you almost always want your loops to end eventually. To avoid these problems, there’s some rules to follow:
1.
Make sure that you use while- loops sparingly. Usually a for-
loop is better.
2. Review your while statements and make sure that the thing you are testing will become False at some point.
3. When in doubt, print out your test variable at the top and bottom of the while- loop to see what it’s doing.
String Delimiters, Part I
A string in Python is a sequence of characters. For Python to
recognize a sequence of characters, like hello, as a
string, it must be enclosed in quotes to delimit the string.
For this whole section on strings, continue trying each set-off line of code in the Shell. Try
"hello"
Note that the interpreter gives back the string with single quotes. Python does not care what system you use. Try
'Hi!'
Having the choice of delimiters can be handy.
Figure out how to give Python the string containing the text: I’m happy. Try it. If you got an error, try it with another type of quotes, and figure out why that one works and not the first.
There are many variations on delimiting strings and embedding special symbols.
Note: A string can have any number of characters in it, including 0. The empty string is ’’ (two quote characters
with nothing between them). Many beginners forget that having no
characters in the middle is legal. It can be useful.
Strings are a new Python type. Try
type('dog')
type('7')
type(7)
The last two lines show how easily you can get confused! Strings
can include any characters, including
digits. Quotes
turn
even digits into strings.
String Concatenation
Strings also have operation symbols. Try in the Shell (noting the space after very):
'very ' + 'hot'
The plus operation with strings means concatenate the strings. Python looks at the type of operands before deciding
what operation is associated with the +. Think of the relation of addition and multiplication of integers, and then guess the meaning of
3*'very ' + 'hot'
Were you right? The ability to repeat yourself easily can be handy. Predict the following and then test. Remember the last section on types:
7+2
'7'+'2'
Python checks the types and interprets the plus symbol based on
the type. Try
'7'+2
With mixed string and int types, Python sees an ambiguous expression, and does not guess which you want - it just gives an error!
This is a traceback error. These occur when the code is being executed. In the last two lines of the traceback it shows the Python line where the error was found, and then a reason for the error. Not all reasons are immediately intelligible to a starting programmer, but they are certainly worth checking out. In this case it is pretty direct. You need to make an explicit conversion, so both are strings if you mean concatenation, ’7’ + str(2), or so both are int if you mean addition, int(’7’) + 2.
0 Comments:
Post a Comment
If you have any doubts . Please let me know.