a saying or expression whose meaning is not obvious from its individual parts
an expression that is natural to a language
generally native speakers, or people who know the language, know the idioms
it's raining cats and dogs
hace mucho frío
les carottes sont cuites
Some French and Spanish Idioms
BTW, does anyone know the literal and idiomatic translations for these sayings?
hace mucho frío
les carottes sont cuites
hace mucho frío
it makes much cold
it's very cold
les carottes sont cuites
the carrots are cooked
I've had enough!
Idioms in Programming Languages
An idiom in a programming language is:
a common task, computation, or construct that's expressed in a way that's unique, peculiar to or encouraged by the particular programming language in use
usually only those who use the language often know the idiomatic way to express things…
shows knowledge of the language, culture, and conventions / norms
More About Programming Language Idioms
multiple ways to express something, but language or community of language users prefer the idiomatic way
not necessarily formally specified
does not have to be followed
if idioms are not used, program can still run
if you don't do it idiomatically, you won't cause errors
Let's See an Example of an Idiom
in Python, there are a few ways to check if a value is in a list of values; two are shown below
both achieve the same result, but the second version is preferred
(we'll look at lists later in the semester)
About Style
style is how the code is formatted; it's how it looks
why is style important? →
you'll probably spend as much time reading code as writing it:
code you've written
example code from books, specifications, forums, etc.
other people's code (collaboration, inherited code, etc.)
if everyone follows the same style, it'll be easier to read and edit existing programs
PEP 8
In Python, an enhancement proposal, called PEP 8, lays out some good rules to follow.
Here Are a Few Conventions To Follow
consistency within a file / project!
4 space indentation (automatic in IDLE) →
use blank lines to separate groups of related code to enhance readability
And a Few More…
surround operators with whitespace in expressions
only use lowercase letters in variables names
separate words in variable names using underscores
Make sure your code is formatted in a way that is readable and is consistent with common conventions.