Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner:Python
#1
Hello,

I am new in Python programming and started reading Python documentation. Few queries I would like to ask:

def fib2(n):  # return Fibonacci series up to n
   """Return a list containing the Fibonacci series up to n."""
   result = []     #why required this?
   a, b = 0, 1
   while a < n:
       result.append(a)    # see below
       a, b = b, a+b
   return result
I'd like to know why it is required to initialize result=[]? However it was stated that it does not require to "declare" variable before assigning to it.

I tried to use it before assigning to it and it said:
"> UnboundLocalError: local variable 'c' referenced before assignment".

Another question I am trying to solve in beginner section of Codechef

Here is the snippet of code:

n,k = (input().split())
print(type(n),type(k))
#str type
n= int(n)
k=int(k)
#int type
print(type(n),type(k))

while(n):
    n=n-1
    num = (input().split())
    print(type(num))
#list type
    num = int(num)
    if(num/k==0):
        ctr=ctr+1
print(ctr)
Initial print statements prints class str class str and class 'int' class 'int'. However, the print inside the while statement prints class 'list'.
Could you please explain why it is of type list?
Any idea/help would be appreciated
Thanks.
Reply


Messages In This Thread
Beginner:Python - by Shaswat - Apr-01-2019, 01:04 PM
RE: Beginner:Python - by ichabod801 - Apr-01-2019, 02:10 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020