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
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.
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.
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.
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
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.
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()
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.
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.
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
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
Post a Comment