Python basics

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

str

string/ text data

await keyword

used in asynchronous functions to specify a point in the function where control is given back to the event loop for other functions to run.

del statement

used to delete objects/variables.

Iterative Keywords

for, while, break, continue, else for: Using for loop, we can iterate any sequence or iterable variable. The sequence can be string, list, dictionary, set, or tuple. while: The while loop repeatedly executes a code block while a particular condition is true

Conditional Keywords

if, elif, else condition keywords act depending on whether a given condition is true or false

frozenset

immutable version of set create using class forzenset()

Import Keywords

import, from, as

Identity operators

is, is not

print(x[5:]

omitting the end_index prints list to end

print(x[:5]

omitting the start_index starts slice at 0

tuple

ordered collections of elements that are unchangeable tuples are immutable a tuple is a read-only version of the list. create a tuple 2 ways Enclosing elements in the parenthesis () Using a tuple() class.

Explicit casting

performed by the user using built-in functions.

pass keyword

performs no operation except to act as a placeholder for a required statement

strip()

removes any whitespace from the beginning or the end

replace()

replaces a string with another string

bytes

represents a group of byte numbers just like an array. bytes are immutable allowed values are 0 to 256

Returning Keywords

return, yield

is operator

returns Boolean True or False. It returns True if the memory address's first value is equal to the second value. Otherwise, it returns False

is not operator

returns boolean values either True or False. It returns True if the first value is not equal to the second value. Otherwise, it returns False.

factorial() math.factorial(x)

returns x factorial

floor()

round a number DOWN to the nearest integer value

ceil()

round the number UP to the nearest integer value

bytearray

same as the bytes type except bytearray mutable bytearray() constructor returns a bytearray object.

for statement

statement that directs program flow through a group of statements a fixed number of times

complex

store complex numbers (real and imaginary part)

python #

symbol to start writing a comment

python indentation

tells a Python interpreter that the group of statements belongs to a particular block of code. The indentation makes the code look neat, clean, and more readable.

try statement

the statement used to enclose code that may contain an exception.

Multi-Line Comments

there is no separate way to write a multi-line comment can use parentheses or \ to continue comments over multiple lines

End of python statement

token \n start new line

trunc()

truncate a number to its nearest integer

Exception-Handling Keywords

try, except, raise, finally, else, assert

if statement

a control flow statement that will execute statements under it if the condition is true. Also kown as a conditional statement.

yield keyword

a keyword that is used like return, except the function will return a generator

python variable

a reserved memory area (memory address) to store value.

code block

a series of statements that are grouped together

string

a set of characters represented in quotation marks

Local variable

a variable that is accessible inside a block of code only where it is declared If we declare a variable inside a method, the scope of the local variable is limited to the method only. So it is not accessible from outside of the method. If we try to access it, we will get an error.

+=

a+=5 Add 5 to a and assign it as a new value to a a = a+5

fabs(x)

absolute value of x

list

an ordered collection (also known as a sequence of elements. List elements can be accessed, iterated, and removed according to the order they inserted at the creation time. Lists are mutable The list can contain data of all data types such as int, float, string Duplicates elements are allowed in the list create a list two ways Enclosing elements in the square brackets []. Using a list() class.

set

an unordered collection of data items that are unique A set is a collection of elements (Or objects) that contains no duplicate elements. Set is mutable Heterogeneous (values of all data types) elements are allowed Insertion order of elements is not preserved, so we can't perform indexing on a Set create a set 2 ways By enclosing values in the curly brackets {} Using a set() class.

Operator Keywords

and, or, not, in, is logical and keyword returns True if both expressions are True logical or keyword returns a boolean True if one expression is true, and it returns False if both values are false logical not keyword returns boolean True if the expression is false

Asynchronous Programming Keywords

async, await

Implicit casting

automatic conversion of data to a different type

Single-line comments

begin with #, interpreter ignores anything written after the # sign, and it is effective till the end of the line.

Transfer Keywords

break, continue, pass transfer keywords are used to alter the program's way of execution in a certain manner

type() method

built-in Python method that checks variable type

break statement

causes an immediate exit of the loop

Inline Comments

concise comments on the same line as the code they describe

Python compound statements

contain (groups of) other statements; they affect or control the execution of those other statements in some way. The compound statement includes the conditional and loop statement.

radians()

convert the angle in degrees to radians

degrees()

convert the angle in radians to degrees

lower()

converts a string to lowercase

upper()

converts a string to uppercase

Structure Keywords

def, class, with, as, pass, lambda

Variable Handling Keywords

del, global, nonlocal

Docstring Comments

describe Python classes, functions, constructors, and methods. The docstring comment should appear just after the declaration. Conventions for writing good documentation strings are mentioned in PEP 257 Triple double quotation marks The docstring comment should appear just after the declaration. A docstring can be a single line or a multi-line comment. Docstring should begin with a capital letter and end with a period.

Variable Scope

dictates what portions of the code can "see" or use a variable, typically derived from where the variable was first created. (See Global v. Local)

4 types of Python statments

1. Print statements 2. Assignment Statements 3. Conditional Statements 4. Looping Statements

Separate multiple statements

;

int

A Python data type that holds positive and negative whole numbers

float

A native type representing rational numbers to limited precision.

return statement

A statement of the form return <expression> It is placed at the end of a function to return a value

continue statement

A statement that causes the remainder of the current iteration of a loop to be skipped. The flow of execution goes back to the top of the loop, evaluates the condition, and if this is true the next iteration of the loop will begin.

Global variable

A variable whose scope is "global" to the program, it can be used and updated by any part of the code. Its global scope is typically derived from the variable being declared (created) outside of any function, object, or method.

bool

A variable whose value can be either true or false is of this data type.

mutable variable

A variable whose value can change after it has been declared

immutable variable

A variable whose value cannot change after it has been declared

casting

Converting from one data type to another

membership operators

In and Not In

Not in operator

It returns True if the object is not present in a given sequence. Otherwise, it returns False

Membership Operator (IN)

It returns a result as True if it finds a given object in the sequence. Otherwise, it returns False.

class keyword

Keyword used to define a class; by default members are private.

tuple packing

Packing can be used when we want to collect multiple values in a single variable

Python keywords

Protected, special words (tells Python you are about to define a function) 36 keywords use keyword module or help() function to get list of keywords All the keywords except, True, False, and None, must be written in a lowercase alphabet symbol.

isinstance

The function call isinstance(ob,C) returns True if object ob is an instance of class C. This is different than testing the type of an object, as it will return True even if the type of ob is a subclass of C.

list slicing

Way to access elements list[start_index : end_index : step] -step = count by __'s -any term can be omitted, will be set to default - a negative step progresses through list backwards

Bitwise Operators

are used to manipulate the individual bits of values.

pass statement

The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action. Odd as it seems, pass has its uses: 1. It can be used to test a statement (say, opening a file or iterating through a collection) just to see if it works. You don't want to do anything as a result, just see if the statement is correct. 2. More interestingly, you can use pass as a place holder. You need to do something at this point, but you don't know what yet. You place a pass at that point and come back later to fill in the details.

while statements

The while loop statement repeatedly executes a code block while a particular condition is true. Also known as a looping statement.

Logical and

True if both the operands are True

Logical or

True if either of the operands is True

Logical not

True if the operand is False

Value Keyword

True, False, None

with statement

Used to cleanup code for a group of statements, while the with statement allows the execution of initialization and finalization code around a block of code.

return keyword

Used to return a value from a function

dict

unordered collections of unique values stored in (Key-Value) pairs. duplicate keys are not allowed, but the value can be duplicated. heterogeneous (i.e., str, list, tuple) elements are allowed for both key and value in a dictionary. But an object can only be a key in a dictionary if it is hashable. dictionary is mutable Dictionary is unordered so we can't perform indexing and slicing create a dictionary 2 ways Enclosing key:values in the curly brackets {} Using a dict() class.

nonlocal keyword

used in nested functions whose local scope is not defined. This means that the variable can be neither in the local nor the global scope.

Expression statements

used to compute and write a value. An expression statement evaluates the expression list and calculates the value. An expression is a combination of values, variables, and operators.

memoryview

used to create a view of the internal data of an object without copying it

global keyword

used to declare the global variable. A global variable is a variable that is defined outside of the method (block of code). That is accessible anywhere in the code file.

def keyword

used to define user-defined funtion or methods of a class

del keyword

used to delete the object.

import statement

used to import modules.

import keyword

used to import the whole module

Block Comments

used to provide descriptions of files, classes, and functions. Should add block comments at the beginning of each file and before each method.

with keyword

used when working with unmanaged resources (like file streams). It allows you to ensure that a resource is "cleaned up" when the code that uses it finishes running, even if exceptions are thrown.

async keyword

used with def to define an asynchronous function, or coroutine.

Implicit continuation

using parenthesis to write a multi-line statement


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

MKTG 3335 Chapters 11-14 TB Questions

View Set

Section 3: Computer Hardware and History

View Set

Chemistry: ChemQuest Skill Practice (Semester 1)

View Set

Adaptive Quizzing: Medication Administration (Chapter 32)

View Set