range() returns the word itself [duplicate] - python-3.x

This question already has answers here:
Python 3 turn range to a list
(9 answers)
Closed 5 years ago.
I'm using python 3.6
when I use the range function in Python console, it didn't return an array, instead it shows the wording itself:
range(1, 5)
range(1,5)
print(range(1,5))
range(1,5)
How can I show the array?

The range() function in Python 3 returns an iterator, something you can iterate on, so you can use it as:
for x in range(1, 5):
print(x)
This iterator returns one value at a time, so it can be more memory efficient.
If you want to get a list, you can use this code:
list(range(1,3))

Related

Query on Slicing a list in Python [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 5 days ago.
How do I print the reverse of a specific selection of a list ?
For example, I have a list,
a = [11,22,33,44,55,66,77,88]
I expect a[1:4:-1] to print [44,33,22], but it gives an empty list.
I have seen Understanding slicing, but I couldn't find an answer.
The python slicing syntax, when 3 parameters are provided, is [start:stop:step]
In order to get the value [44, 33, 22], you'd need to write the slice like this: a[3:0:-1]
This works because the slice starts at index 3, which has a value of 44, and the slice stops before index 0, meaning the last included index is 1 (with a value of 22), based on the step value of -1.

how to analyze the logic of func RANGE in python? [duplicate]

This question already has answers here:
The order of nested list comprehension and nested generator expression in python
(5 answers)
Explanation of how nested list comprehension works?
(11 answers)
Closed 1 year ago.
I am still confused those sentence after I run them, I mean how do they execute step by step, someone can give some suggestions?
print([j for i in range (10) for j in range (5)])
print([[j for i in range (10)] for j in range (5)])
print([j for i in range (10) for j in range (5) for k in range(3)])
Largely, list comprehensions are concise for loops. Sometimes they can be difficult to understand and for that reason, I would suggest converting the list comprehension into a for loop for understanding.
print([j for i in range(10) for j in range(5)])
Is the same as:
result = []
for i in range(10):
for j in range(5):
result.append(j)
The others are very similar, can you try and convert them to for loops?
Hint (2): The second one has a nested list, so you'll need to initialize another list and append that list to the main list, result.
Hint (3): The last list comprehension has another nested for loop with another variable k.

Python 3 : List of odd numbers [duplicate]

This question already has answers here:
Python "for i in" + variable
(3 answers)
Closed 2 years ago.
I'm trying to return a list of odd numbers below 15 by using a python user-defined function
def oddnos(n):
mylist = []
for num in n:
if num % 2 != 0:
mylist.append(num)
return mylist
print(oddnos(15))
But I'm getting this error :
TypeError: 'int' object is not iterable
I didn't understand what exactly this means, please help me find my mistake
Because 15 is an integer, not a list you need to send the list as an input something like range(0,15) which will give all numbers between 0 and 15.
def oddnos(n):
mylist = []
for num in n:
if num % 2 != 0:
mylist.append(num)
return mylist
print(oddnos(range(0,15)))
When you're passing values to the function oddnos, you're not passing a list of values till 15, rather only number 15. So the error tells you, you're passing an int and not a list, hence not iterable.
Try to use range() function directly in the for loop, pass your number limit to the oddnos function.

Looping columns [duplicate]

This question already has answers here:
Printing using list comprehension
(14 answers)
List comprehensions leak their loop variable in Python2: how making it be compatible with Python3
(2 answers)
Do variables defined inside list comprehensions leak into the enclosing scope? [duplicate]
(1 answer)
List comprehension rebinds names even after scope of comprehension. Is this right?
(6 answers)
Closed 4 years ago.
I'm amazed, maybe someone can explain what's happening....
When I run this very simple example:
df = pd.DataFrame(columns=['A','B','C'])
results = pd.DataFrame(columns=df.columns)
for i, col in enumerate(df):
print('.....'+col)
result = [print(col) for i in range(2)]
The result is (col is unknown the 1st time):
.....A
A
A
.....B
A
A
.....C
A
A
But what I really expected is:
.....A
A
A
.....B
B
B
.....C
C
C
What is happening??
I just ran:
import pandas as pd
df = pd.DataFrame(columns=['A','B','C'])
results = pd.DataFrame(columns=df.columns)
for i, col in enumerate(df):
print('.....'+col)
result = [print(col) for i in range(2)]
and it returned:
Out[]:
..A
A
A
..B
B
B
..C
C
C
Python 3.6.5
Pandas 0.20.3

How can I convert a string to a list in Python using a for loop? [duplicate]

This question already has answers here:
String to list in Python
(5 answers)
Closed 6 years ago.
Here's what I have:
def reverse(text):
opposite = []
for character in text:
stuff = opposite.append(character)
return stuff
Is there something wrong with this for loop?
Couldn't you use the split method instead it will return a list
text.split()
Or even
list(text)
Which would list out each character
yourstring='welcome'
l=[]
for char in yourstring:
l.append(char)
print l

Resources