Approx. read time: 3.9 min.
Post: Learn Python Variables and Types
Learn Python Variables and Types – Introduction to Python Variables
Python is completely object-oriented and not statically typed. This means you do not need to declare variables before using them or specify their type. Every variable in Python is an object.
In this tutorial, we will cover the basics of Python variables, including numbers and strings. We will also provide examples and an exercise for practice.
Python Numbers – Learn Python Variables and Types
Python supports two main types of numbers: integers and floating point numbers. It also supports complex numbers, but we will focus on integers and floating point numbers for now.
Integers
To define an integer, use the following syntax:
# Define an integer myintvar = 7 print(myintvar)
This code defines an integer variable myintvar
and prints its value.
Floating Point Numbers
To define a floating point number, you can use one of the following notations:
# Define a floating point number myfloat = 7.0 print(myfloat) # Convert an integer to a float myfloat = float(7) print(myfloat)
These examples show how to define a floating point number directly and by converting an integer.
Python Strings – Learn Python Variables and Types
Strings are defined using either single quotes or double quotes.
# Define a string using single quotes mystring = 'hello' print(mystring) # Define a string using double quotes mystring = "hello" print(mystring)
Using double quotes makes it easier to include apostrophes in the string:
# String with an apostrophe mystring = "Don't worry about apostrophes" print(mystring)
Basic Operations
Simple operators can be executed on numbers and strings:
# Addition of integers one = 1 two = 2 three = one + two print(three) # Concatenation of strings hello = "hello" world = "world" helloworld = hello + " " + world print(helloworld)
These examples demonstrate integer addition and string concatenation.
Multiple Assignments
You can assign values to multiple variables simultaneously:
# Assign multiple variables at once a, b = 3, 4 print(a, b)
This example shows how to assign values to a
and b
in one line.
Mixing Operators
Mixing operators between numbers and strings is not supported:
# This will not work! one = 1 two = 2 hello = "hello" # Uncommenting the following line will cause an error # print(one + two + hello)
This code will produce an error because you cannot add integers and strings together.
TypeError: unsupported operand type(s) for Add: 'int' and 'str'
Exercise – Learn Python Variables and Types
Create a string, an integer, and a floating point number. The string should be named mystring
and contain the word “hello”. The floating point number should be named myfloat
and contain the number 10.0. The integer should be named myint
and contain the number 20.
# Define the variables mystring = "hello" myfloat = 10.0 myint = 20 # Testing code if mystring == "hello": print("String: %s" % mystring) if isinstance(myfloat, float) and myfloat == 10.0: print("Float: %f" % myfloat) if isinstance(myint, int) and myint == 20: print("Integer: %d" % myint)
Answer – Learn Python Variables and Types
In the exercise, you defined three variables: mystring
, myfloat
, and myint
. Here’s the testing code output if the variables are correctly defined:
String: hello Float: 10.000000 Integer: 20
Make sure your variables match the specified values to pass the test.
By completing this exercise, you have practiced defining and using different types of variables in Python.
Here are some popular online compilers where you can run Python code:
- Repl.it
- Repl.it Python Compiler
- Repl.it offers a collaborative environment where you can write and execute Python code. It’s user-friendly and supports many programming languages.
- Trinket.io
- Trinket.io Python Compiler
- Trinket.io is great for beginners and allows you to embed your code in web pages, making it perfect for educational purposes.
- PythonAnywhere
- PythonAnywhere Python Compiler
- PythonAnywhere is a cloud-based Python development environment. You can write, run, and share your Python code online.
- OnlineGDB
- OnlineGDB Python Compiler
- OnlineGDB is an online compiler and debugger tool for C, C++, Python, Java, and many more languages.
- JDoodle
- JDoodle Python Compiler
- JDoodle is an online IDE for Python, Java, and other languages. It offers features like code execution, compilation, and sharing.
- Programiz
- Programiz Python Compiler
- Programiz offers an online compiler for Python with a clean interface. It’s designed for learners and programmers who want to quickly run their code.
Example of Running Code on Trinket.io
To run the given Python code on Trinket.io:
- Go to Trinket.io Python Compiler.
- Copy and paste the Python code into the editor.
- Click the “Run” button to execute the code.