Module Summary - Python

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

INTRODUCTION

 The Basic principle of programming is that it is problem solving.

 Python is one of the easier programming languages to learn, and many of its tools are


free to download and use.

 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.

 There are a lot of different variations of Python, IronPython, Ipython, CPython to name a


few. Most of python’s variations only have some syntax differences but not too many.

 Comments are very useful in your code to help you or someone else understand:

- What your program does,

- What a particular line or section of code does;

- Why you chose to do something a particular way;

- Anything that might be helpful to know if you or someone else looking at the code later and trying to
understand it.

 The # is the symbol to indicate using comments in python.

DISPLAYING TEXT

 One of the simplest but most important things you need that ability to do in programming
is display text.

 For displaying text in Python you use the print() statement.

 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.

 You will make mistakes when programming all programmers make mistakes from typing


mistakes to logic mistakes, it is not something to stress over.
 Recommend solutions to fixing mistakes you can’t find or having trouble with:

- Walk away and come back to it,

- Have another coder look at it;

- Step though your code with your debugger.

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.

When naming your variables there are rules:


- You can’t have spaces,
- Variables are case sensitive;
- Cannot start with a number.

Guidelines for creating variables:

- 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.

 Visual Studio has a built in feature called IntelliSense, which will suggest possible


functions that you can call after you type the full stop “.”.

 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.

Common math operations:

+ Addition

- Subtraction

* Multiplication

/ Division

** Exponent (is for cubing or the power of a value)

% Modulo (is giving you the remainder of a division)

The math rules of order operations are the same in programming as it is in normal mathematics  


Formatting Numbers

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.

There are different formats for the place-holders from assigning the amount of decimal


points to leaving spaces in front of your values. You can also use a Format method to Format
numeric values.

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

WORKING WITH DATES AND TIMES

 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 format of your date is important as different countries and


companies have different date formats.
 The default format is Year-Month-Day.
 In python you use strftime function to format dates, it takes in the parameters; %d for day,
%b for month, and %y for year.
 There are alternate parameters for the function if you want to display the date in different
formats such as the full year typed out.
 If your program uses input to take in a date you will need to convert it to a date datatype,
the strptime function allows you to do this.

 Working with Time


 For working with time you use the same library as date as the library is called datetime.

 The function strftime can also be used to format time the same as for dates but
the parameters are specific to time.

//

MAKING DECISIONS WITH CODE

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.

Constraints for checking your if statements:


== is equal to
!= is not equal to
< is less than
> Is greater than
<= is less than or equal to
>= is greater than or equal to

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

Branching is when your if condition is not true, so something else happens.

A Boolean variable is one that holds the value true or false.

Code in an else statement is only executed if the condition is not true.

You might also like