π C# to Python Quick Reference Guide
A side-by-side comparison for CoderDojo students transitioning from C# to Python! ππ
π Basic Syntax Comparison
C#
Python
Console.WriteLine("Hello!");
print("Hello!")
Comments π¬
C#
Python
// Single line comment
# Single line comment
/* Multi-line comment */
""" Multi-line comment """
C#
Python
Required semicolons ;
No semicolons needed!
Uses { } for code blocks
Uses indentation (4 spaces)
// C#
string name = "Alex" ;
int age = 14 ;
double height = 5.6 ;
bool isStudent = true ;
# Python
name = "Alex"
age = 14
height = 5.6
is_student = True # Note: True/False are capitalized!
// C#
const double PI = 3.14159 ;
# Python (by convention, use ALL_CAPS)
PI = 3.14159 # Not enforced, but everyone knows not to change it!
C# Type
Python Type
Example
string
str
"Hello"
int
int
42
double/float
float
3.14
bool
bool
True / False
List<T>
list
[1, 2, 3]
Dictionary<K,V>
dict
{"key": "value"}
int[] (array)
list
[1, 2, 3]
// C#
Console . WriteLine ( "Hello!" ) ;
Console . WriteLine ( $ "Name: { name } , Age: { age } ") ;
Console . Write ( "No newline" ) ;
# Python
print ("Hello!" )
print (f"Name: { name } , Age: { age } " )
print ("No newline" , end = "" )
// C#
Console . Write ( "Enter name: " ) ;
string name = Console . ReadLine ( ) ;
Console . Write ( "Enter age: " ) ;
int age = int . Parse ( Console . ReadLine ( ) ) ;
# Python
name = input ("Enter name: " )
age = int (input ("Enter age: " ))
// C#
if ( age < 13 )
{
Console . WriteLine ( "Child" ) ;
}
else if ( age < 20 )
{
Console . WriteLine ( "Teen" ) ;
}
else
{
Console . WriteLine ( "Adult" ) ;
}
# Python
if age < 13 :
print ("Child" )
elif age < 20 :
print ("Teen" )
else :
print ("Adult" )
C#
Python
Meaning
==
==
Equal to
!=
!=
Not equal to
<, >
<, >
Less/Greater than
<=, >=
<=, >=
Less/Greater or equal
&&
and
Logical AND
||
or
Logical OR
!
not
Logical NOT
// C#
for ( int i = 0 ; i < 5 ; i ++ )
{
Console . WriteLine ( i ) ;
}
# Python
for i in range (5 ):
print (i )
// C#
string [ ] fruits = { "apple" , "banana" , "cherry" } ;
foreach ( string fruit in fruits )
{
Console . WriteLine ( fruit ) ;
}
# Python
fruits = ["apple" , "banana" , "cherry" ]
for fruit in fruits :
print (fruit )
// C#
int count = 0 ;
while ( count < 5 )
{
Console . WriteLine ( count ) ;
count ++ ;
}
# Python
count = 0
while count < 5 :
print (count )
count += 1
// C#
List < string > names = new List < string > { "Alice" , "Bob" , "Charlie" } ;
// or array:
string [ ] names = { "Alice" , "Bob" , "Charlie" } ;
# Python
names = ["Alice" , "Bob" , "Charlie" ]
Operation
C#
Python
Add item
list.Add(item)
list.append(item)
Remove item
list.Remove(item)
list.remove(item)
Get length
list.Count
len(list)
Access item
list[0]
list[0]
Last item
list[list.Count - 1]
list[-1]
Check if contains
list.Contains(item)
item in list
Clear list
list.Clear()
list.clear()
Python-Only: Slicing! πͺ
# Python
numbers = [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
numbers [2 :5 ] # [2, 3, 4] - from index 2 to 4
numbers [:3 ] # [0, 1, 2] - first 3 items
numbers [7 :] # [7, 8, 9] - from index 7 to end
numbers [- 3 :] # [7, 8, 9] - last 3 items
numbers [::2 ] # [0, 2, 4, 6, 8] - every 2nd item
numbers [::- 1 ] # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] - reversed!
// C#
var student = new Dictionary < string , object >
{
{ "name" , "Alex" } ,
{ "age" , 14 } ,
{ "grade" , 8 }
} ;
# Python
student = {
"name" : "Alex" ,
"age" : 14 ,
"grade" : 8
}
Operation
C#
Python
Get value
dict["key"]
dict["key"]
Set value
dict["key"] = value
dict["key"] = value
Check key exists
dict.ContainsKey("key")
"key" in dict
Get all keys
dict.Keys
dict.keys()
Get all values
dict.Values
dict.values()
// C#
void SayHello ( )
{
Console . WriteLine ( "Hello!" ) ;
}
# Python
def say_hello ():
print ("Hello!" )
// C#
void Greet ( string name )
{
Console . WriteLine ( $ "Hello, { name } !") ;
}
# Python
def greet (name ):
print (f"Hello, { name } !" )
Function with Return Value
// C#
int Add ( int a , int b )
{
return a + b ;
}
# Python
def add (a , b ):
return a + b
// C#
void Greet ( string name , string greeting = "Hello" )
{
Console . WriteLine ( $ "{ greeting } , { name } !") ;
}
# Python
def greet (name , greeting = "Hello" ):
print (f"{ greeting } , { name } !" )
// C#
class Dog
{
public string Name { get ; set ; }
public int Age { get ; set ; }
public Dog ( string name , int age )
{
Name = name ;
Age = age ;
}
public void Bark ( )
{
Console . WriteLine ( $ "{ Name } says: Woof!") ;
}
}
// Using it:
Dog buddy = new Dog ( "Buddy" , 3 ) ;
buddy . Bark ( ) ;
# Python
class Dog :
def __init__ (self , name , age ):
self .name = name
self .age = age
def bark (self ):
print (f"{ self .name } says: Woof!" )
# Using it:
buddy = Dog ("Buddy" , 3 )
buddy .bark ()
Key Differences in Classes
Feature
C#
Python
Constructor
Same name as class
__init__
Self reference
this (optional)
self (required)
Access modifiers
public, private, etc.
None (convention: _private)
Creating object
new ClassName()
ClassName()
What
C# Style
Python Style
Variables
camelCase
snake_case
Functions
PascalCase
snake_case
Classes
PascalCase
PascalCase
Constants
PascalCase or UPPER_CASE
UPPER_CASE
Private
_name or private
_name
// C# Style
public class MyClass
{
private string _firstName ;
public const int MaxValue = 100 ;
public void DoSomething ( ) { }
}
# Python Style
class MyClass :
MAX_VALUE = 100
def __init__ (self ):
self ._first_name = ""
def do_something (self ):
pass
Operation
C#
Python
Length
str.Length
len(str)
Upper case
str.ToUpper()
str.upper()
Lower case
str.ToLower()
str.lower()
Contains
str.Contains("x")
"x" in str
Split
str.Split(' ')
str.split(' ')
Join
string.Join(", ", list)
", ".join(list)
Format
$"Hello {name}"
f"Hello {name}"
Operation
C#
Python
Power
Math.Pow(2, 3)
2 ** 3 or pow(2, 3)
Square root
Math.Sqrt(16)
16 ** 0.5 or math.sqrt(16)
Absolute
Math.Abs(-5)
abs(-5)
Round
Math.Round(3.7)
round(3.7)
Random
new Random().Next(1, 10)
random.randint(1, 10)
π Python Extras (Things C# Doesn't Have!)
# Python
a , b , c = 1 , 2 , 3
x , y = y , x # Swap values!
# Python
# Create a list of squares
squares = [x ** 2 for x in range (10 )]
# Result: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# Filter a list
evens = [x for x in range (20 ) if x % 2 == 0 ]
# Result: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
# Python
def get_name_and_age ():
return "Alex" , 14
name , age = get_name_and_age ()
F-Strings with Expressions
# Python
x = 10
print (f"{ x } squared is { x ** 2 } " ) # "10 squared is 100"
π Quick Reference Table
Concept
C#
Python
File extension
.cs
.py
Run code
dotnet run
python3 file.py
Comments
// or /* */
# or """ """
True/False
true/false
True/False
Null/None
null
None
And/Or/Not
&&/||/!
and/or/not
String format
$"text {var}"
f"text {var}"
Main function
static void Main()
if __name__ == "__main__":
Now you know both C# and Python! ππ
Remember:
C# is great for: Games (Unity), desktop apps, web APIs
Python is great for: AI, data science, automation, scripting
The best programmers know multiple languages! π