This article will help you quickly speed up with Python, the programming language designed for beginners and everybody else. It will also explain what declaring a variable means, what a function is, what’s a class and an object, and how to write Python apps without blowing your mind
Let’s begin! ✨
a = ‘a’
x = 5
c = False
x = 5
You can change the variable type by assigning a value of a different type to it:
x = 5 # x is a Integer type
x = ‘Hello’ # x is now a String
Booleans are capitalized in Python
Semicolons in pythons are only used if we use multiple statements on the same line, otherwise, it is not used to terminate statements
· We use the type() function to check the type of an object in Python
The graphical description of their syntax:
In addition to the division operator, Python also has a floor division operator.
Python represents the floor division with (//).
Comments in Python
Comments are critical to writing clean code.
· In Python, we use a hashtag (#) to write a comment.
In Python: # Comment
· Python has the following built-in data types str :(text type), int, float, complex (Numeric types), dist, tuple, range :(sequence type), dict :(Mapping type), set, frozenset: (Set Types), bool :(Boolean Type), bytes, byte array, :(Binary Types).
💡 Note: In python string Literals we use single (‘ ’)or double (“ “) quotation marks.
Let’s Understand with an example! ✨
It means Hello with the double quotation and single have the same output.
Assign a string to a variable
To assign a variable we use the (=) sign.
Numeric data types
Python has three types of numeric data types which include:
float(floating-point numbers), and
complex. Each one of them has its own properties, characteristics, and applications.
Y= 2.8 #float
Z = 1j #complex
None vs. null
In Python, there is a particular value called None that we typically use to indicate that a variable doesn’t have a value at a particular point in the program.
Conditions of use can be based on specific criteria but by default can be ignored by the user. True or False.
· while in python it relies on indentation to indicate which lines of code are conditional codes.
For multiple conditions
· We use
elif keyword followed by the condition in Python. After that, we write a colon (
:) and indent the code on the next line.
“No switch in Python?? You read it right there’s no
switch in Python. So no switch party over!
For Loop and while loop in Python
let’s see how we can define different types of loops in Python
In Python, we write the keyword for followed by the name of the loop variable, the keyword in, and a call to the range() function specifying the necessary parameters. Then, we write a colon (:) following the body of loop indented.
Let’s understand with an example! ✨
fruits = [“apple”, “banana”, “cherry”]
for x in fruits:
Python uses the keyword while followed by the condition, a colon (:), and in a new line, the body of the loop (indented). Then, we write a colon (:) followed by an indented block
Let’s understand with an example:
i = 1
while i < 6:
i += 1!
Using Python, we write the keyword def followed by the name of the function, and within parentheses the parameters list. After this, we write a colon (:) and the body of the function.
💡 Tip: In Python, a function can only take a certain number of arguments. If you call a function with a greater number of arguments than it expects, an exception will occur.
>>> def foo(x, y):
>>> foo(2, 4, 7)
Traceback (most recent call last):
File “<pyshell#3>”, line 1, in <module>
foo(2, 4, 7)
TypeError: foo() takes 2 positional arguments but 3 were given
Object-oriented programming in Python
let’s see how you can create and use the main elements of this programming paradigm.
Creating a class
· In Python, after the name of the class, we write a colon (:)
X = 5
<class’ — -main — -Myclass’>
· __init__ is one of the reserved methods in Python. it’s known as a constructor. This method can be called when an object is created from the class, and access is required to initialize the attributes of the class
Object Methods in Python
In Python, we define methods with the def keyword followed by their name and the parameters list within parentheses. This parameters list starts with the self parameter to refer to the instance that is calling the method. At the end, we write a colon (:) and the body of the method indented.
Example: Method in a Python Class
Def — -init — -(self, name, age):
Self.name = name
Self.age = age
Print(“Hello my name is “ + self.name)
P1 = person(“john”,36)
Hello my name is john
To create instances of a class:
· In Python, we write the name of the class and pass the arguments within parentheses.
my_circle = Circle(5, “Red”)
my_circle = new Circle(5, “Red”);
There is a lot more to Python than what’s in this article. I highly recommend you check out the Python docs for tutorials and details about other language features.
And remember, the best way to learn a language is to write it, a lot. And start practicing building some projects, So get to coding!
✨ Thanks for reading and stay tuned to this blog! I hope you liked this article and found it interesting!