Real Python: Python 3 Cheat Sheet

Similar documents
06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

CSC Software I: Utilities and Internals. Programming in Python

Lecture 3. Input, Output and Data Types

ENGR 101 Engineering Design Workshop

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Getting Started Values, Expressions, and Statements CS GMU

Variables, expressions and statements

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Comp Exam 1 Overview.

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

Python as a First Programming Language Justin Stevens Giselle Serate Davidson Academy of Nevada. March 6th, 2016

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

Control Structures 1 / 17

AP Computer Science A

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Intro. Scheme Basics. scm> 5 5. scm>

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Try typing the following in the Python shell and press return after each calculation. Write the answer the program displays next to the sums below.

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Chapter 17. Fundamental Concepts Expressed in JavaScript

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

UNIVERSITÀ DI PADOVA. < 2014 March >

DaMPL. Language Reference Manual. Henrique Grando

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

Compsci 101 Fall 2015 Exam 1 Rubric. Problem 1 (24 points)

Python 1: Intro! Max Dougherty Andrew Schmitt

Conditional Expressions and Decision Statements

Introduction to: Computers & Programming: Review prior to 1 st Midterm

CSCE 110 Programming I

CS 1301 Exam 1 Answers Fall 2009

CS 1301 Exam 1 Fall 2009

Python: common syntax

Getting started: 1. Join the wifi network Network: Password:

An Introduction to Python

UNIT II DATA, EXPRESSIONS, STATEMENTS. Python being a interpreter language has two modes namely: Interactive mode, Script Mode or Normal mode

Decisions, Decisions. Testing, testing C H A P T E R 7

INTERMEDIATE LEVEL PYTHON PROGRAMMING SELECTION AND CONDITIONALS V1.0

MICROPROCESSOR SYSTEMS INTRODUCTION TO PYTHON

CSCE 110: Programming I

Introduction to Python

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Introduction to Python Programming

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi

Basic Python Revision Notes With help from Nitish Mittal

CS313D: ADVANCED PROGRAMMING LANGUAGE

Text Input and Conditionals

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

The Big Python Guide

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

A variable is a name for a location in memory A variable must be declared

Problem Solving for Intro to Computer Science

3. Conditional Execution

Unit 3. Operators. School of Science and Technology INTRODUCTION

CMSC 201 Spring 2018

Python memento TI-Smart Grids

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Introduction to programming with Python

PROGRAMMING FUNDAMENTALS

Variable and Data Type I

Topic 2: Introduction to Programming

Variable and Data Type I

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Introduction to Python

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

An overview about DroidBasic For Android

Conditionals and Recursion. Python Part 4

MITOCW watch?v=rvrkt-jxvko

Variables, Constants, and Data Types

CS 102 Lab 3 Fall 2012

Programming for Engineers in Python. Recitation 1

LOOPS. Repetition using the while statement

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

Simple Java Programming Constructs 4

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

Midterm 1 Review. Important control structures. Important things to review. Functions Loops Conditionals

DATA TYPES AND EXPRESSIONS

Python Intro GIS Week 1. Jake K. Carr

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Program Elements -- Introduction

ENGR 102 Engineering Lab I - Computation

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Iteration. Chapter 7. Prof. Mauro Gaspari: Mauro Gaspari - University of Bologna -

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

UNIT 5. String Functions and Random Numbers

Chapter 10: Creating and Modifying Text Lists Modules

3. Conditional Execution

Java Bytecode (binary file)

Python for ArcGIS. Lab 1.

Transcription:

Real Python: Python 3 Cheat Sheet

Numbers....................................... 3 Strings........................................ 5 Booleans....................................... 7 Lists......................................... 9 Dictionaries..................................... 11 IF Statements.................................... 12 Loops........................................ 14 1

Python is a beautiful language. It s easy to learn and fun, and its syntax is simple yet elegant. Python is a popular choice for beginners, yet still powerful enough to back some of the world s most popular products and applications from companies like NASA, Google, Mozilla, Cisco, Microsoft, and Instagram, among others. Whatever the goal, Python s design makes the programming experience feel almost as natural as writing in English. Check out to learn more about Python and web development. Email your questions or feedback to. 2

