Python

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Semantic error

An error in a program that makes it do something other than what the programmer intended.

Syntax error

An error in a program that makes it impossible to parse (and therefore impossible to interpret).

Expression

An expression is a combination of numbers, symbols or other variables that produce a result when evaluated.

Implicit vs Explicit Conversion

As we saw earlier in the video, some data types can be mixed and matched due to implicit conversion. Implicit conversion is where the interpreter helps us out and automatically converts one data type into another, without having to explicitly tell it to do so. By contrast, explicit conversion is where we manually convert from one data type to another by calling the relevant function for the data type we want to convert to. We used this in our video example when we wanted to print a number alongside some text. Before we could do that, we needed to call the str() function to convert the number into a string. Once the number was explicitly converted to a string, we could join it with the rest of our textual string and print the result.

Data Types Recap

In Python, text in between quotes -- either single or double quotes -- is a string data type. An integer is a whole number, without a fraction, while a float is a real number that can contain a fractional part. For example, 1, 7, 342 are all integers, while 5.3, 3.14159 and 6.0 are all floats. When attempting to mix incompatible data types, you may encounter a TypeError. You can always check the data type of something using the type() function.

Functions

Pieces of code that perform a unit of work (eg. print())

Returning Values Using Functions

Sometimes we don't want a function to simply run and finish. We may want a function to manipulate data we passed it and then return the result to us. This is where the concept of return values comes in handy. We use the return keyword in a function, which tells the function to pass data back. When we call the function, we can store the returned value in a variable. Return values allow our functions to be more flexible and powerful, so they can be reused and called multiple times. Functions can even return multiple values. Just don't forget to store all returned values in variables! You could also have a function return nothing, in which case the function simply exits.

Implicit conversion

The interpreter automatically converts one data type into another.

Variables

Variables are names that we give to certain values in our programs. Think a container for data. Variables are Case Sensitive.

Defining Functions Recap

We looked at a few examples of built-in functions in Python, but being able to define your own functions is incredibly powerful. We start a function definition with the def keyword, followed by the name we want to give our function. After the name, we have the parameters, also called arguments, for the function enclosed in parentheses. A function can have no parameters, or it can have multiple parameters. Parameters allow us to call a function and pass it data, with the data being available inside the function as variables with the same name as the parameters. Lastly, we put a colon at the end of the line. After the colon, the function body starts. It's important to note that in Python the function body is delimited by indentation. This means that all code indented to the right following a function definition is part of the function body. The first line that's no longer indented is the boundary of the function body. It's up to you how many spaces you use when indenting -- just make sure to be consistent. So if you choose to indent with four spaces, you need to use four spaces everywhere in your code.

Exception

An error that is detected while the program is running.

None

None is a very special data type in Python used to indicate that things are empty or that they return nothing.

Self-documenting code

Self-documenting code is written in a way that's readable and doesn't conceal its intent. In programming lingo, when we re-write code to be more self-documenting, we call this process refactoring.

Semantics

The meaning of a program.

Assignment

The process of storing a value inside a variable is called assignment

Syntax

The structure of a program.

Keywords

Rreserved words that are used to construct instructions. (eg. for, if, while)

Explicit conversion

eg. str(int or float)


Kaugnay na mga set ng pag-aaral

ARDS, RSI, & ABGs: Practice Questions

View Set

CH 7 Business Strategy: Innovation, Entrepreneurship, and Platforms

View Set

COMM 120 WK 4 MLA Citations Lesson & Quiz

View Set

Org Behaviors Exam 2 Study Guide

View Set

07.11 Natural Selection Module Exam

View Set

Medical Surgical Nursing Chapter 34 Coronary Artery Disease and Acute Coronary Syndrome

View Set