[newbie questions] if conditions - if isset - if empty

Mahmoud Abdel-Fattah accounts at abdel-fattah.net
Sat Apr 14 18:16:18 EDT 2012


Hello,

I'm coming from PHP background ant totally new to Python, I just started
using scrapy, but has some generic question in python.

1. How can I write the following code in easier way in Python ?
if len(item['description']) > 0:
            item['description'] = item['description'][0]
        else:
            item['description'] = ''


In PHP I can write it as follows :
$item['description'] = (len(item['description']) > 0)
? item['description'][0] : '';

 So, is there similar way in Python ?



2. How can I check if this variable defined or not, in PHP I can use
isset();, in other way, how can I make the following syntax in Python ?
$variable = isset($array['element']) ? true : false;



Thanks a lot,
Mahmoud
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120415/f40296e4/attachment.html>


More information about the Python-list mailing list