Python 3 is a high purpose programming language that you can use for varying purposes such as data science, game development, website development, scripting, and so on.
What separates Python 3 from other programming languages such as C++ and Java is the fact that it is dynamically typed and easy to read syntax. With Python 3 developers can perform the same task with fewer lines of code as compared with other coding languages.
In this article, we will see all about programming with Python 3 i.e., basic syntax, variables, data types, etc. So, if you are interested in learning Python 3, then you should follow this guide till the very end.
Getting Started With Python 3 Programming
- Setup
- First Python 3 Program
- Variables
- Data types
- Operators
- If-else statements
- Project
Also Read: How To Make A Tic Tac Toe Game Using React Native.
Setup
1. Install the latest version of Python from the official website.
2. Install an IDE or text editor of your choice, such as IntelliJ, PyCharm, VS Code, Sublime, etc.
I will recommend VS Code as it is extremely lightweight and comes with premium features such as auto-completion, debugging tool, auto-indentation, bracket-matching, etc.
First Python 3 Program
Open up your IDE and type in the following:
print(“Hello, World!”)
Now click run. You should see the words “Hello, World!” in the terminal.
Congratulations, you just made your first Python program!
Variables
Variables are probably a new concept to you if you’re new to programming.
Necessarily, variables are containers with labels which store your data in the memory. Here’s an example of a variable:
x = “Hello, World!” print(x)
Here we declare the variable labeled x, and then we print it using the print() function.
Your output should be “Hello, World!”.
Data types
In programming, data types are an essential concept.
Variables can store data of different types, and various types can do different things.
Python has the following data types built-in by default, in these categories:
Text Type |
string |
Numeric Types |
integer, float, complex |
Sequence Types |
list, tuple, range |
Mapping Type |
|
Set Types |
set, frozenset |
Boolean Type |
boolean |
Binary Types |
bytes, bytearray |
To get the data type of an object, you use the type() function.
Here’s an example of how you can use it:
myint = 5 print(type(myint))
As the data type of the variable myint is an integer the output should be: <class ‘int’>
Setting the Data Type
In Python, the data type is set when you assign a value to a variable:
Example |
Data Type |
x = “Hello World” |
string |
x = 20 |
integer |
x = 20.5 |
floating point |
x = 1j |
complex |
x = [“apple”, “banana”, “cherry”] |
list |
x = (“apple”, “banana”, “cherry”) |
tuple |
x = range(6) |
range |
x = {“name” : “John”, “age” : 36} |
dictionary |
x = {“apple”, “banana”, “cherry”} |
set |
x = frozenset({“apple”, “banana”, “cherry”}) |
frozenset |
x = True |
bool |
x = b”Hello” |
bytes |
x = bytearray(5) |
bytearray |
x = memoryview(bytes(5)) |
memoryvie |
You can also specify the specific data type using the following constructor functions:
Example |
Data Type |
x = str(“Hello World”) |
str |
x = int(20) |
int |
x = float(20.5) |
float |
x = complex(1j) |
complex |
x = list((“apple”, “banana”, “cherry”)) |
list |
x = tuple((“apple”, “banana”, “cherry”)) |
tuple |
x = range(6) |
range |
x = dict(name=”Janna”, age=36) |
dict |
x = set((“apple”, “banana”, “cherry”)) |
set |
x = frozenset((“apple”, “banana”, “cherry”)) |
frozenset |
x = bool(5) |
bool |
x = bytes(5) |
bytes |
x = bytearray(5) |
bytearray |
x = memoryview(bytes(5)) |
memoryview |
Operators
1. Arithmetic operators:
+ Addition
– Subtraction
/ Division
* Multiplication
% Modulus
** Exponent
// Floor division
Examples:
# This is a comment so the interpreter will ignore it print(25+5) # Addition print(25-5) # Subtraction print(5*5) # Multiplication print(25/5) # Division print(5**5) # Exponent print(25//5) # Floor division
These are all the arithmetic operators in Python.
2. Comparison operators
== If two values are equal to each other, the condition is true.
!= If two values are not equal to each other, the condition is true.
<> If two values are not equal to each other, the condition is true.
> If the value of the left operand is greater than the value of the right operand, then the condition becomes true.
< If the value of the left operand is less than the value of the right operand, then the condition becomes true.
>= If the value of the left operand is greater than or equal to the value of the right operand, then the condition becomes true.
<= If the value of the left operand is less than or equal to the value of the right operand, then the condition becomes true.
If… elif… else…
Now that we have learned about operators, we’re ready to start telling our computer how to make decisions! We do this by using if, elif, and else statements.
Here’s an example to find a greater number between two integer values.
a = int(10) b = int(5) if a == b: print("a is equal to b!") elif a > b: print("a is greater than b!") else: print("None of the conditions are true! :O")
Above we used three types of statements and 2 comparison operators.
Explanation
Lines 1-2: we declared the variables a and b and assigned them the values 10 and 5.
Line 3-4: We created an if statement which says if a is equal to b, then print “a is equal to b!”
Line 5-6: An elif statement is added, which says if the above isn’t true and a is greater than b, then print “a is greater than b!”
Line 7-8: We made a third statement which says if none of the conditions are true, the print “None of the conditions are true! :O”
Python 3 If-else programs
- Python 3 Program To Check If Number Is Positive Or Negative.
- Python 3 Program to Check Armstrong Number.
- Python 3 Program to Find Largest Among Three Numbers.
User input
You’re probably wondering: How can I deal with user input in Python?
Well, it’s quite simple! You just use input()!
Here’s how you can do it:
name = str(input("What's your name? ")) print("Hello, "+name)
Line 1: The string inside of the input() function will be prompt to the user!
Line 2: Here is an example of string concatenation. We print the string “Hello,” concatenated to the variable name, which is our input.
Project
Congratulations! You now know the fundamentals of the Python programming language! You should now focus on a project to put what you have learned into practice.
My project recommendation is a simple terminal/command-line calculator. This project involves conditions, operators, variables, inputs, and lots of printing!
Also, check out this simple Python 3 project in which you will learn how to create a simple calculator.
print(“Thank you for reading!”)
Python 3 programs
- Python 3 Program To Add Two Matrices.
- Python 3 Program To Find The Factorial Of A Number.
- Python 3 Program To Convert Celsius To Fahrenheit.
- Python 3 Program to Find the Square Root of A Number.
- Python 3 Program to Calculate The Area of a Triangle.
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator.
With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!