Programming with Python, for beginners

Slides:



Advertisements
Similar presentations
16-May-15 Sudden Python Drinking from the Fire Hose.
Advertisements

Python November 14, Unit 7. Python Hello world, in class.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Introduction to Python
Guide to Programming with Python Chapter Two Basic data types, Variables, and Simple I/O: The Useless Trivia Program.
Python quick start guide
Lecture 2 - Variables, program execution, calculations, print() COMPSCI 101 Principles of Programming.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Introduction to Python
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
PROGRAMMING BASICS Programming for non-programmers. You will need Python Go to
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Input, Output, and Processing
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Fundamental Programming: Fundamental Programming Introduction to C++
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Python Let’s get started!.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Data Manipulation Variables, Data Types & Math. Aug Variables A variable is a name (identifier) that points to a value. They are useful to store.
GCSE Computing: Programming GCSE Programming Remembering Python.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Input, Output and Variables GCSE Computer Science – Python.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
GCSE COMPUTER SCIENCE Practical Programming using Python Lesson 4 - Selection.
Chapter 2 Writing Simple Programs
G. Pullaiah College of Engineering and Technology
Chapter 1.2 Introduction to C++ Programming
GCSE COMPUTER SCIENCE Practical Programming using Python
Python Review 1.
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Chapter 1.2 Introduction to C++ Programming
Whatcha doin'? Aims: To start using Python. To understand loops.
Topics Designing a Program Input, Processing, and Output
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Input and Output Upsorn Praphamontripong CS 1110
Python Let’s get started!.
Introduction to Python
Formatting Output.
Lesson 4 - Challenges.
Python: Control Structures
IF statements.
Presented By S.Yamuna AP/IT
Variables, Expressions, and IO
Programming 101 Programming for non-programmers.
Functions CIS 40 – Introduction to Programming in Python
Engineering Innovation Center
Engineering Innovation Center
Number and String Operations
Introduction to C++ Programming
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
A look at Python Programming Language 2018.
CISC101 Reminders All assignments are now posted.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Beginning Python Programming
Unit 3: Variables in Java
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Data Types and Maths Programming Guides.
Hardware is… Software is…
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Programming with Python, for beginners By @GirlGeekUpNorth

About Me Taught myself to code Founded a Code Club for Adults Manager of MadLab STEM Ambassador Digital Skills Facilitator

Objectives To be able to read and write code Understand the basic building blocks of a program Display information to users (output) Name different data types Store data provided by users (input) Collect data from the user and display it in a way that you choose

Some Things to Consider Programming is a skill – it takes practice, and you need to expect to make mistakes Reading code is easier when done right to left Think 6 = 3 + 3 instead of 3 + 3 = 6 Mistakes are useful – they help you learn

Get Involved! This is not a classroom, if you need to leave then that’s ok Ask questions Say if you’re confused or we’re going too fast

Natural Language vs Formal Language Question: How is spoken language different to a programming language? Check out the next slide...

Can you read this? A uvetrsniiy fnuod taht wodrs wtih mxeid up leettrs cuold slitl be raed relleivaty esilay. Hndas up if you can raed tihs.

Natural Language vs Formal Language How is spoken language different to programming language? Natural language can be structured or unstructured: “I’m going to the park later; are you coming?” “Park. Later. Coming?” Formal languages, like mathematics and Python have rules. 3 + 3 = 6 is syntactically correct. 3 3 += 6 is not. The words and symbols have to be in the right order

What can computers do? Compare and manipulate values This is done using operators (+, -, *, /, <, ≤, ==, !=, <>, >, ≥, …) Store values in memory Variables hold values and have different types. Alter the flow of execution Loop through something a certain number of times. for loops, while loops Split the program into paths and control what happens when. if…else if…else statements, try…catch…finally statements Define and run functions Almost always followed by (brackets).

Trinket.io Can be used to program with Python without worrying about installing software or using the terminal www.trinket.io

“Hello, World” print (“Hello, World”) Questions: What did that do? What can we learn from it? What else can we print()?

“Hello, World” What did we do? Reading right to left: Set up a string parameter of “Hello, World!” Passed it to a function called ‘print’ Output the string to the screen

“Hello, World” What did we learn from it? 1) Python doesn’t need additional setup. 2) The output function is print() 3) Functions can be spotted by their (brackets) 4) We can give functions extra information (parameters) in their brackets 5) Written text needs quotation marks around it

“Hello, World” What else can we print? print (“MadLab”) print (27) print (3.5) print (“Once upon a time…..”)

Data Types Text is referred to as a “string” Numbers can be integers (whole numbers) Or floating point numbers (decimals) Booleans are True or False string “hello world” integer 5 float 2.7 boolean True

Data Types Python can work out the data type for you Try entering into the console print type() What different data types can you find?

