Docsity

Python Review 1 - Introduction to Computing - Slides | CS 1301, Study notes of Computer Science

Partial preview of the text

Download Python Review 1 - Introduction to Computing - Slides | CS 1301 and more Study notes Computer Science in PDF only on Docsity!

Jay Summet CS 1803

Python Review 1

Outline Introduction to Python Operators & Expressions Data Types & Type Conversion Variables: Names for data Functions Program Flow (Branching Input from the user Iteration (Looping

Operators Python has many operators. Some examples are: +, -, *, /, %, >, <, == print Operators perform an action on one or more operands. Some operators accept operands before and after themselves: operand1 + operand2, or 3 + 5 Others are followed by one or more operands until the end of the line, such as: print “Hi!”, 32, 48 When operators are evaluated, they perform action on their operands, and produce a new value.

Example Expression Evaluations An expression is any set of values and operators that will produce a new value when evaluated. Here are some examples, along with the new value they produce when evaluated: 5 + 10 produces 15 “Hi” + “ “ + “Jay!” produces “Hi Jay!” 10 / (2+3) produces 2 10 > 5 produces True 10 < 5 produces False 10 / 3.5 produces 2. 10 // 3 produces 3 10 % 3 produces 1

DANGER! Operator Overloading! NOTE! Some operators will work in a different way depending upon what their operands are. For example, when you add two numbers you get the expected result: 3

  • 3 produces 6. But if you “add” two or more strings, the + operator produces a concatenated version of the strings: “Hi” + “Jay” produces “HiJay” Multiplying strings by a number repeats the string! “Hi Jay” * 3 produces “Hi JayHi JayHiJay” The % sign also works differently with strings: “test %f” % 34 produces “test 34”

Data Types In Python, all data has an associated data “Type”. You can find the “Type” of any piece of data by using the type( function: type( “Hi!”) produces <type 'str'> type( True ) produces <type 'bool'> type( 5) produces <type 'int'> type(5.0) produces <type 'float'> Note that python supports two different types of numbers, Integers (int and Floating point numbers (float. Floating Point numbers have a fractional part (digits after the decimal place, while Integers do not!

Simple Data types in Python The simple data types in Python are: Numbers int – Integer: -5, 10, 77 float – Floating Point numbers: 3.1457, 0. bool – Booleans (True or False Strings are a more complicated data type (called Sequences that we will discuss more later. They are made up of individual letters (strings of length 1

Type Conversion Data can sometimes be converted from one type to another. For example, the string “3.0” is equivalent to the floating point number 3.0, which is equivalent to the integer number 3 Functions exist which will take data in one type and return data in another type. int() - Converts compatible data into an integer. This function will truncate floating point numbers float() - Converts compatible data into a float. str() - Converts compatible data into a string. Examples: int(3.3) produces 3 str(3.3) produces “3.3” float(3) produces 3.0 float(“3.5”) produces 3. int(“7”) produces 7 int(“7.1”) throws an ERROR! float(“Test”) Throws an ERROR!

Variables When a variable is evaluated, it produces the value of the data that it points to. For example: myVariable = 5 myVariable produces 5 myVariable + 10 produces 15 You MUST assign something to a variable (to create the variable name before you try to use (evaluate it.

Program Example Find the area of a circle given the radius: Radius = 10 pi = 3. area = pi * Radius * Radius print( area ) will print 314.15 to the screen.

How to use a function You can cause a function to execute by “calling” it as follows: functionName( Parameters) You can optionally assign any result that the function returns to a variable using the assignment operator: returnResult = functionName(Parameters)

Indentation is IMPORTANT! A function is made up of two main parts, the Header, and the Body. The function header consists of: def funcName(param1,param2): def keyword function name zero or more parameters, comma separated, inside of parenthesis ( A colon : The function body consists of all statements in the block that directly follows the header. A block is made up of statements that are at the same indentation level.

findArea function, with syntax error! def findArea( ): Radius = 10 pi = 3. area = pi * Radius * Radius print(area) You can NOT mix indentation levels within the same block! The above code will result in a syntax error!

What's wrong with findArea – Limited Applicability def findArea( ): Radius = 10 pi = 3. area = pi * Radius * Radius print(area) It will only work for circles of size 10! We need to make this function more general! Step 1: Use parameters to accept the radius of any sized circle!