From c931aee2630fae1d8c2b917c2545bf7d09f444f4 Mon Sep 17 00:00:00 2001 From: Kilokem Date: Fri, 13 Sep 2024 12:04:36 +0200 Subject: [PATCH] FirstThreeLessons --- LearnTheBasics/Lists.py | 44 +++++++++++++++++++++++++++++ LearnTheBasics/VariablesAndTypes.py | 30 ++++++++++++++++++++ README.md | 1 + 3 files changed, 75 insertions(+) create mode 100644 LearnTheBasics/Lists.py create mode 100644 LearnTheBasics/VariablesAndTypes.py create mode 100644 README.md diff --git a/LearnTheBasics/Lists.py b/LearnTheBasics/Lists.py new file mode 100644 index 0000000..55d02ed --- /dev/null +++ b/LearnTheBasics/Lists.py @@ -0,0 +1,44 @@ +mylist = [] + +#Appending elements to a list +mylist.append(2) +mylist.append(4) +mylist.append(6) +mylist.append(7) +mylist.append(2) +mylist.append(1) + +print(mylist[0]) +print(mylist[1]) +print(mylist[2]) + +# Iterating through a list +for x in mylist: + print("Listvalues:",x) + +# Accessing an index which does not exist generates an exception (an error). +# mylists = [1,2,3] +# print(mylists[10]) + +# Exercise +# In this exercise, you will need to add numbers and strings to the correct lists using the "append" list method. You must add the numbers 1,2, and 3 to the "numbers" list, and the words 'hello' and 'world' to the strings variable. + +# You will also have to fill in the variable second_name with the second name in the names list, using the brackets operator []. Note that the index is zero-based, so if you want to access the second item in the list, its index will be 1. + +numbers = [] +strings = [] +names = ["John", "Eric", "Jessica"] + +# write your code here +second_name = names[1] +for i in range(1,4,1): + numbers.append(i) + +strings.append("Hello") +strings.append("world") + + +# this code should write out the filled arrays and the second name in the names list (Eric). +print(numbers) +print(strings) +print("The second name on the names list is %s" % second_name) \ No newline at end of file diff --git a/LearnTheBasics/VariablesAndTypes.py b/LearnTheBasics/VariablesAndTypes.py new file mode 100644 index 0000000..6c74450 --- /dev/null +++ b/LearnTheBasics/VariablesAndTypes.py @@ -0,0 +1,30 @@ +myint = 7 +print(f"The myint variable which contains: {myint}, type is: ", type(myint), "\n") + +myfloat = 7.25 +myfloat = float(7) +print(f"The myint variable which contains: {myfloat}, type is: ", type(myfloat), "\n") + +mystring = "HOLA!" #The difference between the two is that using double quotes makes it easy to include apostrophes +mystring = 'HEY!!' +print(f"The myint variable which contains: {mystring}, type is: ", type(mystring), "\n") + +#Assignments can be done on more than one variable "simultaneously" on the same line like this +a, b = 3, 4 +print(a, "\n" + str(b)) + +#Exercise +#The target of this exercise is to create a string, an integer, and a floating point number. The string should be named mystring and should contain the word "hello". The floating point number should be named myfloat and should contain the number 10.0, and the integer should be named myint and should contain the number 20. + +# change this code +mystring = "hello" +myfloat = float(10) +myint = 20 + +# testing code +if mystring == "hello": + print("String: %s" % mystring) +if isinstance(myfloat, float) and myfloat == 10.0: + print("Float: %f" % myfloat) +if isinstance(myint, int) and myint == 20: + print("Integer: %d" % myint) \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f16b06d --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +This project is based on the https://www.learnpython.org/ python tutorial. \ No newline at end of file