473,394 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

x*x if x>10

I have seen somewhere that you can write something like:

x*x if x>10

but exactly that doesn't work and I can't get any variation to work.

it is possible to nest with an else too.
how do you write it?
and also, is it idiomatic? doesn't seem to add functionality, just
another way of doing the same thing which is quite unpythonic but I
remember reading it was added because it helped simplify the
expression of a certain type of operation.
Jul 27 '08 #1
2 1114
ssecorp schrieb:
I have seen somewhere that you can write something like:

x*x if x>10

but exactly that doesn't work and I can't get any variation to work.

it is possible to nest with an else too.
how do you write it?
and also, is it idiomatic? doesn't seem to add functionality, just
another way of doing the same thing which is quite unpythonic but I
remember reading it was added because it helped simplify the
expression of a certain type of operation.

It's a ternary operator as found in e.g. C or Java like this;
foo = <condition? <true-value: <false-value>;

And it's become available in python2.5, anything below that version will
throw an error.

Diez
Jul 27 '08 #2
On Jul 27, 10:13*pm, ssecorp <circularf...@gmail.comwrote:
I have seen somewhere that you can write something like:

x*x if x>10

but exactly that doesn't work and I can't get any variation to work.
It's called a ternary operator. The format is:

<label= <true-valueif <conditionelse <false-value>
it is possible to nest with an else too.
Sure. You can extend the <false-valuewith another ternary operator:

<label= <value1if <condition1else <value2if <condition2else
<value3>
Jul 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: George | last post by:
Hi, The storeprocedure fails with null exception on the DB2 development centre for SELECT querys for all tables that has the number of columns greater than 10. Strange ,but true ..i reinstalled...
1
by: Raj Tripathi | last post by:
I am using ACT to stress test a simple (HelloWorld) WebService method. When number of connections is <= 10 everything works just fine. But increasing connections causes Http 403 errors. Whats...
5
sanjay123456
by: sanjay123456 | last post by:
Sir i want that mod operation on gr8ter than 10 bit by following code of C long d,c,b,n; int i; d= 65073305
18
by: DaveM | last post by:
On Sun, 27 Jul 2008 05:24:36 -0700 (PDT), alex23 <wuwei23@gmail.comwrote: I've seen the PERL saying/motto/boast, "There's more than one way to do it" derided on more than one occasion on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.