Python ch5

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

What are assignment operators?

Assigns or changes the value of the variable on its left = += (x+=y, x=x+y) -= *= /= %= //= **=

What is the precedence of operators in python?

BIDMAS Brackets Indices -complement, unary + and unary- Multiply, divide, modulo, floor division Add, subtract Relational operators Equality operators == != Assignment operators Identity operators Membership operators Logical operators -for operators with equal precedence expression evaluated from left to right

What are relational operators?

Compare values of operands on either side of it to determine a relation among them. == != < > <= >=

What classes do number data types have?

Int Boolean Floating point Complex (iota)

What is the difference between immutable and mutable data types?

Mutable- variables whose value can be changed after they are created and assigned. Immutable- variable whose value cannot be changed after they are created and assigned.

Draw a mind map for all the data types:

Onenote.

What are the classes in sequence data type?

Strings Lists Tuples

What happens if you update the value of an immutable variable?

The old variable is destroyed and a new variable is created by the same name in memory.

What are comments?

#comment Non executable statements in a program, used to add a remark/note in the source code. Used to make source code easier for humans to understand. Used to document the meaning and purpose of the source code, it's input and output requirements and how it functions and how to use it.

What is the Boolean data type?

(bool) Subtype of integer Stores true and false values True value is non zero False value is zero

What are arithmetic operators?

+ - * / % // ** // aka floor division or integer division

What are the features of python?

1. High level lang 2. Interpreted lang (uses interpreter) 3. Easy to understand programs as syntax is clearly defined and has relatively simple structure 4. Case-sensitive 5. Portable and platform independent, means it can run on various operating systems and hardware platforms. 6. Rich library of predefined functions 7. Helpful in web development. Many web services and apps are made with python 8. Uses indentation for blocks and nested blocks

What are the data types?

1. Integer 2. Boolean 3. Float 4. Complex 5. String 6. List 7. Tuples 8. Sets 9. None 10. Dictionary

What are the rules for naming an identifier?

1. Name should begin with an uppercase or lowercase alphabet or underscore sign. 2. Can be of any length 3. Name cannot start with a number 4. Can have only alphanumeric characters and underscore 5. It should not be a keyword or reserved word 6. Cannot use special symbols in identifier names

What does >>> symbol indicate?

>>> is the python prompt which indicates that the interpreter is ready to take instructions. We can type commands or statements to execute them using a python interpreter. Eg Num1 = 12 >>>num1 12

What is an expression?

A combination of constants, variables and operators. An expression always evaluates to a value. A value or a stand-alone expression can be considered an expression but a standalone operator is not an expression.

What are Strings within Datatypes?

A group of characters including alphabets, numbers, or special characters including spaces. String values are enclosed in single quotation or double quotation marks. Cannot perform numerical operations on strings even if string has numeric value.

What is a programming language?

A language used to specify the set of instructions used in a program to the computer.

What is none?

A special data type with a single value, used to signify the absence of a value in a situation. It supports no special operations and is neither false or true.

What is an assignment statement?

A statement that sets a variable to a specified value. A = "awesome" B = 'b' Count = 5

What is a statement?

A unit of code that the python interpreter can execute.

What is an identifier?

A user defined name given to a variable, function or a constant in a program.

What is a variable?

A variable in a program is uniquely identified by a name (identifier). Variable in python refers to an object, item or element stored in the memory. The value of a variable can be string, numeric or any combination of alphanumeric characters. Wherever a variable name occurs in an expression the interpreter replaces it with the value of that particular variable.

What is the Python shell?

A window in which Python statements and expressions are executed immediately

What are 2 ways to use the python interpreter?

A) interactive mode B) script mode

What is an object in python?

All values, data types are treated as objects that can be assigned to some variable or can be passed to a function as an argument. Every object is assigned a unique identity ID which stays the same for lifetime of the object. This ID is like the memory address of the object Id() function returns the identity of the object

What is script mode?

Allows us to write more than 1 instruction in a file called python source code file that can be executed. Can write, save and use the interpreter to execute the file. Extension of file .py and files are saved in the python installation folder

What is idle?

An integrated development environment for Python.

What is a python sequence?

An ordered collection of items where each item is indexed by an integer.

What is a program?

An ordered set of instructions to be executed by a computer to carry out a specific task.

What are sets in data types?

An unordered collection of items separated by commas and the items are enclosed in curly brackets. A set is similar to a list but it cannot have duplicate entries. Once created elements of a set cannot be changed.

What are logical operators?

And, or, not The logical operator evaluates to either true or false By default all values are true except none, false, 0, empty collections, "", (),[],{}.

What are runtime errors?

Causes abnormal termination of program whilst executing. Statement is syntactically correct but the interpreter cannot execute it.

What is machine language?

Consists of strictly just 0s and 1s and is known as low level language.

What are operators?

Constructs used to perform specific mathematical calculations or logical operations on values. The values that operators work on are called operands. Operands may be unary or binary.

What are some python keywords?

Continue Is Lambda Try Return From Non local Del Global With Yield Assert Import Pass Except In Raise

What is a data type?