Python has integers and floats. Integers are simply whole numbers, like 314, 500, and 716. Floats, meanwhile, are fractional numbers like 3.14, 2.867, 76.88887. You can use the type method to check the value of an object. 1 >>> type(3) 2 <class 'int'> 3 >>> type(3.14) 4 <class 'float'> 5 >>> pi = 3.14 6 >>> type(pi) 7 <class 'float'> In the last example, pi is the variable name, while 3.14 is the value. You can use the basic mathematical operators: 1 >>> 3 + 3 2 6 3 >>> 3-3 4 0 5 >>> 3 / 3 6 1.0 7 >>> 3 / 2 3

8 1.5 9 >>> 3 * 3 10 9 11 >>> 3 ** 3 12 27 13 >>> num = 3 14 >>> num = num - 1 15 >>> print(num) 16 2 17 >>> num = num + 10 18 >>> print(num) 19 12 20 >>> num += 10 21 >>> print(num) 22 22 23 >>> num -= 12 24 >>> print(num) 25 10 26 >>> num *= 10 27 >>> num 28 100 There s also a special operator called modulus, %, that returns the remainder after integer division. 1 >>> 10 % 3 2 1 One common use of modulus is determining if a number is divisible by another number. For example, we know that a number is even if it s divided by 2 and the remainder is 0. 1 >>> 10 % 2 2 0 3 >>> 12 % 2 4 0 Finally, make sure to use parentheses to enforce precedence. 1 >>> (2 + 3) * 5 2 25 3 >>> 2 + 3 * 5 4 17 4

Strings are used quite often in Python. Strings, are just that, a string of characters - which s anything you can type on the keyboard in one keystroke, like a letter, a number, or a backslash. Python recognizes single and double quotes as the same thing, the beginning and end of the strings. 1 >>> "string list" 2 'string list' 3 >>> 'string list' 4 'string list' What if you have a quote in the middle of the string? Python needs help to recognize quotes as part of the English language and not as part of the Python language. 1 >>> "I cant do that" 2 'I cant do that' 3 >>> "He said \"no\" to me" 4 'He said "no" to me' Now you can also join (concatenate) strings with use of variables as well. 1 >>> a = "first" 2 >>> b = "last" 3 >>> a + b 4 'firstlast' If you want a space in between, you can change a to the word with a space after. 1 >>> a = "first " 2 >>> a + b 3 'first last' There are different string methods for you to choose from as well - like upper(), lower(), replace(), and count(). upper() does just what it sounds like - changes your string to all uppercase letters. 1 >>> str = 'woah!' 2 >>> str.upper() 3 'WOAH!' Can you guess what lower() does? 5

1 >>> str = 'WOAH!' 2 >>> str.lower() 3 'woah!' replace() allows you to replace any character with another character. 1 >>> str = 'rule' 2 >>> str.replace('r', 'm') 3 'mule' Finally, count() lets you know how many times a certain character appears in the string. 1 >>> number_list =['one', 'two', 'one', 'two', 'two'] 2 >>> number_list.count('two') 3 3 You can also format/create strings with the format() method. 1 >>> "{0} is a lot of {1}".format("Python", "fun!") 2 'Python is a lot of fun!' 6

Boolean values are simply True or False. Check to see if a value is equal to another value with two equal signs. 1 >>> 10 == 10 2 True 3 >>> 10 == 11 4 False 5 >>> "jack" == "jack" 6 True 7 >>> "jack" == "jake" 8 False To check for inequality use!=. 1 >>> 10!= 10 2 False 3 >>> 10!= 11 4 True 5 >>> "jack"!= "jack" 6 False 7 >>> "jack"!= "jake" 8 True You can also test for >, <, >=, and <=. 1 >>> 10 > 10 2 False 3 >>> 10 < 11 4 True 5 >>> 10 >= 10 6 True 7 >>> 10 <= 11 8 True 9 >>> 10 <= 10 < 0 10 False 11 >>> 10 <= 10 < 11 12 True 13 >>> "jack" > "jack" 14 False 7

