Skip to main content

Let’s Get Started with Python



NOTE: The tutorial is aimed at people who have no prior programming experience. If you have previous experience with other programming language (C) and just want to compare the syntax you can jump to this page(link will be updated soon).
If you have no compilers installed then you may follow the installation guide here>> How to Install PyCharm?
Tutorial00
Statements

Let’s start by using print(), it simply prints whatever you type in double or single codes. As per the tradition let’s print Hello World.
In the python file you created type print(“Hello World”) or print(‘Hello World’) as shown below


Then right click on the python file in the left bar and click run ‘filename’ in the dialogue box that will appear. This will run your program and the output will be displayed in the terminal below.
Let us enhance our program by printing through reference.
Here we take a variable x and give it some value, for example 0. Now, I want my program to print the value of x and not the variable x. So I’ll take the variable and initialize its value. In the next line I’ll print(x). Here you see we don’t use any codes while printing the value of x. Go ahead and try print(‘x’) and see the difference in output. Also, you are free to swap these lines of code and hence you can understand the execution of your program.
Type of an Object
If you want to know the type of variables you’re using, you will find type() very helpful. In the example below we need to find the type of variable x and even want to print its type.

So here we store the type of variable x in another variable y and print its type. The print function adds a newline by default so if you want to print in the same line then you can type end=" " after putting a comma in the print statement.
We see that storing the type of variable x in y adds to more work so why don’t you try printing its type without storing simply by using print(type(x)).
Swap the lines of code and get ready for the errors. Trust me these errors will really help you to understand the execution of every line and the meaning of every space.

Operators
You can perform mathematical operations using math operators like addition in this example
print("Lets Add")
a = 10
b = 20
sum = a + b
print('sum of',a,'and',b,'is',sum)
Use + for addition, - for subtraction, * for multiplication, / for division and % (modulo) to know the remainder.
Interactive Exercise
Now this is going to be an interactive exercise. You can code it differently but try making it interactive with all you have learnt till now. Here is an example below.
name = input('What is your name?')
print("Hello", name)
age = input("What is your age?")
print("You are young at", age)
If-Else
Now, going with its literal meaning, if this is true then do this, else if this is true then do that.
With this you can check conditions and do much more. Suppose you ask a user for his name and wants to check if the input is valid, a valid input in this case should be a combination of alphabets and not integers.
 
name = input("What is your name?")
if name.isalpha() == True:
    print("Hello",name)
else:
    print('please enter a valid input')

Here, the function isalpha() is checking the input (here, name) is alphabetic, by returning True.
Try print(name.isalpha()). If you will give it an invalid input then else condition will be executed.
You can also check for integers using isnumeric()

Alright, it’s the time for some exercise.
All the people less than the age of 18 are minors so you are asked not to register them and all the people above 40 years of age are also not permissible for registration. So, write a program to register only the valid users. Here is one way to do it.

name = input('what is your name?')
if name.isalpha() == True:
    print('Hello', name)
    age = input('what is your age?')
    if age.innumeric() == True:
        if int(age) < 18:
            print('youre a minor, cant be registered')
        elif int(age) > 40:
            print('Application cancelled')
        else:
            print('Registered')
    else:
        print('please enter your age in digits')
else:
    print('please enter a valid input')

Note: If you will give the input name as danny man the program will exit telling you to enter a valid input since space is not an alphabet. 
Here we specify the data type of age int(age) when comparing it with an integer.

While Loop
This is another interesting property. It is very useful when you don’t want to code again and again and you can simply put it in the while loop with a counter.
The counter counts the number of times the loop will work. In the example below you see the condition is entered in while(condition) and the counter (here i) is decreasing its value. The loop works till the value of i is greater than 0.

i = 5
while(i>5):
    print(i)
    i = i - 1
NOTE: The conditional statements if, while, for all terminate with a colon (:) and the next line of code is typed leaving four blank spaces (not tab in pycharm)
For Loop
Similarly we can use for loop. Here is an example below. This loop initialize i with the value 0 and the loop will execute till i<5

for i in range(0,5):
    print(i)
Congratulations you have learned all the basic of Python Programming. Now you're ready to step up⌣⌣⌣😊

Comments

Popular posts from this blog

How to Install PyCharm? (Windows, Mac, Ubuntu)

  A little introduction PyCharm is one of the widely used IDE for python programming and used by many programmers these days. It is available in three editions: Professional, Community and Educational (Edu). We will prefer Edu edition as it is an open source and free to use. Also, it fits our purpose of learning python as a beginner. If you have already installed pycharm or some other compiler you can skip this and read the tutotrial here>> Let's Get Started with Python Install PyCharm You can go to the official page and follow the guide to installation, to go to the official page click here or stick with me and follow the steps below. This is the direct link to install PyCharm Edu>> click here Already Installed PyCharm Community or Professional? Then you just need to install EduTools Plugin. To install the plugin follow these simple steps: 1.        Go to Files>>Settings... or Press Ctrl+Alt+S to open Setting/Preferences dialogue. 2.  

101/Introduction to C language - part 1

We are assuming you have C compiler already Installed in your systems, if not you can read  How to install C compilers ? (Ubuntu, Mac, Windows) What is C ? C is high-level compared to assembly but low leveled compared to JavaScript. But in general terms C is considered as high level language . So when we talk about c language, we usually hear the following terms Header file Data types Variables Main function Operators Hello World Program ( gateway to programming world  😉 ) Addition/Subtraction program for the basic understanding of Operators What is Header File ? As a beginner I never got a satisfactory answer for the same. In a nutshell, we can say “ It's just like a brain to a body ” ,  It enables us to use the functionality which is pre-implemented by the Developers, just like the brain adds the functionality to other body parts. Header file has a file extension of '.h'  for example “stdio.h” also called standard input-ou