Python is a versatile and powerful programming language known for its readability and simplicity. Here are some basic concepts and syntax in Python:
Print Statement:
To display output, use the print statement:
python
Copy code
print("Hello, World!")
Variables:
Variables store data. No need to declare the data type explicitly.
python
Copy code
x = 5
message = "Hello"
Data Types:
Python has various data types, including integers, floats, strings, lists, tuples, and dictionaries.
Indentation:
Python uses indentation (whitespace) to define code blocks. This is a fundamental aspect of Python's syntax.
python
Copy code
if x > 0:
print("Positive number")
Comments:
Use the # symbol for single-line comments.
python
Copy code
# This is a comment
Input:
To take user input, use the input() function.
python
Copy code
name = input("Enter your name: ")
Conditional Statements:
Python supports if, elif, and else statements for decision-making.
python
Copy code
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
Loops:
Python supports for and while loops.
python
Copy code
for i in range(5):
print(i)
while x > 0:
print(x)
x -= 1
Functions:
Functions are defined using the def keyword.
python
Copy code
def greet(name):
print("Hello, " + name)
greet("John")
Lists:
A list is an ordered collection of items.
python
Copy code
my_list = [1, 2, 3, 'apple', 'orange']
Tuples:
Similar to lists, but immutable.
python
Copy code
my_tuple = (1, 2, 3, 'apple', 'orange')
Dictionaries:
A collection of key-value pairs.
python
Copy code
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
String Manipulation:
Python offers a variety of string manipulation methods.
python
Copy code
message = "Hello, World!"
print(message.upper())
Importing Modules:
Python has a vast standard library. You can import modules to extend functionality.
python
Copy code
import math
print(math.sqrt(25))
Exception Handling:
Use try, except blocks for handling exceptions.
python
Copy code
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero.")
These basics provide a foundation for understanding Python programming. As you continue learning, you'll explore more advanced topics and become proficient in Python development.
Comments
Post a Comment