We’re also going to look at some initial things that help make coding in Python easier.
Multi-line lines of code
The first issue is what to do if a line of code starts getting really long. In Python, by default, the line of code ends when you move to a new line. However, you can extend code past that line by adding a backslash ( \ ) character as the last line of the program.
Typically, you will indent your code so that it lines up with the first part of an expression on the above line, like is shown below:
answer = var1 + var2 + var3 + \
var4 + var5 + var6
If you are inside a set of parentheses, like you are calling a function, the backslash character is not needed, as is shown below:
print('Today is ', todaysDate, ' and it should be ',
todaysWeather)
#or using our previous example
answer = (var1 + var2 + var3 +
var4 + var5 + var6)
Modifying Print
If you use the print command multiple times, you might notice that each time you call the command, it goes on a new line. This doesn’t have to be however. Specify end = ‘ ‘ and you will remove the new line character at the end, and use a space instead. Consider the example below:
print(myString, end = ' ')
Additionally, Python puts a space between each new element in a print command when they are separated by a space. You can change that by changing the default space to something else with sep=’ ‘. For example you might use a comma for a list of items.
print("Things bought at the store today:", end = ' ')
print(item1, item2, item3, sep=', ')
Escape Characters
Using the backslash (yes that one again) allows us to escape characters. This usually means printing something that cannot be normally printed, like a new line character, a tab, etc.
\n | newline – moves the user output to the next line |
\t | tab – skip to the next tab position from where you are |
\\ | Allows you to print the backslash character |
\’ | Prints a single quote character |
\” | Prints a double quote character |
String Concatenation
Concatenation is the idea of adding two or more strings together, either as literal strings, variables, or a combination. In Python, we use the plus ( + ) character to concatenate. (Note: Different languages use different characters to concatenate. Other languages might use the ampersand (&), dot (.), or require the use of a function. )
To use the string, you simply specify the <first string> + <second string> like the example below.
print("Hello " + yourName)
Formatting Numbers
It doesn’t take long to work with numbers before you start getting numbers with crazy long decimal places starting to show. That’s where the format function comes into play. It allows you to format numbers in easy to use manner.
print(format(12.3456789, '.2f')) # show two decimal places
print(format(12.3456789, 'e')) # show in scientific notation
print(format(987654.56789, ',.2f')) # show two decimal places, with comma seperator
print(format(.345, '%')) # display as a percentage
Named Constants
Named constants allow us to define values, to make our code easier to read, but that won’t change. They are written in all caps, an use an underscore ( _ ) to specify a name change.
FIRST_FLIGHT = 1903
INTEREST_RATE = 0.01
AUTHOR = "Walter Wimberly"
Python Output – Beginner Tips was originally found on Access 2 Learn