Python Basics to Start Programming Flashcards

1
Q

What are docstrings in python ?

A

Multiple line comments enclosed within triple quotation marks are docstrings.
“””
This is an dockstring
“””

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

How to use python comments ?

A

Two Methods,

  1. for single line comments use # symbol.
  2. for multiline comments use docstrings.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to use python comments ?

A

Two Methods,

  1. for single line comments use # symbol.
  2. for multiline comments use docstrings.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How numbers start in python ?

A

Number in python should be starts with number 0-9 > 1,10
,decimal sighn > .1 , .326
,or hypen sighn > -22

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

How to write strings in python ?

A

Single qoutes & double quotes are used
“Hi there, I am a string”
‘Hello world’

If string contains single quotes also,then enclose this in double quotes,if a string contains double quotes then enclosed it in single quotes. if a string contains both then use escape character.
“Mary’s dog said Woof”
‘The dog of Mary said “Woof”.’
‘Mary's dog said “Woof”.’
“Mary’s dog said "Woof".”

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

How to add line break in python string ?

A

Two Methods:

  1. Use Escape Sequence with n
  2. Use Tripple Quotatin Marks

string1 = “Hemant \nPinky\n Bella”
string2 = “"”Hemant
Pinky
Bella”””

print(string1)
print(string2)

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

How many Data Types in Python ?

A
  1. Four Data Types in Python.
  2. Integer
  3. Float
  4. String
  5. Boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How many Arithmatic Operators in Python ?

A

Total Seven Operators.

    • Addition 1 + 1 = 2
    • Subtraction 10 - 1 = 9
  1. * Multiplication 3 * 5 = 15
  2. / Division 10 / 5 = 2
  3. % Modulus (remainder after division) 11 % 5 = 1
  4. ** Exponent 3**2 = 9
  5. // Floor division 11 // 5 = 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How many comparison operators in python ?

A

Total Eight Operators

Operator Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
!= Not equal to
is Object identity
is not Negated object identity

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

How Many Boolean Operators in Python ?

A

Total Three

or x or y Either x or y is True
and x and y Both x and y are True
not not x x is not True

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

Python Variable Creation Rules ?

A
  1. Variable Name must be starts with letter or underscore.
  2. After first character you use letters,underscores or numbers.
  3. Variables are case sensitive.
  4. PEP 8 style conventions recommend that you use only lowercase letters in variable names.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the acronym of IEEE?

A

Institute of Electrical and Electronics Engineers.

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

Which Languages are popular for Data Science ?

A

Julia and R languages are very much popular in Data Science.

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

How to Show dollar Amount with string?

A

price = 2345
print(f”${ price:,}”)

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

How to save multiline format strings in varible ?

A

Two Methods

  1. Use Escape Sequence
  2. Use Tripple Quotation Marks
user1 = "hemant"
user2 = "Pinky"
user3 = "Bella"
allUsers = f"{user1}\n{user2}\n{user3}"

print(allUsers)

allUsers = f”””
User1 - {user1}
User2 - {user2}
User3 - {user3}”””

print(allUsers)

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

How to use multiline f-string to show integer variables ?

A

Tripple Quotation marks are used.
unitPrice = 24
quantity = 100
saleTaxRate = .65
subTotal = quantity * unitPrice
saleTax = subTotal * saleTaxRate
total = subTotal + saleTax

outPut = f”"”Subtotal : Rs. {subTotal:,}
SaleTax : Rs. {saleTax:,}
Total : Rs. {total:,}”””

print(outPut)

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

What is the Base of the binary, octal, hexadecimal number system, and digits used in each number system?

A
  1. Binary - Base 2 : 0,1
  2. Octal - Base 8 : 0,1,2,3,4,5,6,7
  3. Decimal - Base 10 0,1,2,3,4,5,6,70,1,2,3,4,5,6,7,8,9
  4. Hexadecimal - Base 16
    0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How to convert a number to another number system in python ?

A

We have to use three Functions,oct(),bin(),hex() .
print(oct(x)),
print(bin(x)),
print(hex(x))

print(“Hexa: “,0xff)
print(“Binary: “,0b1111)
print(“Octal:”,0o377)

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

