Professional Documents
Culture Documents
CSC2401 Lecture 2
CSC2401 Lecture 2
I (Python)
❑Python Variables
Creating Variables
Casting
Get the Type
Single or Double Quotes?
Case-Sensitive
Python - Variable Names
Multi Words Variable Names
Python Variables - Assign Multiple Values
Python - Output Variables
Python - Global Variables
Python - Variable Exercises
Or by creating a python file on the server, using the .py file extension, and running it in the
Command Line:
Where in other programming languages the indentation in code is for readability only, the
indentation in Python is very important.
The number of spaces is up to you as a programmer, the most common use is four, but it has to be at least
one.
A comment does not have to be text that explains the code, it can also be used to prevent
Python from executing code:
Casting
If you want to specify the data type of a variable, this can be done with casting.
You will learn more about data types and casting later in this tutorial.
Case-Sensitive
Variable names are case-sensitive.
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different variables)
❑ Camel Case
Each word, except the first, starts with a capital letter:
❑ Pascal Case
❑ Snake Case
Each word is separated by an underscore character:
Note: Make sure the number of variables matches the number of values, or else you will get an error.
❑ One Value to Multiple Variables
And you can assign the same value to multiple variables in one line:
❑ Unpack a Collection
If you have a collection of values in a list, tuple etc. Python allows you to extract the values
into variables. This is called unpacking.
Notice the space character after “Python ” and “is ” , without them the result would be "Pythonisawesome".
The best way to output multiple variables in the print() function is to separate them with commas,
which even support different data types:
Test Yourself With Exercises: Now you have learned a lot about variables, and how to use them in Python.
Try to insert the missing part to make the code work as expected:
1. Create a variable named carname and assign the value Honda to it.
6. Insert the correct syntax to assign the same value to all three variables in one code line.
7. Insert the correct keyword to make the variable x belong to the global scope.