Data Types We can find out how values are treated using the ‘type’ function. print type(“Hello, World!”) print type(‘Hello, World!’) print type(17) print type(3.2) print type('17') print type('3.2') print type(1000000) print type(1,000,000)  This will cause an error because commas are special

Operators Operators allow us to manipulate variables +, -, *, /, <, >, ==, % print (6 / 3) print (5 + 7) print (10 % 6) print (4 * 5)

Putting it all together Just as numbers can be added together to form bigger numbers, strings can be added to each other to form bigger strings. (This is called concatenation.) print(“Hello ” + “coding” + “ people”) Remember the spaces – the computer won’t do it for you!

Inputs So if the print() function allows us to output our data, how do we get the data in? Data coming into the program needs to be stored immediately, otherwise the computer loses track of it. For this we use variables. They hold data in memory so we can access it later. They have types (int, float, string, etc.) They have a name. Names can’t start with a number or a symbol.

Variables Variables hold data in memory so we can access it later. They have types (int, float, string, etc.) They have a name. variable_name = variable Example my_name = “Claire” my_age = 21

Variable Types name = “Claire” age = 32 town = “Salford” print name print type(name)

Converting Data Types Define your variables legs = 4 pet = “Dog” name = “Charlie” Concatenate your variables into this sentence print (“My “ + pet + “ called “ + name + “ has “ + legs + “ legs”)

Converting Data Types legs = 4 pet = “Dog” name = “Charlie” legs = str(legs) print “My “ + pet + “ called “ + name + “ has “ + legs + “ legs” string = str, integer = int, float = float

Raw Input How about we look at how a program can request information? Raw Input is a form of asking for data name = raw_input(“What is your name?”) print (name) print type(name)

Raw Input Questions: What did that do? Reading right to left: Set up a string parameter of “What is your name?” Sent it to a function called ‘raw_input’ Waited for the user to enter data Assigned the new data a position in memory. Called that area of memory ‘name’ and typed it as a string variable

Raw Input Questions: What can we learn from it? Reading right to left: The string parameter we pass to raw_input() is used as a prompt. The function returns a value We can store the returned value ‘=’ (equals) is the assignment operator Variables do not have quotation marks around them.

Raw Input & Data Types As a default, raw_input returns the data as a string If we want the data to be treated as something else, we must pre-define this age= raw_input(“How old are you”) print type(age)

Converting Raw Inputs age = int(raw_input(“How old are you?”)) price = float(raw_input(“What does it cost?”)) print type(age) print type(price)

Summary You have used functions print() sends data to the screen type() tells you the type of data raw_input() gets data from the user (as a string) float(), int() and str() convert values

Challenge! Ask the user for their name, store this as a string Ask the user their age, store this as an integer Convert the user’s age into a string Concatenate the name and age into this sentence; The user is called name and they are age years old Print the result of each step to the console

Functions We have used built-in functions: print(“Hello, World!”) type(text) raw_input(“Type something. ”)

Whitespace Whitespace is used to structure code, and must be used correctly Indentations are used to separate blocks of code One indentation is either one stroke of the tab key, or four strokes of the space bar Indent your code to show it belongs to the code above

Defining Functions We can use Python to define our own functions def addition(x,y): return x + y print addition(4,6)

Loops Computers can process the same thing thousands of times. This is called looping and there are two types. for loops do things a certain number of times for i in range(10): print (i) for letter in “Hello”: print(letter)

Break it down for i in range(10): print (i) print range(10) ‘i’ represents the iteration through the range, the loop function simply moves ‘i’ along the list

Loops Computers can process the same thing thousands of times This is called looping and there are two types while loops do things while a condition is True i = 1 while i < 10: i = i + 1 print(i)

Break it down i = 1 while i < 10: i = i + 1 print(i) ‘i’ is set to 1, each time the loop is run, the value of ‘i’ is increased by 1

Summary Loops Useful for repetitive tasks Can be broken or continued Complex series, games, artificial intelligence Can be broken or continued ‘For’ Loops Do the same thing a limited amount of times ‘While’ Loops Check a condition each time and loop again if it is true

If/Else Statements What if we wanted to change the outcome of our function, depending on what information we feed in? def bythree(number): if number % 3 == 0: return “divisible by three” else: return “not divisible by three”

If/Else Statements def bythree(number): if number % 3 == 0: return “divisible by three” else: return “not divisible by three” print bythree(9) print bythree(7)

Elif If more than two conditions need to be evaluated, we use elif for additional parameters def bythree(number): if number % 3 == 0: return “divisible by three” elif number == 5: return “you entered 5!” else: return “not divisible by three”

Challenge! Given two integer values, return their sum. Unless the two values are the same, then return double their sum. sum_double(1, 2) → 3 sum_double(3, 2) → 5 sum_double(2, 2) → 8 def sum_double(a, b):

What Next? Codecademy – Python Track Coursera – Programming for Everyone CodeUp Manchester