Python - Programming

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Nested If Statements

Although the indentation of the statements makes the structure apparent, nested conditionals become difficult to read very quickly. In general, it is a good idea to avoid them when you can. if x = = y: print(' x and y are equal') else: if x < y: print(' x is less than y') else: print(' x is greater than y')

initialize

An assignment that gives an initial value to a variable that will be updated.

semantic error

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

What is a bug?

An error in a program.

Print function

An instruction that causes the Python interpreter to display a value on the screen.

index

An integer value used to select an item in a sequence, such as a character in a string.

format operator

An operator, %, that takes a format string and a tuple and generates a string that includes the elements of the tuple formatted as specified by the format string.

decrement

An update that decreases the value of a variable.

increment

An update that increases the value of a variable (often by one).

Conditional statements

Give the ability to check conditions and change the behavior of the program accordingly. The simplest form is the if statement: example: if x > 0 : print(' x is positive')

If Logic

If the logical condition is true, then the indented statement gets executed. If the logical condition is false, the indented statement is skipped. if x% 2 = = 0 : print(' x is even') else : print(' x is odd')

The for and While loops are generally constructed by:

Initializing one or more variables before the loop starts

There are three logical operators

and, or, and not. Example: For example, x > 0 and x < 10

Operators

are special symbols that represent computations like addition and multiplication. The values the operator is applied to are called operands. The operators +, -, *, /, and ** perform addition, subtraction, multiplication, division, and exponentiation. To truncate the result of dividing two interger use use floored ( // integer).

iteration variable

call the variable that changes each time the loop executes and controls when the loop finishes

The underscore character ( _ )

can appear in a name. It is often used in names with multiple words, such as my_name or airspeed_of_unladen_swallow. Variable names can start with an underscore character, but we generally avoid doing this unless we are writing library code for others to use.

Variable names

can be arbitrarily long. They can contain both letters and numbers, but they cannot start with a number. It is legal to use uppercase letters, but it is a good idea to begin variable names with a lowercase letter

"try / except" conditional execution structure is built into Python to

handle unexpected. The idea of try and except is that you know that some sequence of instruction( s) may have a problem and you want to add some statements to be executed if an error occurs. inp = input(' Enter Fahrenheit Temperature:') try: fahr = float( inp) cel = (fahr - 32.0) * 5.0 / 9.0 print( cel) except: print(' Please enter a number')

An expression

is a combination of values, variables, and operators.

What is a string

is a sequence of characters.

A statement

is a unit of code that the Python interpreter can execute.

What is an item

is one of the values in a sequence.

What is Debugging?

is the process of finding the cause of the error in your code.

return value

is the result of a function that is returned by the function when called

What is call the pattern of processing the start of a string at the beginning, select each character in turn, do something to it, and continue until the end

it is called a traversal.

What is the expression in the brackets: fruit[ 1]

it is called an Index

How do you call a method of a function?

it is similar to calling a function (it takes arguments and returns a value) but the syntax is different. example of the "upper method": word.upper(). The empty parentheses indicate that it takes no arguments.

what does the Python function "dir" does?

it lists the methods available for an object. The "type" function shows the type of an object and the "dir" function shows the available methods.

Main memory

it loses its information when the power is turned off.

what does immutable means?

it means you can't change an existing string. The best you can do is create a new string that is a variation on the original.

what does the Python function "help" does?

it provides information about the function and its methods

the not operator

negates a boolean expression, so not (1 > 3) is true

What does the "in" operator does?

takes two strings and returns True if the first appears as a substring in the second: example: "a" in "Banana" returns True.

What does the format "%" operator allow us to do?

the format operator "%" allows us to construct strings, replacing parts of the strings with the data stored in variables. When applied to integers, "%" is the modulus operator. But when the first operand is a string, "%" is the format operator. example: camels = 42 type: 'I have spotted %d camels.' % camels result: 'I have spotted 42 camels.' "%d" format an integer, "%g" to format a floating-point number and "%s" to format a string. help files: https:// docs.python.org/ library/ stdtypes.html# printf-style-string-formatting.

operator = =

which compares two operands and produces True if they are equal and False

The modulus operator

works on integers and yields the remainder when the first operand is divided by the second. the operator is a percent sign (%).

How do you access the last index character of a string?

you access by typing: len(fruit) -1 or fruit[-1]

invocation

A statement that calls a method.

function definition

A statement that creates a new function, specifying its name, parameters, and the statements it executes.

function call

A statement that executes a function. It consists of the function name followed by an argument list.

import statement

A statement that reads a module file and creates a module object.

empty string

A string with no characters and length 0, represented by two quotation marks.

format string

A string, used with the format operator, that contains format sequences.

module object

A value created by an import statement that provides access to the data and code defined in a module.

argument

A value provided to a function when the function is called. This value is assigned to the corresponding parameter in the function.

accumulator

A variable used in a loop to add up or accumulate a result.

counter

A variable used in a loop to count the number of times something happened. We initialize a counter to zero and then increment the counter each time we want to "count" something.

Interactive mode

A way of using the Python interpreter by typing commands and expressions at the prompt.

Description of the if statement:

The boolean expression after the if statement is called the condition. We end the if statement with a colon character (:) and the line( s) after the if statement are indented.

central processing unit

The heart of any computer. It is what runs the software that we write; also called "CPU" or "the processor".

Machine code

The lowest-level language for software, which is the language that is directly executed by the central processing unit (CPU).

Semantics

The meaning of a program.

function object

The name of the function is a variable that refers to a function object. header The first line of a function definition.

flow of execution

The order in which statements are executed during a program run.

problem solving

The process of formulating a problem, finding a solution, and expressing the solution.

body

The sequence of statements inside a function definition.

dot notation

The syntax for calling a function in another module by specifying the module name followed by a dot (period) and the function name.

If-Then-ElseIf Logic

There is no limit on the number of elif statements. If there is an else clause, it has to be at the end, but there doesn't have to be one. if choice = = 'a': print(' Bad guess') elif choice = = 'b': print(' Good guess') elif choice = = 'c': print(' Close, but not correct')

Parse

To examine a program and analyze the syntactic structure.

Interpret

To execute a program in a high-level language by translating it one line at a time.

Compile:

To translate a program written in a high-level language into a low-level language all at once, in preparation for later execution.

what is a class bool

True and False are special values that belong to the class bool they are not strings:

composition

Using an expression as part of a larger expression, or a statement as part of a larger statement.

Prompt

When a program displays a message and pauses for the user to type some input to the program.

short-circuiting the evaluation is

When the evaluation of a logical expression stops because the overall value is already known.

what is the guardian pattern?

Where we construct a logical expression with additional comparisons to take advantage of the short-circuit behavior.

fruitful function

A function that returns a value.

algorithm

A general process for solving a category of problems.

infinite loop

A loop in which the terminating condition is never satisfied or for which there is no terminating condition.

parameter

A name used inside a function to refer to the value passed as an argument.

function

A named sequence of statements that performs some useful operation. Functions may or may not take arguments and may or may not produce a result.

search

A pattern of traversal that stops when it finds what it is looking for.

source code

A program in a high-level language.

High-level language

A programming language like Python that is designed to be easy for humans to read and write.

Low-level language

A programming language that is designed to be easy for a computer to execute; also called "machine code" or "assembly language".

Portability

A property of a program that can run on more than one kind of computer.

format sequence

A sequence of characters in a format string, like %d, that specifies how a value should be formatted.

Program

A set of instructions that specifies a computation.

what Python reserve Keywords?

33

The other Boolean comparison are

== equal != not equal > greater < less >= greater or equal <= less or equal "is" - is the same as "is not" - is not the same as

what is a flag

A boolean variable used to indicate whether a condition is true or false.

void function

A function that does not return a value.

method

A function that is associated with an object and called using dot notation.

What are some of the conceptual Patterns to construct a program?

Input: Get data from the "outside world". This might be reading data from a file, or even some kind of sensor like a microphone or GPS. In our initial programs, our input will come from the user typing data on the keyboard. Output: Display the results of the program on a screen or store them in a file or perhaps write them to a device like a speaker to play music or speak text. Sequential execution: Perform statements one after another in the order they are encountered in the script. Conditional execution Check for certain conditions and then execute or skip a sequence of statements. Repeated execution Perform some set of statements repeatedly, usually with some variation. Reuse Write a set of instructions once and give them a name and then reuse those instructions as needed throughout your program.

How is a method call refer?

It is called an invocation; for example: word.upper() so in this case, we would say that we are invoking upper on the word.

item

One of the values in a sequence.

deterministic

Pertaining to a program that does the same thing each time it runs, given the same inputs.

pseudorandom

Pertaining to a sequence of numbers that appear to be random, but are generated by a deterministic program.

What can you do when you are debugging a program?

Reading: Examine your code, read it back to yourself, and check that it says what you meant to say. running Experiment by making changes and running different versions. Often if you display the right thing at the right place in the program, the problem becomes obvious, but sometimes you have to spend some time to build scaffolding. ruminating Take some time to think! What kind of error is it: syntax, runtime, semantic? What information can you get from the error messages, or from theoutput of the program? What kind of error could cause the problem you're seeing? What did you change last, before the problem appeared? retreating At some point, the best thing to do is back off, undoing recent changes, until you get back to a program that works and that you understand. Then you can start rebuilding.

iteration

Repeated execution of a set of statements using either a function that calls itself or a loop.

What are Python module?

Simply, is a file consisting of Python code. it can define functions, classes and variables and can also include runnable code.

If-Then-Else Logic

Since the condition must either be true or false, exactly one of the alternatives will be executed. The alternatives are called branches, because they are branches in the flow of execution.

object

Something a variable can refer to. For now, you can use "object" and "value" interchangeably.

What is the function of the secondary memory in a computer?

Store information for the long term, even beyond a power cycle

Secondary memory

Stores programs and data and retains its information even when the power is turned off. Generally slower than main memory. Examples of secondary memory include disk drives and flash memory in USB sticks.

What 3 general errors will you encounter in Python?

Syntax errors: These are the first errors you will make and the easiest to fix. A syntax error means that you have violated the "grammar" rules of Python. does its best to point right at the line and character where it noticed it was confusing. The only tricky bit of syntax errors is that sometimes the mistake that needs fixing is actually earlier in the program than where Python noticed it was confusing. So the line and character that Python indicates in a syntax error may just be a starting point for your investigation. Logic errors A logic error is when your program has good syntax but there is a mistake in the order of the statements or perhaps a mistake in how the statements relate to one another. A good example of a logic error might be, "take a drink from your water bottle, put it in your backpack, walk to the library, and then put the top back on the bottle." Semantic errors A semantic error is when your description of the steps to take is syntactically perfect and in the right order, but there is simply a mistake in the program. The program is perfectly correct but it does not do what you intended for it to do. A simple example would be if you were giving a person directions to a restaurant and said, "... when you reach the intersection with the gas station, turn left and go one mile and the restaurant is a red building on your left." Your friend is very late and calls you to tell you that they are on a farm and walking around behind a barn, with no sign of a restaurant. Then you say "did you turn left or right at the gas station?" and they say, "I followed your directions perfectly, I have them written down, it says turn left and go one mile at the gas station." Then you say, "I am very sorry, because while my instructions were syntactically correct, they sadly contained a small but undetected semantic error.".

Python

follows mathematical convention. The acronym PEMDAS

A Boolean

expression is an expression that is either true or false.


संबंधित स्टडी सेट्स

Chapter 10 Social Class in the United States

View Set

Physiology - Ch. 5 Membrane Dynamics

View Set

Math Exam 2 (2.1,2.2,12.1,2.3,2.4,3.1,3.2)

View Set

Quiz Questions and in class questions: Research Methods I

View Set

Oceanography: Biogenic Sediments

View Set