1. Home
  2. /
  3. Python Typing Test
  4. /
  5. List Comprehension and Functions

List Comprehension and Functions - Python Typing CST Test

Skip to main content
CodeSpeedTest
Languages
Start TypingJump into a test — pick any languageAdaptive TrainingUnlock chars as you master themPractice DrillsFocused sessions targeting weak spotsDaily ChallengesNew coding challenges every dayRace ModeCompete against others in real timeAI OpponentRace against an AI at your WPM levelTournamentsLive coding speed tournamentsArcade GamesZType, Overkill Survival, Glyphica & moreGamificationXP, coins, badges & quests
LeaderboardGlobal rankings for every languageCertificatesEarn verifiable Bronze / Silver / Gold certsActivityDaily streaks & historical analyticsProfileYour stats, badges & achievements
Browse Languages500+ languages with real code examplesBlogTips, guides & deep divesFree ToolsWPM calculator, typing speed report & moreFAQCommon questions answeredGetting StartedNew to CodeSpeedTest?AboutOur story & missionSupportGet help — Pro users get priorityContactGet in touch with the team
Pricing
Mode:
Duration:
Output
Fibonacci: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Primes up to 30: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Even squares: [4, 16, 36, 64, 100]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import math
from typing import List, Optional

def fibonacci(n: int) -> List[int]:
"""Generate fibonacci sequence up to n numbers"""
if n <= 0:
return []
elif n == 1:
return [0]

fib = [0, 1]
for i in range(2, n):
fib.append(fib[i-1] + fib[i-2])
return fib

def find_primes(limit: int) -> List[int]:
"""Find all prime numbers up to limit using sieve"""
primes = []
sieve = [True] * (limit + 1)

for i in range(2, int(math.sqrt(limit)) + 1):
if sieve[i]:
for j in range(i * i, limit + 1, i):
sieve[j] = False

return [i for i in range(2, limit + 1) if sieve[i]]

# Main execution
if __name__ == "__main__":
fib_sequence = fibonacci(10)
print(f"Fibonacci: {fib_sequence}")

primes = find_primes(30)
print(f"Primes up to 30: {primes}")

