Module Summary - Python
Module Summary - Python
Module Summary - Python
One you learn how to code in one programming language it will be easier to learn other
programming languages, as the principles are the same.
Comments are very useful in your code to help you or someone else understand:
- Anything that might be helpful to know if you or someone else looking at the code later and trying to
understand it.
DISPLAYING TEXT
One of the simplest but most important things you need that ability to do in programming
is display text.
Your text inside prints brackets must be enclosed with quotes, Python accepts both single ‘
and double “ quotes.
For printing on multiple lines you could use multiple print statements or you can use \n
which declares a new line for the text that follows it.
You can you triple quotes for the text to display, as you have typed it into the editor this is
not a recommended method as most other programming languages don’t support it.
STRING VARIABLES
Input
For getting input from the user in python you use the input command.
Input is broken into two parts:
- A message you specify to display to the user.
- User input.
You will use a variable to record the value entered by the user.
Variables
A variable is a place holder or object you can store a value or piece of data and come back to it
later. Variables are named storage locations or objects that contain data you can access later or
as you need to.
Variables and strings can be combined with the + symbol, you often need to add punctuation or
spaces to display the output properly.
Variables contents can be manipulated with functions, for example you can use the .lower()
function to change a string to all lowercase.
Functions are a section of a program that performs a specific task.
Naming Variables and Guidelines
Variables are named by you, a variable can have their value change later in any stage of the code.
- You should use names that make sense or relevant to what they are being used for, but that
are not too long.
- Use a casing scheme, PascalCasing and camelCasing are two common schemes used.
Programmers do not memorize all the functions, when they need these functions they use
IntelliSense to prompt them, they have or get documentation and they can search on the
internet.
IntelliSense doesn’t always prompt you with a function, because it might not know what
you’re going to store in the variable. To get around this you initialize your variable, which means
when you create it you give the variable a value.
//
STORING NUMBERS
When working with variables remember you can store numbers as well as strings, the quotes
around the text indicates a string if you want to assign a numeric value to the variable you simply
enter the number.
You can perform math operation on variables containing numeric values, the operation symbols
for maths in programming are slightly different but many are the same.
+ Addition
- Subtraction
* Multiplication
/ Division
The + symbol signifies addition in maths and for combining two strings together, and will not
allow you to add a numeric value to the end of a string. Therefore, you put a place-holder in your
string and after you close off your string you specify the value you want to put in the place holder.
Inputting Numbers
If you have more than one file in Visual Studio project you will have to specify which file you
want to run, or assign the file you are working on as the Startup file, the default startup file is the
first file created.
Setting up user input is similar to setting up input for strings however the input function by default
reads you input as a string so you need to convert your input into a numeric value
Dates
For working with dates you will need to import the date library into your program.
A library is a collection of precompiled routines and methods for you to use in your program
Import is a function for making objects and libraries available to your program.
Without importing libraries, certain functions won’t be available to you, they won’t even show up on
the InteliSense.
Date Formats
The function strftime can also be used to format time the same as for dates but
the parameters are specific to time.
//
If Statements
When solving problems with your code, your code will need to make decisions or check conditions
for this you use an if statement or an if else statement.
When checking two strings are equal to each other with an if statement, it is often best to convert
both of them to upper or lower case, as python is case sensitive and will not properly work less
both are the same case.
There are generally two different ways to write an if statement you should always consider which
method makes the most sense and is the simplest.
Branching