Python Basics to Start Programming Flashcards
What are docstrings in python ?
Multiple line comments enclosed within triple quotation marks are docstrings.
“””
This is an dockstring
“””
How to use python comments ?
Two Methods,
- for single line comments use # symbol.
- for multiline comments use docstrings.
How to use python comments ?
Two Methods,
- for single line comments use # symbol.
- for multiline comments use docstrings.
How numbers start in python ?
Number in python should be starts with number 0-9 > 1,10
,decimal sighn > .1 , .326
,or hypen sighn > -22
How to write strings in python ?
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 to add line break in python string ?
Two Methods:
- Use Escape Sequence with n
- Use Tripple Quotatin Marks
string1 = “Hemant \nPinky\n Bella”
string2 = “"”Hemant
Pinky
Bella”””
print(string1)
print(string2)
How many Data Types in Python ?
- Four Data Types in Python.
- Integer
- Float
- String
- Boolean
How many Arithmatic Operators in Python ?
Total Seven Operators.
- Addition 1 + 1 = 2
- Subtraction 10 - 1 = 9
- * Multiplication 3 * 5 = 15
- / Division 10 / 5 = 2
- % Modulus (remainder after division) 11 % 5 = 1
- ** Exponent 3**2 = 9
- // Floor division 11 // 5 = 2
How many comparison operators in python ?
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 Many Boolean Operators in Python ?
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
Python Variable Creation Rules ?
- Variable Name must be starts with letter or underscore.
- After first character you use letters,underscores or numbers.
- Variables are case sensitive.
- PEP 8 style conventions recommend that you use only lowercase letters in variable names.
What is the acronym of IEEE?
Institute of Electrical and Electronics Engineers.
Which Languages are popular for Data Science ?
Julia and R languages are very much popular in Data Science.
How to Show dollar Amount with string?
price = 2345
print(f”${ price:,}”)
How to save multiline format strings in varible ?
Two Methods
- Use Escape Sequence
- 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 to use multiline f-string to show integer variables ?
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)
What is the Base of the binary, octal, hexadecimal number system, and digits used in each number system?
- Binary - Base 2 : 0,1
- Octal - Base 8 : 0,1,2,3,4,5,6,7
- Decimal - Base 10 0,1,2,3,4,5,6,70,1,2,3,4,5,6,7,8,9
- Hexadecimal - Base 16
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
How to convert a number to another number system in python ?
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 to concatenate firstName lastName variables ?
Two Methods :
- save firstName & lastName Variables in other variable then print .
- print firstName & lastName Variables using print() function
firstName = “Hemant”
lastName = “ Kumar”
fullName = firstName + lastName
print(“Full Name :”,fullName)
print(“Full Name :”,firstName + lastName)
How to set width & align in f-string ?
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 to Conactenate dollor sign with integer value ?
Two Methods :
- Use print Function.
- 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 to Get Length of String ?
Using len() Function ?
string1 = “” string2 = “ ” string3 = “Thjs is an string”
print(len(string1))
print(len(string2))
print(len(string3))