Learning Python

Python: Variables and More


Hello Word!


Whenever learning a new programming language it is tradition to begin by writing a program that displays the message "Hello World!" So let's get started!


print("Hello World")


While there is not much to this line of code there are a couple important things to note. First is the use of the print function which in python is print().


print()


With out going into too much detail at this point, the print function is used when you want to display information on the screen to the user. In this case the message being displayed is "Hello World!"


Strings


Now onto the other part of the code. The words Hello, World! are enclosed in quotation marks, and this because we want to use those words as a string. A string is a particular data type, more on data types later, which can be words, characters, or symbols. Without the quotation marks, when the code is run it will result in an error.


print(Hello, World!)

>>SyntaxError: invalid syntax


Challenge


Our first challenge is to write a code that displays Hello, your name! where your name is replaced by your actual name.


print("Hello, Michael")

>>Michael


Variables


Variables are an important part of any programming language as they allow you to store information that can be used at any point of your code.


name = "Billy"

print(name)


In the above example the variables name is name and the information being assigned to the variable is the string "Billy"

Note that when you print variables you do not need quotation marks, even if a string is stored in the variable.

In addition to strings you can also store other data types, in this case we are naming a variable age and storing the integer 10.

age = 10

print(age)


Challenge:


Create a variable named food and assign to the variable the name of your favorite food. Then print the variable. What data type should you store in the variable?


Combining Variables and Strings


Now things begin to get a little more interesting when we are able to combine information stored in variables with other strings like in the example below.


name = "Billy"

print("Nice to meet your", name)

>>Nice to meet you Billy


Inputs


Getting input or information from the user is an essential feature to many programs and is our chance to write our first truly interactive program.


name = input("What is your name?")

print("Nice to meet you", name)


At first glance