# List comprehension example
squares = [x**2 for x in range(1, 11) if x % 2 == 0]
print(f"Even squares: {squares}")
Coding works best on desktop or with an external keyboard.
~`
!1
@2
#3
$4
%5
^6
&7
*8
(9
)0
_-
+=
⌫
Tab
Q
W
E
R
T
Y
U
I
O
P
{[
}]
|\
Caps
A
S
D
F
G
H
J
K
L
:;
"'
↵
⇧
Z
X
C
V
B
N
M
<,
>.
?/
⇧
Ctrl
⊞
Alt
␣
Alt
⊞
Ctx
Ctrl
👉Middle
i
L Pinky
L Ring
L Middle
L Index
Thumb
R Index
R Middle
R Ring
R Pinky
CodeSpeedTest

Improve your coding speed, code accuracy, and programming syntax WPM with practice sessions across 500+ programming languages.

Quick Links

HomeAboutFeaturesGetting StartedLanguages

Legal & Support

Pro ⚡ PricingContactPrivacy PolicyTerms of Service

Connect

CodeSpeedTest on GitHubCodeSpeedTest on TwitterEmail CodeSpeedTest

© 2026 CodeSpeedTest. All rights reserved.

List Comprehension and Functions — Python Code

Demonstrates Python's list comprehension, type hints, and function definitions with docstrings.

import math
from typing import List, Optional

def fibonacci(n: int) -> List[int]:
	"""Generate fibonacci sequence up to n numbers"""
	if n <= 0:
		return []
	elif n == 1:
		return [0]

	fib = [0, 1]
	for i in range(2, n):
		fib.append(fib[i-1] + fib[i-2])
	return fib

def find_primes(limit: int) -> List[int]:
	"""Find all prime numbers up to limit using sieve"""
	primes = []
	sieve = [True] * (limit + 1)

	for i in range(2, int(math.sqrt(limit)) + 1):
		if sieve[i]:
		for j in range(i * i, limit + 1, i):
		sieve[j] = False

	return [i for i in range(2, limit + 1) if sieve[i]]

# Main execution
if __name__ == "__main__":
	fib_sequence = fibonacci(10)
	print(f"Fibonacci: {fib_sequence}")

	primes = find_primes(30)
	print(f"Primes up to 30: {primes}")

	# List comprehension example
	squares = [x**2 for x in range(1, 11) if x % 2 == 0]
	print(f"Even squares: {squares}")

Python Language Guide

Python is a high-level, dynamically typed, multi-paradigm programming language known for simplicity, readability, and massive ecosystem support. It powers web development, data science, machine learning, automation, scripting, backend systems, and more.

Primary Use Cases

  • ▸Backend web development
  • ▸Machine learning & AI
  • ▸Data analysis & visualization
  • ▸Automation & scripting
  • ▸API development
  • ▸Scientific computing
  • ▸DevOps tooling
  • ▸Cybersecurity scripting

Notable Features

  • ▸Simple, readable syntax
  • ▸Massive standard library
  • ▸Dynamically typed
  • ▸Extensive third-party ecosystem (PyPI)
  • ▸Cross-platform
  • ▸Strong scientific & ML libraries

Origin & Creator

Created by Guido van Rossum in 1991, inspired by ABC language with a vision of a simple, readable language for everyday programming tasks.

Industrial Note

Python dominates in AI/ML research, automation-heavy engineering teams, fast MVP prototyping, data-driven industries, hybrid cloud pipelines, ETL scripting, scientific computing, and large-scale analytical workflows.

Quick Explain

  • ▸Python emphasizes clean syntax and developer productivity.
  • ▸It supports procedural, object-oriented, and functional programming styles.
  • ▸Used across data science, AI, web development, automation, and scripting.

Core Features

  • ▸Object-oriented and functional support
  • ▸Garbage-collected memory management
  • ▸Interactive REPL
  • ▸Rich built-in data types
  • ▸Module/package system
  • ▸Asynchronous programming (async/await)

Learning Path

  • ▸Basics + syntax
  • ▸OOP + modules
  • ▸Web or data specialization
  • ▸Async + frameworks
  • ▸Advanced tooling

Practical Examples

  • ▸Data analysis script
  • ▸REST API with FastAPI
  • ▸Machine learning model
  • ▸Automation with Selenium
  • ▸File system utilities

Comparisons

  • ▸Easier than Java for beginners
  • ▸More flexible than C++
  • ▸Slower than Go/Rust
  • ▸Stronger ML ecosystem than JavaScript

Strengths

  • ▸Beginner-friendly
  • ▸Huge ecosystem
  • ▸Excellent for AI/ML
  • ▸Fast development cycle
  • ▸Great community support

Limitations

  • ▸Slower execution than compiled languages
  • ▸Weak mobile development ecosystem
  • ▸GIL limits multi-threaded CPU performance
  • ▸Runtime errors due to dynamic typing

When NOT to Use

  • ▸High-performance embedded systems
  • ▸Performance-critical computation
  • ▸Mobile app development
  • ▸Browser-based execution

Cheat Sheet

  • ▸List comprehension: [x for x in arr]
  • ▸Dictionary: {'a': 1}
  • ▸Lambda: lambda x: x + 1
  • ▸Async: async/await
  • ▸Import: from module import X

FAQ

  • ▸Is Python slow?
  • ▸Slower than compiled languages but fast enough with optimizations.
  • ▸Is Python good for ML?
  • ▸Yes-it's the top ML/AI language.
  • ▸Can Python run on web?
  • ▸Yes via backends, not directly in browser.
  • ▸Is Python good for beginners?
  • ▸It’s the most beginner-friendly mainstream language.

30-Day Skill Plan

  • ▸Week 1: Syntax & basics
  • ▸Week 2: OOP + functions
  • ▸Week 3: APIs + automation
  • ▸Week 4: Pick a specialization

Final Summary

  • ▸Python is a flexible, beginner-friendly language with a massive ecosystem.
  • ▸It dominates AI/ML, automation, and backend development.
  • ▸Its clarity, libraries, and community make it ideal for rapid development.
  • ▸Despite performance limitations, it’s among the most versatile languages ever built.

Project Structure

  • ▸src/ modules
  • ▸requirements.txt / pyproject.toml
  • ▸venv environment
  • ▸tests/ folder
  • ▸README + configs

Monetization

  • ▸Freelance automation scripts
  • ▸ML/AI engineering
  • ▸Backend development
  • ▸Data analysis consulting

Productivity Tips

  • ▸Use type hints
  • ▸Use virtualenv
  • ▸Use list comprehensions
  • ▸Profile code regularly

Basic Concepts

  • ▸Variables & dynamic typing
  • ▸Control flow
  • ▸Functions & classes
  • ▸Modules and packages
  • ▸Lists, tuples, dicts, sets
  • ▸Error handling

Official Docs

  • ▸Python Official Documentation
  • ▸PyPI Package Index
  • ▸Python PEP Index

More Python Typing Exercises

Python Quick Sort AlgorithmPython Dictionary ComprehensionsPython Filter and MapPython Lambda and ReducePython Set ComprehensionPython Nested List ComprehensionPython Function with Optional ArgumentPython Zip FunctionPython List Comprehension with ConditionPython Recursion Example

Practice Other Languages

CReactC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypherGremlin