24
loading...
This website collects cookies to deliver better user experience
# file: hello.py
print("Hello, world!")
print()
will output to the screen whatever is inside of the parenthesis and between quotes. Above print()
we have a #
followed by something, everytime we see this #
in Python, we say it is a comment. Every comment is ignored by the Python interpreter.python hello.py
.print("Joseph is a boy who likes Python")
print("Joseph is 18 years-old")
print("Joseph likes to play video games")
print("Joseph is a Computer Science student")
name = "Joseph" # That's how we declare a variable in Python
print(name, "is a boy who likes Python")
print(name, "is 18 years-old")
print(name, "likes to play video games")
print(name, "is a Computer Science student")
name
anywhere. There is no need to write by hand the name "Joseph".age = 18 # int (integer)
wallet = 18.50 # float (floating point numbers)
name = "Joseph" # String (Collection of characters)
can_vote = True # Boolean (Boolean can be two things: True or False)
imaginary_number = 1j # Complex
if
, else if
and else
.age
and check if
you can vote based on your age
. The most common voting age at Brazil is 18. So we need to check if the received integer is greater than or equal to 18.age = 19
if age >= 18:
print("You can vote!")
else:
print("You can not vote!")
if age >= 18
will just be executed if the condidition age >= 18
is True. In this example above, the program will print out to the screen "You can vote!" because age = 19 and 19 is greater than 18, which means the if
is True, that's why it is gonna be executed instead of else
. else
is just executed when the conditions above him are false. If age was equal to 17, else
would be executed because age >= 18
is False. elif
. Let's write a program to check your grade.grade = "A"
if grade == "A":
print("Awesome!")
elif grade == "B":
print("Good")
elif grade == "C":
print("Not so good")
else:
print("Bad")
elif
, we can check more than one condition and we use else
without a condition because it will only be executed if and only if every condition above is false. In this example, the program will print out "Awesome!" because the first condition if grade == "A"
is True and the program will just finish after it.if something_is_true:
# Execute this
elif something_is_true:
# Execute this
else:
# Execute this if and only if every condition above is false
if
, elif
and else
when you need it. If you want to solve some programming puzzles, try this website here.first_color = "Red"
second_color = "Green"
third_color = "Blue"
my_list = [] # That's how you declare an empty list
colors = ["Red", "Green", "Blue"] # Declaring and defining the elements of a list
colors = ["Red", "Green", "Blue"] # Declaring and defining the elements of a list
print(colors[0]) # Output: Red
print(colors[1]) # Output: Blue
colors[0]
we get "Red" as the output, why? it's the first element of the list.append()
.colors = [] # Empty list
colors.append("Red") # colors = ["Red"]
colors.append("Blue") # colors = ["Red", "Blue"]
numbers = [2, 3, -1, 20]
size = len(numbers) # size = 4
numbers = [2, 3, -1, 20]
# Deleting element from the end of the array
numbers.pop() # numbers = [2, 3, -1]
# Deleting element at the index 1
numbers.pop(1) # numbers = [2, -1]
fruits = ["apple", "banana", "cherry", "apple"]
fruits.remove("apple") # fruits = ["banana", "cherry", "apple"]
fruits = ["apple", "banana", "cherry", "apple"]
fruits.clear() # fruits = []
numbers = [2, 3, -1, 20]
numbers.sort() # numbers = [-1, 2, 3, 20]
numbers.sort(reverse = True) # numbers = [20, 3, 2, -1]
sorted()
rather than sort()
.numbers = [2, 3, -1, 20]
sorted_numbers = sorted(numbers) # sorted_numbers = [-1, 2, 3, 20]
fact = "Python is awesome!"
print(fact) # "Python is awesome!"
print(type(fact)) # <class 'str'> ('str' means 'string')
print(fact[0]) # P
print(fact[-1]) # !
Operations with string
Capitalize string
country = "brazil"
country = country.capitalize() # Brazil
country = "brazil"
country = country.upper() # BRAZIL
country = country.lower() # brazil
if
condition.fact = "Python is awesome!"
if fact.endswith("awesome!"):
print("Yes, it ends with 'awesome!'")
fact = "Python is awesome!"
if fact.endswith("awesome!"):
fact.replace("awesome", "terrific") # Now, fact is "Python is terrific!"
names = "Jack, Zoe, Mike, John, Gabriel"
names = names.split(", ") # ["Jack", "Zoe", "Mike", "John", "Gabriel"]
split()
split the string using the space.country = "usa"
if country.islower():
country = country.upper() # USA
if country.isupper():
country = country.lower() # usa
info = ["Hicaro", 18, 232838131912-1]
print("My name is", info[0])
info
list. It is bad design. What should I do that? Basically, when you want to map something to another thing, we're gonna be using a Dictionary.info = {} # Empty dictionary
info["Name"] = "Hicaro"
info["Age"] = 18
info["Tel"] = 232838131912-1
if info["Name"] == "Hicaro":
print("My name is", info["Name"])
{}
.info = {"Name": "Hicaro", "Age": 18, "Tel": 232838131912-1}
if info["Name"] == "Hicaro":
print("My name is", info["Name"])
info["Name"]
is more readable than info[0]
.friends_name = ["Laurie", "Erlich", "Gilfoyle", "Richard", "Monica"]
print("Hello, ", friends_name[0])
print("Hello, ", friends_name[1])
print("Hello, ", friends_name[2])
print("Hello, ", friends_name[3])
print("Hello, ", friends_name[4])
print()
for each friend repetitively, that's why we use a loop. We have two options for looping (for
and while
). We use for
when we want to iterate over a sequence (it is not a rule, you can use anything you want) and while
when we want to repetitively do a thing while something is true.friends_name = ["Laurie", "Erlich", "Gilfoyle", "Richard", "Monica"]
for name in friends_name:
print("Hello, ", name)
for
because we can use in
. name
is basically a variable that represents an element of the list, starting from the first to the last element. This loop will stop when we iterate over the whole list.def my_function_name():
print("Printing something from function")
my_function_name() # Printing something from function
def
, after that I gave a name for my function (my_function_name
) so I can access it (just like we do with variables and list), after that I use ()
(I'll explain it later). Below the definition, I defined the body of the function, the block of code that we want to execute. In the last of code, we are calling the function using my_function_name()
and then it will execute the function. See more examples:def sum(number):
print(number + 3)
sum(2) # 5
number
will equal to 2 and then we will print 2 + 3
which is 5.my_sum = sum(2) # None
None
and not 5, why? Because we are not returning anything from the function, if you want to return
the sum and store it in the variable my_sum
, we should use the return
keyword.def sum(number):
return number + 3
my_sum = sum(2) # 5