Python Flashcards
Types of variables and how to print them.
int
float
str
bool
print(“Age is: {}”.format(age))
print(f”Age is: {age}”)
print(“Age is: “, age)
How to accept data from console?
age = input(“What is your age?”)
Type conversion.
age = input("What is your age?") future_age = int(age) + 5
String variables have which functions?
capitalize() count() index() isalpha() isdecimal() islower() join() lower() lstrip() replace() rindex() rstrip() title() upper() zfill()
Arithmatic operators
Comparison operators
Logical operators
+ - * / % // **
> < >+ <= == !=
and or not
Operator precedence
** Exponentiation (raise to the power)
~ + - Complement, unary plus and minus (method names for the last two are +@ and -@)
* / % // Multiply, divide, modulo and floor division
+ - Addition and subtraction
» « Right and left bitwise shift
& Bitwise ‘AND’td>
^ | Bitwise exclusive OR' and regular
OR’
<= < > >= Comparison operators
<> == != Equality operators
= %= /= //= -= += *= **= Assignment operators
is is not Identity operators
in not in Membership operators
not or and Logical operators
Condition
if age < 25: print("Young") elif age < 40: print("Very energetic") else: print("New young")
While loop
For loop
while count < 5:
print(count)
count += 1
for item in range(5):
print(“Item is {}”.format(item))
Lists
my_list = [“1”, “5”, “7”, “12”]
.append() .clear() .copy() .count() .extend() .index() .insert() .pop() .remove() .reverse() .sort()
Tuple
my_tuple = (1, 2, 3, 4)
Tuples are immutable
Interpreted or Compiled?
Python is interpreted language.
Print multiple *
print(‘*’ * 10)
Function in Python?
How are arguments passed?
def my_function(name): print("Hello {}".format(name))
my_function(“chini”) # Calling the function
Positional Arguments - based on order.
Keyword Arguments - based on keywords in arguments.
Sort a list.
my_list = [‘1’, ‘2’, ‘3’]
my_list.sort() #prints ascending order
my_list.sort(reverse=True) #prints descending order
my_list.sort(key=myFunc) #uses custom function to sort.
Dictionaries
As of Python 3.7, dictionaries are ordered.