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.