Cs50 Week 6 Lecture Summary: Introduction to Python

Cs50 has been interesting so far, the course begins with a low-level language C to teach its student the basics of programming, now we are transitioning from C to python.

Python is a high-level programming language, where we begin to talk about the usefulness, benefit and easy implementation of its code.

In this blog post I will talk about what python is, the kind of application it can run, how it differs and relate to C.

My Cs50 Lecture 6: Python Summary

Python is an interpreted, object-oriented and high-level programming language with multipurpose features. Python is currently one of the top trends languages in the world of programming as at the time of writing this post, because of its easy to write and execute features most people prefer to use it

Examples of popular website app built using python are Instagram, Google, Netflix, Quora and so on.

It’s used across most industry in Machine Learning, Data Science, Web Programming, Game development e.t.c

Writing Python is simple, we only need to open up a text editor and save with .py extension or we can download our preferred IDE online.

Unlike the C program which has to be compiled before running it, Python program can be run without clearly compiling it first.

  • In Python, They are many data types
    • bool, True or False
    • float, real numbers
    • int, integers
    • str, strings
    • range, sequences of numbers
    • list, sequence of mutable values, that we can change or add or remove
    • tuple, sequence of immutable values, that we can’t change
    • dict, collection of key/value pairs, like the hash table
    • set, collection of unique values
  • Python Variable
    • Working with Python variable is completely different from C, we don’t need to specify the data type, we only initialize it and the program understands it.
    • for example in C we could say int x = 3; but in python, we just have to declare it like x = 3, and no need for colon(:).
  • Conditional Statements:
    • While Python conditional statements still tend to look like C, but they are some slight difference, some old condition is gone.
    • For example in C, we can say if {something}, else if {something} but in Python no more curly bracket after statement, unless your value is round around another variable, nothing like else if, but now elif. Even commenting in Python now start with ‘#’ and not // in C, we can also use the input() function to get input from the user instead of scanf or getchar in C.
  • Loops
    • Unlike C we have three varieties of loop, which are the (for, while and do-while loop), Python has only 2 of them, which are: “the (for loop and while loop)”, no more do-while loop. You can implement a substitute of the do-while loop with the while loop by utilizing break function. Also in Python no more (++ increment) but rather +=1 or var = var + 1 and others.
#do_while sample code in Python
#a Program that force user to input Negative integer

while True:
	integer = int(input("A Negative Integer: ")) 
	if integer < 0:
print("Your value is: ", + integer)
  • Array (List)
    • Array in Python has more functions compare to C. Python array are called List, unlike C, Python array is not fixed, they can grow or be shrink as needed.
    • To declare a list we can use the square bracket. e.g number = [1,2,3,”A”,”c”]. we can also add element into the list by using the .append methods.
    • Also no more strlen as in C, we now have len() in Python.
  • Tuple
    • python also has a data type Tuple, that is not quite like anything comparable to C.
    • Tuples are ordered, immutable set of data; they are great for association collections of data
  • Dictionary
    • Python also has built-in support for dictionaries, it allows us to store data values in key-value pairs, it’s just like a hash table in C, where we can store any value and index it with a key.
#Dictionaries of Best Student in the Science department

students = {"Physics":"Seun", "Chemistry":"Collins", "Biology":"Vincent"}

for subject, student in students.items():
	print("Best Student in {0} is {1}".format(subject,student)) 
  • Functions
    • We can also declare a function in Python. Like a variable in Python, we don’t need to specify the return type of function.
    • All functions are introduced with the def keyword (defined keyword)
#A program that print Cough in x times

#define cough function with def keywords
def cough(number):	
	for i in range(number):

#number of times, cough should print
while True:
	value = int(input("How many times to cough: "))
	if value > 0:
  • Object in Python
    • An object in python is sort of analogous to C structures.
    • To define a type of object, we use the keyword class, which stands as an object constructor.
    • Object can also contain method. Methods in objects are function that belongs to the object.
class info: #Class as a constructor for creating an object

	def __init__(self, name, age): 
		self.name = name
		self.age = age
	#A function that will behave like method
	def print(self):
		print("My name is {}, and I am {}years old.".format(self.name, self.age))
first_name = info("John", 24)

#print method is called on object 
  • Styles in Python
    • Good style is really important in Python, unlike C, no more curly braces for blocks, now they are used to declare dictionaries.
    • Tab and indentation are needed to be used correctly in python else the program will not work as intended
  • Including Files
    • Just like C programs can consist of multiples files to form a single program, so can Python programs tie files together. We include our specified file by using the keyword import <file>/ module
  • Regular expressions
    • Regulation expressions or patterns is used to match a particular set of strings, for example to find if “a” is presence in “an”. To use RegEx we have to import the module
  • Some examples of Regex syntax used are:
    • ., or any character
    • .*, for 0 or more character
    • .+, for 1 or more characters
    • ? for something optional
    • ^, for start of input
    • $, for end of input


Cs50 is a very interesting course, I love the fact that we start with a low-level language C, making me understand the basics of programming and how the system works generally.

While this is just a summary of what I learned from the Cs50 Week6 lecture, they are also more examples and code I did not include in this post. You can always refer to EDX to learn more about Cs50

Thanks for reading.

have anything to say or add? pls, don’t hesitate to use the comments section...

have you taken any python basic courses in the past and how was it?

Leave a Reply