Bird
Raised Fist0
Data Analysis Pythondata~10 mins

P-values and significance in Data Analysis Python - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the p-value from a t-test.

Data Analysis Python
from scipy import stats
sample1 = [5, 6, 7, 8, 9]
sample2 = [5, 5, 6, 7, 8]
t_stat, p_value = stats.ttest_ind(sample1, sample2)
print('P-value:', [1])
Drag options to blanks, or click blank then click option'
Asample1
Bp_value
Ct_stat
Dstats
Attempts:
3 left
💡 Hint
Common Mistakes
Using the t-statistic instead of the p-value.
Printing the function instead of the variable.
2fill in blank
medium

Complete the code to check if the p-value is less than the significance level 0.05.

Data Analysis Python
p_value = 0.03
if p_value [1] 0.05:
    print('Reject the null hypothesis')
else:
    print('Fail to reject the null hypothesis')
Drag options to blanks, or click blank then click option'
A<
B>=
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < for the comparison.
Checking equality instead of less than.
3fill in blank
hard

Fix the error in the code to correctly calculate the p-value for a paired t-test.

Data Analysis Python
from scipy import stats
before = [20, 21, 19, 22, 20]
after = [22, 23, 20, 24, 21]
t_stat, p_value = stats.ttest_rel(before, [1])
print('P-value:', p_value)
Drag options to blanks, or click blank then click option'
Aafter
Bstats
Cbefore
Dttest_rel
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the same sample twice.
Passing the function name instead of data.
4fill in blank
hard

Fill both blanks to create a dictionary of words and their lengths, but only include words longer than 3 letters.

Data Analysis Python
words = ['apple', 'cat', 'banana', 'dog', 'pear']
lengths = { [1]: len([2]) for word in words if len(word) > 3 }
Drag options to blanks, or click blank then click option'
Aword
Bwords
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Using the list 'words' instead of the variable 'word'.
Using 'len' as a key instead of the word.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase words and their lengths, including only words with length greater than 4.

Data Analysis Python
words = ['apple', 'cat', 'banana', 'dog', 'pear']
result = { [1]: [2] for word in words if len(word) [3] 4 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original word instead of uppercase.
Using wrong comparison operator.

Practice

(1/5)
1. What does a p-value less than 0.05 usually indicate in a hypothesis test?
easy
A. The results are statistically significant and unlikely due to chance.
B. The results are definitely caused by random chance.
C. The sample size is too small to draw conclusions.
D. The test was performed incorrectly.

Solution

  1. Step 1: Understand the meaning of p-value

    A p-value measures how likely the observed data would occur if the null hypothesis were true (random chance).
  2. Step 2: Compare p-value to significance level

    If the p-value is less than 0.05 (common threshold), it means the observed result is unlikely due to chance, so we reject the null hypothesis.
  3. Final Answer:

    The results are statistically significant and unlikely due to chance. -> Option A
  4. Quick Check:

    p-value < 0.05 means significant [OK]
Hint: p-value < 0.05 means strong evidence against chance [OK]
Common Mistakes:
  • Thinking p-value shows probability null is true
  • Confusing significance level with p-value
  • Assuming p-value proves practical importance
2. Which of the following is the correct way to check if a p-value stored in variable p_val is significant at 0.05 level in Python?
easy
A. if p_val == 0.05: print('Significant')
B. if p_val > 0.05: print('Significant')
C. if p_val < 0.05: print('Significant')
D. if p_val <= 0.5: print('Significant')

Solution

  1. Step 1: Recall significance condition

    A p-value less than 0.05 means significance, so we check if p_val < 0.05.
  2. Step 2: Match condition to code

    if p_val < 0.05: print('Significant') uses < 0.05 correctly. Others check wrong conditions or wrong thresholds.
  3. Final Answer:

    if p_val < 0.05: print('Significant') -> Option C
  4. Quick Check:

    Use < 0.05 for significance [OK]
Hint: Check if p_val < 0.05 for significance [OK]
Common Mistakes:
  • Using > instead of < for significance
  • Checking equality to 0.05 exactly
  • Using wrong threshold like 0.5
3. Given this Python code snippet, what will be printed?
p_val = 0.07
if p_val < 0.05:
    print('Reject null hypothesis')
else:
    print('Fail to reject null hypothesis')
medium
A. Reject null hypothesis
B. SyntaxError
C. No output
D. Fail to reject null hypothesis

Solution

  1. Step 1: Check the p-value against 0.05

    The p_val is 0.07, which is greater than 0.05, so the condition p_val < 0.05 is False.
  2. Step 2: Determine which branch runs

    Since the condition is False, the else block runs, printing 'Fail to reject null hypothesis'.
  3. Final Answer:

    Fail to reject null hypothesis -> Option D
  4. Quick Check:

    p_val = 0.07 > 0.05 means fail to reject [OK]
Hint: p_val > 0.05 means fail to reject null [OK]
Common Mistakes:
  • Confusing greater than with less than
  • Assuming equality means reject
  • Ignoring else branch
4. This code is intended to print 'Significant' if p-value is less than 0.05, but it always prints 'Not Significant'. What is the error?
p_val = 0.03
if p_val > 0.05:
    print('Significant')
else:
    print('Not Significant')
medium
A. The comparison operator should be '<' instead of '>'
B. The variable p_val is not defined
C. The print statements are reversed
D. The p_val value is incorrect

Solution

  1. Step 1: Analyze the condition logic

    The code checks if p_val > 0.05 to print 'Significant', but significance means p_val < 0.05.
  2. Step 2: Identify the fix

    Changing the operator to < will correctly print 'Significant' when p_val is 0.03.
  3. Final Answer:

    The comparison operator should be '<' instead of '>' -> Option A
  4. Quick Check:

    Use < 0.05 for significance check [OK]
Hint: Use '<' to check p-value significance [OK]
Common Mistakes:
  • Using '>' instead of '<' for significance
  • Swapping print statements without fixing condition
  • Assuming variable is undefined
5. You run a test and get a p-value of 0.049. You want to be very strict and use a significance level of 0.01. What should you conclude?
hard
A. Reject the null hypothesis because 0.049 < 0.05
B. Fail to reject the null hypothesis because 0.049 > 0.01
C. Reject the null hypothesis because 0.049 < 0.01
D. Fail to reject the null hypothesis because 0.049 > 0.05

Solution

  1. Step 1: Understand significance level

    The significance level is set to 0.01, meaning p-values must be less than 0.01 to reject the null hypothesis.
  2. Step 2: Compare p-value to significance level

    The p-value 0.049 is greater than 0.01, so we fail to reject the null hypothesis at this stricter level.
  3. Final Answer:

    Fail to reject the null hypothesis because 0.049 > 0.01 -> Option B
  4. Quick Check:

    p-value > significance level means fail to reject [OK]
Hint: Compare p-value to chosen significance level carefully [OK]
Common Mistakes:
  • Using default 0.05 without checking chosen level
  • Confusing less than and greater than
  • Ignoring stricter significance level
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard