Ch. 3 Functions (Python3)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Common Conversion specifiers

%d substitute as integer %f substitute as floating-point decimal %s substitute as string %x substitute as hexadecimal in lowercase %X substitute as hexadecimal in uppercase %e substitute as floating-point exponential format in lowercase %E substitute as floating-point exponential format in uppercase

String Concatenation

A program can add new characters to the end of a string in a process known as string concatenation. The expression "New" + "York" concatenates the strings New and York to create a new string NewYork.

Sequence Type

A sequence type is a type that specifies a collection of objects ordered from left to right. A string's characters are ordered from the first letter of the string to the last.

String Literal

A string value specified in the source code of a program. A programmer creates a string literal by surrounding text with single or double quotes, such as 'MARY' or "MARY".

Type conversion

A type conversion is a conversion of one type to another, such as an int to a float.

Dictionary container

Associative relationships can be described in Python using a container called a dictionary. A dictionary is represented by the dict object type. A dict maps unique keys to values. A key is like the word "cat" from the English dictionary example. A value describes some data associated with a key, such as a definition. A key can be any immutable type, such as a number, string, or tuple; a value can be any type. A programmer creates a dict using braces { } to surround the key:value pairs that comprise the dictionary contents. For example, { 'Lionel Messi': 10, 'Christiano Ronaldo': 7} describes a dictionary with two keys: the football players 'Lionel Messi' and 'Christiano Ronaldo', associated with the values 10 and 7 (their respective jersey numbers). An empty dictionary is created with the expression { }.

Tuple

Behaves similar to a list but is immutable - once created the tuple's elements can not be changed. A tuple is also a sequence type, supporting len(), indexing, and other sequence type functions. A new tuple is generated by creating a list of comma-separated values, such as 5, 15, 20. Typically, tuples are surrounded with parentheses, as in (5, 15, 20). Note that printing a tuple always displays surrounding parentheses. A tuple is not as common as a list in practical usage, but can be useful when a programmer wants to ensure that values do not change. For example, a tuple might store the latitude and longitude of a landmark because a programmer knows that the first element should be the latitude, the second element should be the longitude, and the landmark will never move from those coordinates.

Simple example of converting float and int types

Converting a float to an int will truncate the floating-point number's fraction. For example, the variable temperature might have a value of 18.75232, but can be converted to an integer expression int(temperature). The result would have the value 18, with the fractional part removed.

len()

Finds the number of elements in a sequence. A built-in function can be used to find the length of a string (and any other sequence type).

append() pop () remove()

Lists are mutable, meaning that a programmer can add and remove elements from a list as needed. New elements are added to a list using the append() list method. Elements can be removed using the pop() or remove() methods. A method instructs an object to perform some action, and is executed by specifying the method name following a "." symbol and an object. For example, my_list.append('abc') adds 'abc' to my_list.

List container

One such container is a list, created by surrounding a sequence of variables or literals with brackets [ ]. For example, the statement my_list = [10, 'abc'] creates a new list variable my_list that contains the two items 10 and 'abc'. A list item is often called an element of that list. Lists are mutable, meaning that a programmer can add and remove elements from a list as needed. Note that lists can contain mixed types of objects. For example, x = [1, 2.5, 'abc'] creates a new list x that contains an integer, a floating-point number, and a string.

Containers

Programmers commonly desire to group related variables together. Containers, which contain references to other objects instead of data, are able to accomplish this. One such container is a list.

Accessing individual characters of a sequence

Programs commonly access an individual character of a string. As a sequence type, every character in a string has an index, or position, starting at 0 from the leftmost character. For example, the 'A' in string 'ABC' is at index 0, 'B' is at index 1, and 'C' is at index 2. A programmer can access a character at a specific index by appending brackets [ ] containing the index:

Sequence type functions

Sequence-type functions are built-in functions that operate on sequences like lists and strings. A subset of such functions is provided above definition (functions useful to lists).

String formatting expressions

String formatting expressions allow a programmer to specify a string that has placeholders for variables' values.

String

Strings are a fundamental part of almost any program, and are useful for displaying information to users and recording data, among other things.

Implicit conversion

The interpreter automatically performs common conversions between numeric types like int and float. Such automatic conversion is known as implicit conversion.

Conversion specifiers

The placeholders, known as conversion specifiers, define the type of value being printed. A conversion specifier begins with the % character, followed by a specific sequence of characters that indicate the type of value to be printed. The string literal is followed by another % character and the variable to substitute. The %d conversion specifier indicates that the substituted value is an integer. The % indicating the start of the conversion specifier, and the d indicating integer. Similarly, %f specifies a float, and %s a string.

Index

The position of a character in a string is called the character's index.

Common Data Types

int float string list tuple dict Numeric types int and float represent the most common types used to store data. All numeric types support the normal mathematical operations such as addition, subtraction, multiplication, and division, among others. Sequence types string, list, and tuple are all containers for collections of objects ordered by position in the sequence, where the first object has an index of 0 and subsequent elements have indices 1, 2, etc. The only Mapping type in Python is the dict type. Like a sequence type, a dict serves as a container. However, each element of a dict is independent, having no special ordering or relation to other elements. A dictionary uses key-value pairs to associate a key with a value.

Functions useful to Lists

len(list) list1 + list2 min(list) max(list) sum(list) list.index(val) list.count(val)

Example of Common Conversion specifiers

participation example:

Binary

x


Set pelajaran terkait

Chapter 14 Building Positive Employee Relations

View Set

Chapter 6: Human Behavior and Organizational Environment

View Set

PEDS Exam #1 Ch. 23: The Child with Respiratory Dysfunction

View Set

Test 3 investments CH. - 7,8,9,&10

View Set

Old Testament History and Literature

View Set