Python Basics Flashcards

1
Q

Global Keyword - Inside a function

A

def myfunc():
global x
x = “fantastic”

myfunc()

print(“Python is “ + x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Global Keyword - Inside a function but variable declared outside

A

x = “awesome”

def myfunc():
global x
x = “fantastic”

myfunc()

print(“Python is “ + x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Python Built-in data types

A

Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Python Numbers

A

There are three numeric types in Python:

int
float
complex
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Random() module in python?

A

Code :
————————————————————–
import random

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Python Casting

A

printing string converting to tuple

code:
—————————————————————
strType = “CodingNinjas”

tupleType = tuple(strType)
print(“After converting string to tuple : “)
print(tupleType)
print(“Data type of the tupleType :”,type(tupleType))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Casting / Type conversion in python

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Type conversion in python

A

intType is converted from int to float

the Python interpreter automatically converts one data type to another. Since Python handles the implicit data type conversion, the programmer does not have to convert the data type into another type explicitly.

The data type to which the conversion happens is called the destination data type, and the data type from which the conversion happens is called the source data type.

In type conversion, the destination data of a smaller size is converted to the source data type of larger size. This avoids the loss of data and makes the conversion safe to use.

Code
——————————————————–
intType = 20
floatType = 0.8

result = intType + floatType

print(“datatype of intType:”, type(intType))

print(“datatype of floatType:”, type(floatType))

print(“intType + floatType = “, result)
# result is of type float
print(“result: “, type(result))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Python Slicing Strings

A

Slice From the Start
b = “Hello, World!”
print(b[:5])
———————————————————————————-
Slice To the End
b = “Hello, World!”
print(b[2:])
———————————————————————————-
Negative Indexing
b = “Hello, World!”
print(b[-5:-2])
Output : orl
———————————————————————————-

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Python - Modify Strings

A

upper() method
lower() method
strip() method removes any whitespace from the beginning or the end:
a = “ Hello, World! “
print(a.strip()) # returns “Hello, World!”
————————————————————————————
replace() method replaces a string with another string:
a = “Hello, World!”
print(a.replace(“H”, “J”))
————————————————————————————
split() method returns a list where the text between the specified separator becomes the list items.
Example

The split() method splits the string into substrings if it finds instances of the separator:
a = “Hello, World!”
print(a.split(“,”)) # returns [‘Hello’, ‘ World!’]
————————————————————————————

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Python - Escape Characters

A

An escape character is a backslash \ followed by the character you want to insert.
txt = “We are the so-called "Vikings" from the north.”

Other escape characters used in Python:
Code Result
' Single Quote
\ Backslash
\n New Line
\r Carriage Return
\t Tab
\b Backspace
\f Form Feed
\ooo Octal value
\xhh Hex value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Python String Methods

A

Method Description
capitalize() Converts the first character to upper case
casefold() Converts string into lower case
center() Returns a centered string
count() Returns the number of times a specified value occurs in a string
encode() Returns an encoded version of the string
endswith() Returns true if the string ends with the specified value
expandtabs() Sets the tab size of the string
find() Searches the string for a specified value and returns the position of where it was found
format() Formats specified values in a string
format_map() Formats specified values in a string
index() Searches the string for a specified value and returns the position of where it was found
isalnum() Returns True if all characters in the string are alphanumeric
isalpha() Returns True if all characters in the string are in the alphabet
isascii() Returns True if all characters in the string are ascii characters
isdecimal() Returns True if all characters in the string are decimals
isdigit() Returns True if all characters in the string are digits
isidentifier() Returns True if the string is an identifier
islower() Returns True if all characters in the string are lower case
isnumeric() Returns True if all characters in the string are numeric
isprintable() Returns True if all characters in the string are printable
isspace() Returns True if all characters in the string are whitespaces
istitle() Returns True if the string follows the rules of a title
isupper() Returns True if all characters in the string are upper case
join() Joins the elements of an iterable to the end of the string
ljust() Returns a left justified version of the string
lower() Converts a string into lower case
lstrip() Returns a left trim version of the string
maketrans() Returns a translation table to be used in translations
partition() Returns a tuple where the string is parted into three parts
replace() Returns a string where a specified value is replaced with a specified value
rfind() Searches the string for a specified value and returns the last position of where it was found
rindex() Searches the string for a specified value and returns the last position of where it was found
rjust() Returns a right justified version of the string
rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a list
rstrip() Returns a right trim version of the string
split() Splits the string at the specified separator, and returns a list
splitlines() Splits the string at line breaks and returns a list
startswith() Returns true if the string starts with the specified value
strip() Returns a trimmed version of the string
swapcase() Swaps cases, lower case becomes upper case and vice versa
title() Converts the first character of each word to upper case
translate() Returns a translated string
upper() Converts a string into upper case
zfill() Fills the string with a specified number of 0 values at the beginning

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Bitwise Operators - Python

A

& AND Sets each bit to 1 if both bits are 1 x & y
OR Sets each bit to 1 if one of two bits is 1 x | y
^ XOR Sets each bit to 1 if only one of two bits is 1 x ^ y
~ NOT Inverts all the bits ~x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Bitwise Operators - Left shift operator «

A

you can test with a math formula y«n
y x 2^n
print(3 « 1)
output : 6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Bitwise Operators - Right shift operator »

A

you can test with a math formula y«n
y / 2^n
print(3»1)
output : 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly