Uploaded by Nikhil Narayanan

Fundamentals of Programming in Python

advertisement
Fundamentals of
Programming in Python
Part 1: IDLE, Hello World
Overview of these notes
• These notes are intended as a reference guide and give an overview
of key programming concepts
• Once you have familiarized yourself with Python programming, we
will apply all of this in Java (which, ultimately is what we will study)
• Of course you can continue to develop your programming skills in
Python (you can make very complex programs in Python)
Programming in Python- Interfaces
• Once you have downloaded the Python Integrated Learning and
Development Environment (IDLE), there are two windows within
which we can write/develop code:
”Shell”
“File” or
“Script”
Programming in Python- Interfaces
• The Shell, also known as the console or command line is where you
can execute programs from, receive input and output from/to your
program and can type and run some code
• It is not very useful for writing larger programs with multiple lines of
code
• For our intents and purposes, we can consider the Shell to be where
we will see our program outputs (and take in inputs)
Programming in Python - Interfaces
• The File or Script is where you will write and code up your programs
in Python
• You can save the file (as .py extension) and combine many files in one
program
• It functions like any other text editor, except that:
• It will highlight Python keywords
• It will interpret and compile the code for you
• It will automatically indent code where appropriate
Running your first Python script
• Open a new file in Python, call it “HelloWorld.py”
• Type the following code into your script:
print(“Hello World!”)
• Press “Cmd + F5” or “Ctrl + F5” to run your script
• Congratulations! You have written your first program!
Python keywords
• Python has number of keywords that it has reserved for various
functions, operations or variables
• It will automatically highlight them, and it also means that you cannot
use these words as names, variables etc.
Examples:
for, while, in, if, else, range, print, elif, import,
enumerate, max, min, to, from
• There are many more that you will run into, and you’ll know if if it
highlights in purple or orange
Python Variables
•
•
•
•
A variable is a name assigned to a value
These can be numbers, words or any other data type of our choice
Variables are mutable (changeable)
In Python we can create and assign a variable with the “=“
myNum = 0
myName = ”Mr. Nik"
• Python doesn’t care what you put after the ”=“ as long, of course, the data
type does change later in your script
• Variable names cannot have spaces!
Data Types
• This will be a more significant issue in Java, but it is good to briefly go
over some data types
• A data type fundamentally refers to how much memory a variable will
occupy
• For example, an integer occupies 4 bytes of memory while a Boolean
occupies 1 byte of memory
• You do not need to specify the data type beforehand
Python Comments
• Commenting is a way of describing what your code is doing both to
yourself and others
• You can comment in Python by adding a “#” in front of a sentence:
# this is a comment
• You typically want to comment at the very least at the header of your
script:
# Date: 1 January 2021
# Program: Sorting Algorithm
• You should comment your code frequently and is generally good
practice for the IB but also future programming that you may do
Python Calculations
• Calculations in Python work in an intuitive manner, with the usual
math operators defined:
Operator
Definition
*, +, -, /
Multiplication, addition, subtraction,
division
**
Exponentiation
%
Modulus
• For more complex Math operations, we will need to import another
library to our script (more on libraries later on!)
Practice!
1. Write a program that calculates the GPA given a series of grades.
Manually assign the grades as follows and calculate the average.
Save it to a variable called avg. Print the average to the shell.
grade1 = 55
grade2 = ...
# as many grades as you wish
avg = #write code here
print(“Your average is” + avg)
Download