Popular Posts

Friday, August 14, 2020

Python Beginner: Variables and Data types

 Hello, Developer!


Today, we will learn "variables". It is an important concept of programming. Let's think "variable" as box something we put it in. We can store "Text", "Numbers" and "True" or "False" values in this "variable". And we can use it for input, calculation, comparison and so on. I will show you some examples.


Variable examples:

- Strings(Plain text) type: my_name="Seb"

- Numbers type: my_age=100 

- Boolean type: is_developer=True, is_alien=False


Let's create a new Python file and input first exercise codes as below. This exercise codes don't include any variables yet! 

print("There once was a man named George,")
print("he was 70 years old. ")
print("he really liked the name George, ")
print("but didn't like being 70.")

I showed the first program result as following. 

Have you seen the same result? Alright, let's update codes using "variables" as below. 

I modified some codes using the "str()" command, so it shows 3 variables type as well. The "str()" command change the variable type from Numbers or Booleans into Strings type and it can be displayed through "print" command. 

Here are modified second program codes and the result.

character_name = "John"
character_age =35
is_male = True
print("There once was a man named " + character_name + ", ")
print("he was " + str(character_age) + " years old. ")
print("Is he male?" + str(is_male))
character_name = "Sarah"
character_age = 45
is_male = False
print("She really liked the name " + character_name + ", ")
print("but didn't like being " + str(character_age) + ".")
print("Is he male?" + str(is_male))


Can you see the same result? Great, now you can create and use variables in your program.

See you next post, developer! :) 

No comments:

Post a Comment

Python - Web crawling exercises

Hello Python developer! How are you? Hope you've been good and had wonderful time with Python. There are 10 Python requests module exerc...