15 >>> "jack" >= "jack" 16 True 8

Lists are containers for holding values. 1 >>> fruits = ['apple','lemon','orange','grape'] 2 >>> fruits 3 ['apple', 'lemon', 'orange', 'grape'] To access the elements in the list you can use their associated index value. Just remember that the list starts with 0, not 1. 1 >>> fruits[2] 2 orange If the list is long and you need to count from the end you can do that as well. 1 >>> fruits[-2] 2 orange Now, sometimes lists can get long and you want to keep track of how many elements you have in your list. To find this, use the len() function. 1 >>> len(fruits) 2 4 Use append() to add a new element to the end of the list and pop() to remove an element from the end. 9

1 >>> fruits.append('blueberry') 2 >>> fruits 3 ['apple', 'lemon', 'orange', 'grape', 'blueberry'] 4 >>> fruits.append('tomato') 5 >>> fruits 6 ['apple', 'lemon', 'orange', 'grape', 'blueberry', 'tomato'] 7 >>> fruits.pop() 8 'tomato' 9 >>> fruits 10 ['apple', 'lemon', 'orange', 'grape', 'blueberry'] Check to see if a value exists using in the list. 1 >>> 'apple' in fruits 2 True 3 >>> 'tomato' in fruits 4 False 10

A dictionary optimizes element lookups. It uses key/value pairs, instead of numbers as placeholders. Each key must have a value, and you can use a key to look up a value. 1 >>> words = {'apple': 'red','lemon': 'yellow'} 2 >>> words 3 {'apple': 'red', 'lemon': 'yellow'} 4 >>> words['apple'] 5 'red' 6 >>> words['lemon'] 7 'yellow' This will also work with numbers. 1 >>> dict = {'one': 1, 'two': 2} 2 >>> dict 3 {'one': 1, 'two': 2} Output all the keys with keys() and all the values with values(). 1 >>> words.keys() 2 dict_keys(['apple', 'lemon']) 3 >>> words.values() 4 dict_values(['red', 'yellow']) 11

The IF statement is used to check if a condition is true. Essentially, if the condition is true, the Python interpreter runs a block of statements called the if-block. If the statement is false, the interpreter skips the if block and processes another block of statements called the else-block. The else clause is optional. Let s look at two quick examples. 1 >>> num = 20 2 >>> if num == 20: 3... print('the number is 20') 4... else: 5... print('the number is not 20') 6... 7 the number is 20 8 >>> num = 21 9 >>> if num == 20: 10... print('the number is 20') 11... else: 12... print('the number is not 20') 13... 14 the number is not 20 You can also add an elif clause to add another condition to check for. 1 >>> num = 21 12

2 >>> if num == 20: 3... print('the number is 20') 4... elif num > 20: 5... print('the number is greater than 20') 6... else: 7... print('the number is less than 20') 8... 9 the number is greater than 20 13

There are 2 kinds of loops used in Python - the for loop and the while loop. for loops are traditionally used when you have a piece of code which you want to repeat n number of times. They are also commonly used to loop or iterate over lists. 1 >>> colors = ['red', 'green', 'blue'] 2 >>> colors 3 ['red', 'green', 'blue'] 4 >>> for color in colors: 5... print('i love ' + color) 6... 7 I love red 8 I love green 9 I love blue while loops, like the for Loop, are used for repeating sections of code - but unlike a for loop, the while loop continues until a defined condition is met. 1 >>> num = 1 2 >>> num 3 1 4 >>> while num <= 5: 5... print(num) 6... num += 1 7... 8 1 9 2 10 3 11 4 12 5 14

Functions are blocks of reusable code that perform a single task. You use def to define (or create) a new function then you call a function by adding parameters to the function name. 1 >> def multiply(num1, num2): 2... return num1 * num2 3... 4 >>> multiply(2, 2) 5 4 You can also set default values for parameters. 1 >>> def multiply(num1, num2=10): 2... return num1 * num2 3... 4 >>> multiply(2) 5 20 Ready to learn more? Visit to learn Python and web development. Cheers! 15