Python Flashcards
Comments In python?
hint: caveat?
they begin with #
This statement is ignored by the interpreter and serves as documentation for our code.
is used for single-line comments in Python
””” this is a comment “”” is used for multi-line comments, not ignored by the interpreter, these are called docstrings.
Imperative programming:
In computer science, imperative programming is a programming paradigm of software that uses statements that change a program’s state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program consists of commands for the computer to perform
Python features:
6
- Dynamically types: 1.No need to declare anything. An assignment statement binds a name to an object, and the object can be of any type.2.No type casting is required when using container objects
- Interpreted: bytecode then into machine lang.
- Uses indentation to structure code.
- Platform Independent.
- High-level lang: No need to take care of memory used by program, etc.
- Embeddable. It can be used within C/C++.
Python 2 VS Python 3
- / in 2 gives int and 3 gives float.
- print in 2 is keyword and 3 is function.
print ‘Py 2’ and print(‘python 3’)
…….
input()
how to get int iutput?
how to ask the user to give certain input?
input ( ):
This function first takes the input from the user and converts it into a string.
The type of the returned object always will be string.
It does not evaluate the expression it just returns the complete statement as String.
var = input(“Text to display on output screen”)
to get input:
var = int(input()) # typecast
What is Console in Python?
Console (also called Shell) is basically a command-line interpreter that takes input from the user i.e one command at a time and interprets it.
how do we take multiple inputs from the user?
Using split() method Using List comprehension: yet to explore
split()
x,y = input().split(separator, maxsplit)
- default separator is white space.
- it breaks the string by the specified separator.
print()
attributes? (5)
Syntax: print(value(s), sep= ‘ ‘, end = ‘\n’, file=file, flush=flush)
Parameters:
value(s): Any value, and as many as you like. Will be converted to a string before printed
sep=’separator’ : (Optional) Specify how to separate the objects, if there is more than one.Default:’ ‘
end=’end’: (Optional) Specify what to print at the end.Default: ‘\n’
file : (Optional) An object with a write method. Default:sys.stdout
flush : (Optional) A Boolean, specifying if the output is flushed (True) or buffered (False). Default: False
Returns: It returns output to the screen.
flush argument of print:
If false, it is buffered. if true it is flushed.
The I/Os in python are generally buffered, meaning they are used in chunks.
If it is set to true, the output will be written as a sequence of characters one after the other. This process is slow simply because it is easier to write in chunks rather than writing one character at a time.
Print a list without newline:
# using * symbol prints the list # elements in a single line print(*list_name)
Example:
>>> list = [10,20,30,40] >>> print(list) [10, 20, 30, 40] >>> print(*list) 10 20 30 40
Character in python?
No such data type in python.
It is simply a string with length 1.
String in python?
how to create them?
how to access them?
indexing type here?
- an array of bytes representing Unicode characters.
- single, double or triple quotes to create them.
- triple quotes allow multi-lines.
- access using indexing, starts from 0.
- negative indexing is also there: -1 refers to the last character.
String slicing?
- to access a range of characters.
- colon : is the slicing operator.
- string[3:12] //12th not included. 3rd included.
Updation and deletion
string
Strings are immutable. a character can’t be changed or deleted.
the entire string can be deleted.
> > > del string_name
and we can reassign a new string to the same one.
Escape Sequencing in python:
- triple quotes allow the use of double and single strings.
- the backlash is used to escape.
' will print ‘
\ will print “
format():
Format method in String contains curly braces {} as placeholders which can hold arguments according to position or keyword to specify the order.
# Python Program for # Formatting of Strings
# Default order String1 = "{} {} {}".format('Geeks', 'For', 'Life') print("Print String in default order: ") print(String1)
# Positional Formatting String1 = "{1} {0} {2}".format('Geeks', 'For', 'Life') print("\nPrint String in Positional order: ") print(String1)
# Keyword Formatting String1 = "{l} {f} {g}".format(g='Geeks', f='For', l='Life') print("\nPrint String in order of Keywords: ") print(String1)
justify a string?
or operator %
- use of for left, center, right justification respectively.
A string can be left(
Size of list?
len(List)
How to add elements to the desired position in the list?
list.insert(position,val)
list also allows negative indexing.
Add more than one element at the end of the list
list.extend([val1,val2,val3]}
delete an element from list:
2 ways:
- list.remove(element_value)
removes only one element, returns error if element not in list. - list.pop() removes and returns last element.
list. pop(index_position) removes and returns element at that position.
Slicing of the list:
to print in reverse order: list[::-1]
in range: [start index: end index]
whole list: list[:]
to print from last:
[:-6] use negative index then beginning becomes last.
List comprehension:
List comprehensions are used for creating new lists from other iterables like tuples, strings, arrays, lists, etc.
A list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element.
newList = [ expression(element) for element in oldList if condition ]