Data type identifies the type of data values a variable can hold and the operations that can be performed on that data.

What are syntax errors?

Errors that occur when the rules of the programming language are not followed.

What is the difference between explicit and implicit type conversion?

Explicit type conversion = type casting , takes place because the programmer forced it in the program. Risk of losing data since we are forcing an expression to be a specific type. Str (x) Chr (x) character Unichr (x) Unicode character Implicit type conversion aka coercion Happens when data type conversion is done automatically by interpreter. The interpreter converts data into a wider size of data to not lose info when possible, inbuilt in type promotion.

Why do we get errors in a program?

If you use a variable that has not been assigned a value in an expression. If you make: 1. Syntax errors 2. Logical errors 3. Runtime errors

What is ide?

Integrated Development Environment used for programming languages.

What are identity operators?

Is, is not Used to determine whether the value of a variable is a certain type or not. Can be used to determine whether 2 values are referring to same object or not.

What is interactive mode in interpreter?

It allows execution of individual statements instantaneously. >>> used to prompt Interactive mode Press enter and statement is executed and output is displayed The mode is convenient for testing 1 line of code for instant execution but it does not save the statements for future use, have to retype statements (All statements executed on the interpreter itself)

What is python?

It is an open source, high level, interpreter based language that can be used for scientific and non-scientific computing programs.

How are variables declared in python?

It is implicit in python Variable are automatically declared and defined when they re assigned a value for the first time.

What is mutablitity and immutability?

It is the ability or inability to change the values once a variable of a specific data type has been created and assigned a value.

Does python use and interpreter or compiler?

It uses an Interpreter to convert its instructions into machine language.

Give some practical uses of python data types:

Lists- simple iterable (can be looped) collection of data that can be frequently modified. Tuples- used when we don't need any changes in data (name of months) Set- used when we need uniqueness of elements and to avoid repetition of data Dictionary- if stat is being constantly modified or need a fast lookup based on a custom key (mobile phone book)

What is mapping and dictionary?

Mapping- an unordered data type in python, only one mapping data type called dictionary. Dictionary- in python holds items in key:value pairs. Items are enclosed in curly brackets {}. Dictionaries permit faster access to data Every key is separated from its value using : The key:value pairs of a dictionary can be accessed using the key. The keys are usually strings and values can be any data type. To access any value we have to specify it's key in [ ]

What are the 6 types of operators?

Membership Identity Logical Arithmetic Assignment Relational

Give examples of mutable and immutable data types:

Mutable: Int Float Bool Complex Str Tuple1 Set1 Immutable: Dict1 List1

Explain immutability with an example:

Num1 = 200 Num2 = num1 Num1 = num2/10 >>>num1 = 20 Object 200 and object 20 have 2 different memory ids - here num1 is rebuilt to connect to another id telling us it's immutable (class =int)

What is debugging?

Process of identifying and removing errors from a computer program.

What are examples of high level programming languages?

Python, C++, Visual Basic, PHP, Java

Output: print('sanaz'+'hussain') and print('sanaz', hussain')

Sanazhussain Sanaz hussain

What are lists within data types?

Sequence of items separated by commas and items are enclosed in [ ].

What are tuples within data types?

Sequence of items separated by commas and items are enclosed in parentheses ( ) Once created we cannot change the Tuple.

What is concatenation?

The joining of two or more strings

What is type casting?

The temporary conversion of a value from one data type to another. =int(input())

What is the print() function used for?

To output data to a standard output device which is the screen. The function print() evaluates the expression before displaying it. It outputs a complete line and moves to the next line for subsequent output. Syntax for print:

What is the input() function used for?

To take user input, it prompts user to enter data. Accepts all user input as string even if numeric values are entered. Input ([prompt]) Prompt is the string that will be displayed on screen before taking input. Entering data is terminated by pressing enter.

Which data types can hold long lists of info?

Tuples, lists, dictionaries and sets.

How do you execute a script?

Type file name and path at prompt Click run from run module in menu

What is the difference between unary and binary operands?

Unary operators operate on 1 operand (eg. A=10 (operand), -(10)=-10.) Unary operators have higher precedence than binary operators. Binary operators operate in 2 operands (a+b, a**2, 8//6)

Is not a unary or binary logical operator?

Unary, acts on 1 operand.

What are membership operators?

Used to check if a value is a member of the given sequence or not. In, not in

What are logical errors/ semantic errors?

When meaning of program is incorrect Bug in program causing it to behave incorrectly, it produces an undesired output but without the interpreter stopping the execution. Only evidence of bug is wrong output. Work backwards from output to find problem

What is type conversion?

a conversion of one data type to another Can happen explicitly or implicitly


Kaugnay na mga set ng pag-aaral

N115: Week 2:: CNS Pharmacology Objectives and Study Questions

View Set

Quoting, Paraphrasing, Summarizing

View Set

A&P Chapter 5: Integumentary System

View Set

Chapter 15 (Darwin and Evolution)

View Set

Chapter 4 Notes: Prior Restraint

View Set

Chapter 48: Nursing Assessment: Endocrine System (lewis) Questions

View Set