CARVIEW |
- CCNA online course
- Linux online course
- VMware ESXi online course
- Nmap online course
- MySQL online course
- Raspberry Pi online course
- Apache HTTP Server course
- VMware Player online course
- Splunk online course
- SQL online course
- Oracle VirtualBox online course
- Python online course
- Asterisk course
- VMware Workstation Player course
- Process Explorer course
- Pillow online course
- Create a web crawler in Python
- A short introduction to…
What are variables?
In programming, variables are storage locations that are used to store information that can later be referenced and manipulated. You can consider a variable as a sort of a container where you store an information that can later be accessed with the variable name.
In Python, variables are created when they are first assigned values. Values are assigned using the assignment operator (=). For example, to create a variable named x and store the numeric value of 3 in it, you can use the following command:
x = 3
The command above does three things:
1. Creates an object that will represent the value 3.
2. Creates the variable x.
3. Links the variable x to the new object 3.
You can now use the variable x in your code. For example, to print the content of the variable, you can use the following command:
>>> print (x) 3
Notice how you didn’t need to write quotes around the variable name. When you want to print the content of a variable, quotes are not used. Consider what would happen if you use quotes:
>>> print ('x') x
Python considers the text inside the quotes to be a string and not a variable name. That is why the letter x was printed, and not the content of the variable x.
Once declared, the value of a variable can be changed:
>>> x = 3 >>> print (x) 3 >>> >>> x = 5 >>> print (x) 5 >>>
Notice how the first print() function printed the value of 3. We have then changed the value of the variable x to 5. The second print() function printed the value of 5.
Python course
- Introduction
- Python overview
- Install Python on Windows
- Install Python on Linux
- Add Python to the Windows Path
- Run Python code
- Interactive prompt
- IDLE editor
- Command line
- Help mode
- Basic programs
- Write your first program
- Use comments
- What are variables?
- Variable data types
- Variable names
- Numeric variables
- Strings
- Get the current date and time
- Operators overview
- Arithmetic operators
- Comparison operators
- Logical operators
- Assignment operators
- Membership operators
- Identity operators
- Conditional statements
- The if statement
- Get user input
- The if...else statement
- The if...elif statement
- Nested if statements
- Use logical operators
- Loops
- The for loop
- Use for loop with the range() function
- The break statement
- The continue statement
- The pass statement
- Use else statement in loops
- The while loop
- Nested loop statements
- Errors
- Types of errors
- Syntax and logical errors
- The try...except statements
- The try...except...else statements
- The try...except...finally statements
- Catch specific exceptions
- Raise exception
- Nest exception handling statements
- Modules
- What are modules?
- Import modules
- Find files on disk
- Display module content
- Strings
- What are strings?
- Escape characters
- Access individual characters
- String functions
- Search strings
- Concatenating strings
- Lists, sets, tuples, dictionaries
- What are lists?
- Modify lists
- Loop through a list
- Check whether a value is in a list
- Sorting lists temporarily
- Sorting lists permanently
- Obtaining the list length
- What are sets?
- What are dictionaries?
- Add new key-value pair to a dictionary
- Modify a value in a dictionary
- Delete a key-value pair in a dictionary
- Loop through a dictionary
- What are tuples?
- Looping over a tuple
- Working with files
- How to read and write files
- Read a file
- Read and write – with statement
- Make a list of lines from a file
- Functions
- What are functions?
- Return statement
- Positional arguments
- Keyword arguments
- Default values for parameters
- Flexible number of arguments
- Variable scopes