Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Global and Local Variables in Python
Next article icon

Introduction to Python has been dealt with inthis article. Now, let us begin with learning python.

Running your First Code in Python 
Python programs are not compiled, rather they are interpreted. Now, let us move to writing python code and running it. Please make sure that python is installed on the system you are working on. If it is not installed, download it fromhere. We will be using python 2.7.

Making a Python file: 
Python files are stored with the extension ".py". Open a text editor and save a file with the name "hello.py". Open it and write the following code:

Python3
print("Hello World")# Notice that NO semi-colon is to be used

Time Complexity: O(1)
Auxiliary Space: O(1)

Reading the file contents: 
Linux System - Move to the directory from the terminal where the created file (hello.py) is stored by using the 'cd' command and then type the following in the terminal : 

python hello.py

Windows system - Open command prompt and move to the directory where the file is stored by using the 'cd' command and then run the file by writing the file name as a command.

Variables in Python 
Variables need not be declared first in python. They can be used directly. Variables in python are case-sensitive as most of the other programming languages. 

Example: 

Python3
a=3A=4print(a)print(A)

The output is : 

34

Time Complexity: O(1)

Auxiliary Space: O(1)

Expressions in Python 
Arithmetic operations in python can be performed by using arithmetic operators and some of the in-built functions. 

Python3
a=2b=3c=a+bprint(c)d=a*bprint(d)

The output is : 

56

Time Complexity:O(1)

Auxiliary Space:O(1)

Conditions in Python 
Conditional output in python can be obtained by using if-else and elif (else if) statements. 

Python3
a=3b=9ifb%a==0:print("b is divisible by a")elifb+1==10:print("Increment in b produces 10")else:print("You are in else statement")

The output is : 

b is divisible by a

Time Complexity:O(1)

Auxiliary Space:O(1)

Functions in Python 
A function in python is declared by the keyword 'def' before the name of the function. The return type of the function need not be specified explicitly in python. The function can be invoked by writing the function name followed by the parameter list in the brackets. 

Python3
# Function for checking the divisibility# Notice the indentation after function declaration# and if and else statementsdefcheckDivisibility(a,b):ifa%b==0:print("a is divisible by b")else:print("a is not divisible by b")#Driver program to test the above functioncheckDivisibility(4,2)

The output is : 

a is divisible by b

Time Complexity:O(1)

Auxiliary Space:O(1)


So, python is a very simplified and less cumbersome language to code in. This easiness of python is itself promoting its wide use.


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp