Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
25 Cards in this Set
- Front
- Back
How to get input from the keyboard to one program?
|
input( )
Example: >>> name = input('what is your name? ') |
|
Using function input to get numbers, what is the type of them once receive?
|
They turn to string.
|
|
What is the main advantage of triple quotes (''')?
|
Allows to write a string in multiple lines.
|
|
How to see the functions in a imported module?
|
import math
dir(math) # will show all the functions in this module. |
|
What is the error in code below and what is the fix for it?
>>> i = 0 >>> s = 'xyz' >>> while not (s[i] in 'aeiouAEIOU'): print(s[i]) i = i + 1 |
In the code above, the error occurs when s is indexed at i and i is outside of the range of valid indices. To prevent this error, add an additional condition is added to ensure that i is within the range of valid indices for s:
>>> i = 0 >>> s = 'xyz' >>> while i < len(s) and not (s[i] in 'aeiouAEIOU'): print(s[i]) i = i + 1 |
|
How to more generally refers to the end of the string s?
|
Use the function len(s) or let it empty [m:].Example:
can be represented using its length: s = 'Program' >>> s[:len(s)] 'Program' #or >>> s[2:] 'ogram' |
|
Given a string s, why the statment below is wrong?
>>> s[6] = 'd' Traceback (most recent call last): File <"pyshell#19", line 1, in s[6] = 'd' TypeError: 'str' object does not support item assignment |
The slicing and indexing operations do not modify the string that they act on, so the string that s refers to is unchanged by the operations above. In fact, we cannot change a string. Operations like the previous result in errors. To change a string s use:
>>> s = 'Learn to Program' >>> s = s[:5] + 'ed' + s[5:] >>> 'Learned to Program' |
|
How to convert a string s uppercase?
|
Use the method upper()
S.upper() -> string Return a copy of the string S converted to uppercase. >>> s 'Learned to Program' >>> s.upper() 'LEARNED TO PROGRAM' |
|
How to convert a uppercase string s to lowercase?
|
Use method s.lower()
>>> s = 'LEARNED TO PROGRAM' >>> s.lower() 'learned to program' |
|
How to count the occurrence of substring sub in a string s?
|
Use method s.count()
S.count(sub[, start[, end]]) -> int Return the number of non-overlapping occurrences of substring sub in string S[start:end]. Optional arguments start and end are interpreted as in slice notation. |
|
How to locate a substring inside a string?
|
Use method s.find()
S.find(sub [,start [,end]]) -> int Return the lowest index in S where substring sub is found, such that sub is contained within s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure. |
|
How is it called the special no-value value in python?
|
None
|
|
Is logical operators case sensitive?
|
Yes. True / False are not the same as true and false
|
|
What is the operand of floor division?
|
//
>>>3//2 >>> 1 |
|
What is the operand of modulo operation?
|
%
>>> 11 % 4 >>> 3 |
|
How to revert a string s using slicing?
|
s[ : : -1]
|
|
How to slice a string with a periodic pace?
|
Use a third parameter among the brackets. Example:
'MNKMDLVADVAEKTDLS'[16:0:-4] 'SKDD' # In this example it make the reverse slicing. |
|
what the function isinstance(...) does?
|
isinstance(object, class-or-type-or-tuple) -> bool
Return whether an object is an instance of a class or of a subclass thereof. With a type as second argument, return whether that is the object's type. Ex: >>> isinstance('hello', str) True >>> isinstance(7, int) True >>> isinstance(1.5, float) True |
|
What the call: string1.count(string2[, start[, end]]) returns?
|
Returns the number of times string2 appears in string1. If start is specified, starts
counting at that position in string1; if end is also specified, stops counting before that position in string1. |
|
What is return from the call:string1.find(string2[, start[, end]])
|
Returns the position of the first occurrence of string2 in string1; −1 means
string2 was not found in string1. If start is specified, starts searching at that position in string1; if end is also specified, stops searching before that position in string1. |
|
What is return from the call: string1.startswith(string2[, start[, end]])?
|
Returns True or False according to whether string2 starts with string1. If start is
specified, uses that as the position at which to start the comparison; if end is also specified, stops searching before that position in string1. |
|
What is return from the call: string1.strip([string2])?
|
Returns a string with all characters in string2 removed from its beginning and end;
if string2 is not specified, all whitespace is removed. |
|
What is return from the call: string1.lstrip([string2]) ?
|
Returns a string with all characters in string2 removed from its beginning; if
string2 is not specified, all whitespace is removed. |
|
What is returned from the call: string1.rstrip([string2])?
|
Returns a string with all characters in string2 removed from its end; if string2 is
not specified, all whitespace is removed. |
|
If you get errors about ASCII encodings, what can be done?
|
then put this at the top of your python scripts:
# -*- coding: utf-8 -*- |