#include <stdio.
h>
int main(void)
{
printf("hello, world\n");
}
print("hello, world")
make hello
./hello
clang -o hello hello.c -lcs50
./hello
python hello.py
functions
printf("hello, world");
printf("hello, world\n");;
print("hello, world");
libraries
#include <cs50.h>
import cs50
from cs50 import get_string
string answer = get_string("What's your name?\n");
printf("hello, %s\n", answer);
string answer = get_string("What's your name? ");
printf("hello, %s\n", answer);
answer = get_string("What's your name? ")
print("hello, " + answer)
answer = get_string("What's your name? ")
print("hello,", answer)
answer = get_string("What's your name? ")
print(f"hello, {answer}")
answer = input("What's your name? ")
print(f"hello, {answer}")
variables
int counter = 0;
int counter = 0;
counter = 0
counter = counter + 1;
counter = counter + 1;
counter = counter + 1
counter += 1
types
bool
char
double
float
int
long
string
...
bool
float
int
str
...
range sequence of numbers
list sequence of mutable values
tuple sequence of immutable values
dict collection of key-value pairs
set collection of unique values
...
get_char
get_double
get_float
get_int
get_long
get_string
...
get_float
get_int
get_string
from cs50 import get_float
from cs50 import get_int
from cs50 import get_string
from cs50 import get_float, get_int, get_string
conditionals
if (x < y)
{
printf("x is less than y\n");
}
if (x < y)
{
printf("x is less than y\n");
}
if x < y:
print("x is less than y")
if (x < y)
{
printf("x is less than y\n");
}
else
{
printf("x is not less than y\n");
}
if (x < y)
{
printf("x is less than y\n");
}
else
{
printf("x is not less than y\n");
}
if x < y:
print("x is less than y")
else:
print("x is not less than y")
if (x < y)
{
printf("x is less than y\n");
}
else if (x > y)
{
printf("x is greater than y\n");
}
else
{
printf("x is equal to y\n");
}
if (x < y)
{
printf("x is less than y\n");
}
else if (x > y)
{
printf("x is greater than y\n");
}
else
{
printf("x is equal to y\n");
}
if x < y:
print("x is less than y")
elif x > y:
print("x is greater than y")
else:
print("x is equal to y")
str
object-oriented programming
OOP
docs.python.org/3/library/stdtypes.html#string-methods
docs.python.org/3/library/functions.html
docs.python.org
loops
int i = 0;
while (i < 3)
{
printf("meow\n");
i++;
}
int i = 0;
while (i < 3)
{
printf("meow\n");
i++;
}
i = 0
while i < 3:
print("meow")
i += 1
for (int i = 0; i < 3; i++)
{
printf("meow\n");
}
for (int i = 0; i < 3; i++)
{
printf("meow\n");
}
for i in [0, 1, 2]:
print("hello, world")
for i in range(3):
print("hello, world")
for _ in range(3):
print("hello, world")
while (true)
{
printf("meow\n");
}
while (true)
{
printf("meow\n");
}
while True:
print("meow")
named parameters
positional parameters
while True:
print("meow")
truncation
floating-point imprecision
integer overflow
integer overflow
exceptions
print
docs.python.org/3/library/functions.html#print
list
docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range
len
docs.python.org/3/library/functions.html#len
dict
key value
docs.python.org/3/library/stdtypes.html#mapping-types-dict
sys
docs.python.org/3/library/sys.html
pip