How to concatenate firstName lastName variables ?

A

Two Methods :

  1. save firstName & lastName Variables in other variable then print .
  2. print firstName & lastName Variables using print() function

firstName = “Hemant”
lastName = “ Kumar”
fullName = firstName + lastName
print(“Full Name :”,fullName)
print(“Full Name :”,firstName + lastName)

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

How to set width & align in f-string ?

A

print(f”fullName:>9”)
You can control the width of your output (and the alignment of content within that width)
by following the colon in your f-string with
< (for left aligned),
^ (for centered),
or > (for right aligned).

firstName = “First”
middleName = “Middle”
lastName = “Last”
print(firstName,middleName,lastName)
print(f”{firstName}”,f”{middleName:^10}”,f”{lastName:}”)

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

How to Conactenate dollor sign with integer value ?

A

Two Methods :

  1. Use print Function.
  2. Save integer value in string variable by using f-strings.
unitPrice = 23
quantity = 34
salesTaxRate = .54
subTotal = quantity \* unitPrice
salesTax = subTotal \* salesTaxRate
baseTotal = subTotal + salesTax
s\_subTotal = "$" + f"{subTotal:,.2f}"
s\_baseTotal = "$" + f"{baseTotal:,.2f}"
s\_salesTax = "$" + f"{salesTax:,.2f}"

output = f”””
Sub Total : {s_subTotal:>9}
Sales Tax : {s_salesTax:>9}
Total : {s_baseTotal:>9}
“””
print(output)

print(f”Unit Price:${unitPrice}\nQuantity:{quantity}\nSale Tax: ${salesTax:.2f}\nTotal: ${subTotal}”)
print(“Unit Price:$”,unitPrice,”\nQuantity:”,quantity,”\nSale Tax: $”,salesTax,”\nTotal: $”,subTotal)

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

How to Get Length of String ?

A

Using len() Function ?

string1 = “”
string2 = “ ”
string3 = “Thjs is an string”

print(len(string1))
print(len(string2))
print(len(string3))

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

List some string Functions ?

A

len()

24
Q

How to search a particular character in string ?

A

We have to use print() Function,it returns True or False value ?

s = "Abracadabra Hocus Pocus you're a turtle dove"
#print True if t found in s
print("t" in s)
#print False if t not found in s
print("t" not in s)
25
Q

How to print first character of string ?

A

We have to use string subscript.
string = “Hemant”

print(string[0])

26
Q

How to print 15 hyphens in a row ?

A

print(“-“ * 15)

27
Q

How to print string characters from 0 to 10 ?

A

s = “Abracadabra Hocus Pocus you’re a turtle dove”

print(s[0:10])

28
Q

How to Print every third char of string from 0 to 10 ?

A

s = “Abracadabra Hocus Pocus you’re a turtle dove”

print(s[0:20:2])

29
Q

How to print Largest character of string ?

A

Use max(s) function,it returns largest character of string by its ASCII Value .

s = “Abracadabra Hocus Pocus you’re a turtle dove”

print(max(s))

30
Q

How to print the smallest character of string ?

A

This prints nothing because it detects spaces

s = “Abracadabra Hocus Pocus you’re a turtle dove”

print(min(s))

31
Q

How to find the index value of character in string?

A

s = “Abracadabra Hocus Pocus you’re a turtle dove”

print(s.index(“H”))

32
Q

How to know number of occurencs of H in String ?

A

s = “Abracadabra Hocus Pocus you’re a turtle dove HHHHHHHH”

print(s.count(“H”))

33
Q

How to know the index value of H in given range of string ?

A

s = “Abracadabra Hocus Pocus you’re a turtle dove HHHHHHHHZ”

print(len(s))

print(s.index(“H”,13,50))

34
Q

How to print ASCII Value of character ?

A
Using ord() function ?
s = "Abracadabra Hocus Pocus you're a turtle dove HHHHHHHHZ"

print(ord(“A”))

35
Q

How to print character of ASCII Value ?

A

s = “Abracadabra Hocus Pocus you’re a turtle dove HHHHHHHHZ”

print(chr(65))

36
Q

How to Create Keyboard Shorctcut for running python code in vsCode ?

A

Goto File → Preferences → Keyboard Shortcuts
Type run python in terminal and Click the Python: Run Python File in Terminal.
Now create a kyboard shortcut sequence that works for you.

Source : https://jasonmurray.org/posts/2020/vscodeplay/

37
Q

How to use Escape Sequence in String ?

A
string4 = 'This is Hemant\'s Place'
string5 = "This is \"Hemant\""

print(string4)
print(string5)

38
Q

How to import date module ?

A

import datetime as dt

39
Q

How to save today’s date in variable ?

A

import datetime as dt
todayDate = dt.date.today()
date = dt.date(2019,12,31)

print(todayDate);print(date)

40
Q

How to create a Date object ?

A

import datetime as dt

date = dt.date(2019,12,31)

print(todayDate);print(date)

41
Q

How to date object values separately ?

A

import datetime as dt
date = dt.date(2019,12,31)

print(date.month)
print(date.day)
print(date.year)

42
Q

How to format date object output to display date ?

A

import datetime as dt
todayDate = dt.date.today()
date = dt.date(2019,12,31)

print(f”{date:%A, %B %d, %Y}”)

43
Q

How to show Date in DD YY format ?

A

import datetime as dt
today = dt.date.today()
todays_date = f”{today:%d/%m/%Y}”

print(todays_date)

44
Q

How to know the class of variable ?

A

import datetime as dt

today = dt.date.today()

print(type(today))

45
Q

How to save midnight time in variable and print ?

A

import datetime as dt

date = dt.time()

print(date)

print(type(date))

46
Q

How to calculate time spans ?

A

import datetime as dt

newYearDay = dt.date(2019,1,1)

memorialDay = dt.date(2019,5,27)

print(memorialDay - newYearDay)

47
Q

How to print extended date by adding days to current date ?

A

import datetime as dt
uration = dt.timedelta(days=146)
todayDate = dt.datetime.today()
extendedDate = todayDate + duration

print(f”Today’s Date = {todayDate:%a, %b %d %Y}”)
print(“Extended Date = “,f”{extendedDate:%a, %b %d %Y}”)

48
Q

How to Claculate your age in python ?

A

import datetime as dt
dob = dt.date(1983,6,1)
todayDate = dt.date.today()
deltaAge= todayDate - dob
daysOld = deltaAge.days

print(f”DOB = {dob}”)
print(f”Today’s Date = {todayDate}”)
print(f”Your are {daysOld // 365} Years & “ f”{(daysOld % 365) // 30} Months Old “ f”on {todayDate}”)

49
Q

How many types of Arithmetic Operators are in Python,
list them in order of precedence?

A

Total Nine,Precedence is PEMDAS

1 - () grouping
2 - **exponent
3 - –negation
4 - *multiplication
5 - /division
6 - %modulus
7 - //floor division
8 - +addition
9 - –subtraction

50
Q

How many types of Comparison Operators are in Python ?

A

Total Six Comparison Operators.

1 - == equal to
2 - != not equal to
3 - > greater than
4 - < less than
5 - >= greater than or equal to
6 - <= less than or equal to

51
Q

How many types of Boolean Operators ?

A

Total Three

1 - and logical and
2 - or logical or
3 - not logical not

52
Q

When was Python language created?

A

It was created by Guido Van Rossum in 1991.

53
Q

How to print Python Keywords?

A

import keyword

print(keyword.kwlist)

54
Q

How to check string value in Python?

A

sun = ‘down’

if sun == ‘down’ :
print(‘Good Night’)

55
Q

How to Check Boolean Variable Value ?

A

taxable = True

if taxable :
print(‘Amount is Taxable’)

56
Q

How to Create date variable in Python ?

A

import datetime as dt
myBirthday = dt.date(1983,3,11)
print(f”YYYY-MM-DD - {myBirthday}”)

57
Q

Format String Examples

A

print(f”Subtotal: ${quantity * unit_price:,.2f}”) # round off to two decimal points
print(f”Sales Tax Rate {sales_tax_rate:.2%}”) # shows in two decimal digits
print(f”Subtotal: ${quantity * unit_price:,}”) # shows comma in thousand